-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransmitter.m
169 lines (133 loc) · 5.16 KB
/
transmitter.m
1
clear all;close all;%----------------------------------------------------%---------------- Initial Parameters ----------------%----------------------------------------------------numberOfBits = 100; %number of bitsQAM = 4; %4-QAMBitsPerSymbol = log2(QAM); %number of bits in one symbolif mod(numberOfBits, BitsPerSymbol) ~= 0 error('numberOfBits must be a multiple of log2(M).');endFc_tx = 400; %carrier frequencyphase_tx = 0; %carrier phasephase_tx = 2*pi*0.13;Fs = 10000; %sampling frequencydt = 1/Fs; %simulation time differentialRs = 100; %Symbol rateNs = Fs/Rs; %Number of samples per symbol%----------------------------------------------------%----------------Creation of the bitstream ----------%----------------------------------------------------dataIn = randi(2,1,numberOfBits) - 1; % Generate vector of binary data%dataIn = [0 0 0 0 1 1];%----------------------------------------------------%----------------- QAM Mapper -----------------------%----------------------------------------------------numberOfSymbols = length(dataIn)/BitsPerSymbol; %number of symbols in the FIFO% Define mapping table applying Gray mappingif QAM == 4 mappingTable(1) = 1 + 1*j; mappingTable(2) = -1 + 1*j; mappingTable(3) = 1 - 1*j; mappingTable(4) = -1 - 1*j;endif mappedSymbols = zeros(1, numberOfSymbols);% Map bits to symbolsfor i = 1 : BitsPerSymbol : length(dataIn) symbolBits = dataIn(i:i+BitsPerSymbol-1); symbolIndex = 2^1 * symbolBits(1) + 2^0 * symbolBits(2); % Mapping mappedSymbols((i - 1)/BitsPerSymbol + 1) = mappingTable( symbolIndex + 1); endfor%----------------------------------------------------%----------------- Upsampling -----------------------%----------------------------------------------------%% Adding zeros between symbolsfor i = 1:length(mappedSymbols) mappedSymbols_upsampled( (i-1)*Ns+1 : i*Ns) = [mappedSymbols(i) zeros(1, Ns-1)];end%----------------------------------------------------%----------------- Pulse shaping --------------------%----------------------------------------------------% Sqrt-Raised-Cosine Impulse Response Filteralpha = 0.35; % excess bandwidth factor = 0.35 for IS 136G = 4; % Group delay (how many symbols are delayed) % Order of filter is N = Ns * G + 1A_SRRC = 1; n = -Ns*G : Ns*G;for i = 1 : length(n) if(n(i) == 0) B_SRRC(i) = (1/Ns) * ( 1 - alpha + 4 * (alpha/pi)); elseif (abs(n(i)) == Ns/(4*alpha)) B_SRRC(i) = (1 + (2/pi)) * sin(pi / (4*alpha)) + (1 - (2/pi)) * cos(pi / (4*alpha)); B_SRRC(i) = (1/Ns) * (alpha/sqrt(2)) * B_SRRC(i); else B_SRRC(i) = sin(pi * (1-alpha) * n(i)/Ns) + 4*alpha*n(i)/Ns * cos(pi * (1+alpha) * n(i)/Ns); B_SRRC(i) = B_SRRC(i) / (pi * n(i)/Ns * (1 - (4*alpha*n(i)/Ns)^2) ); B_SRRC(i) = (1/Ns) * B_SRRC(i); end end% Normalize by the energyB_SRRC = B_SRRC/sqrt(B_SRRC*B_SRRC');%Adding zeros at the end to take into account the delay given by the filters (at the transmitter and the receiver)mappedSymbols_upsampled_and_padded = [mappedSymbols_upsampled zeros(1, 2*Ns*(G+2))];Tx_I_BB = filter(B_SRRC, A_SRRC, real(mappedSymbols_upsampled_and_padded)); Tx_Q_BB = filter(B_SRRC, A_SRRC, imag(mappedSymbols_upsampled_and_padded));%----------------------------------------------------%----------------- Modulation: Baseband to RF -------%----------------------------------------------------%t = 0 : dt : ((length(mappedSymbols) + 2*G) * Ns - 1) * dt;t = 0 : dt : (length(Tx_I_BB) - 1) * dt;Tx_I = Tx_I_BB .* (sqrt(2)*cos(2*pi*Fc_tx*t+phase_tx));Tx_Q = Tx_Q_BB .* (sqrt(2)*sin(2*pi*Fc_tx*t+phase_tx));Tx = Tx_I - Tx_Q;%----------------------------------------------------%----------------- Figures --------------------------%----------------------------------------------------##figure(1);##stem(dataIn,"filled");##title('Data In');##xlabel('Bit Index');##ylabel('Binary Value');##figure(2);##subplot(2,1,1);##stem(real(mappedSymbols));##title("Real part")##subplot(2,1,2);##stem(imag(mappedSymbols));##title("Imaginary part");##figure(3);##scatter(real(mappedSymbols), imag(mappedSymbols) , "filled");##axis([-2 2 -2 2]);##title("Symbols");##figure(4);##subplot(3,1,1);##stem(real(mappedSymbols));##subplot(3,1,2);##stem(real(mappedSymbols_upsampled));##subplot(3,1,3);##plot(Tx_I);##figure(5);##subplot(3,1,1);##plot(Tx_I);##subplot(3,1,2);##plot(Tx_Q);##subplot(3,1,3);##plot(Tx);####figure(6);##stem(imag(mappedSymbols_upsampled));##figure(7);##plot(Tx_Q_BB);##hold on##stem(Tx_Q_BB);##hold on##stem(1:4:length(Tx_Q_BB), Tx_Q_BB(1:4:end), 'filled');%----------------------------------------------------%----------------- Clear ----------------------------%----------------------------------------------------%clear Tx_I Tx_Q Tx_I_BB Tx_Q_BB clear alpha i dt n%clear mappedSymbols mappedSymbols_upsampled_and_padded mappedSymbols_upsampledclear symbolBits symbolIndex