OFDM transmitter chain viz. binary data source,data mapping,IFFT,CP insertion. This time domain data is passed to the channel and AWGN. The OFDM receiver consists of CP removal,FFT, data demapping and decoding of the same data.
The ofdm matlab code for the above block schematic.
OFDM transmitter in this example consists of FEC encoder, BPSK modulator and 256 point IFFT.
OFDM receiver part consists of FFT, BPSK demodulator and viterbi decoder.
BER curve is also included for this OFDM matlab model and this is taken after passing the transmitter data from AWGN channel.
MATLAB OFDM Transmitter Code
clc; clear all; close all; %.............................................................. % Initiation %.............................................................. no_of_data_bits = 64%Number of bits per channel extended to 128 M =4 %Number of subcarrier channel n=256;%Total number of bits to be transmitted at the transmitter block_size = 16; %Size of each OFDM block to add cyclic prefix cp_len = floor(0.1 * block_size); %Length of the cyclic prefix %............................................................ % Transmitter %......................................................... %......................................................... % Source generation and modulation %........................................................ % Generate random data source to be transmitted of length 64 data = randsrc(1, no_of_data_bits, 0:M-1); figure(1),stem(data); grid on; xlabel('Data Points'); ylabel('Amplitude') title('Original Data ') % Perform QPSK modulation on the input source data qpsk_modulated_data = pskmod(data, M); figure(2),stem(qpsk_modulated_data);title('QPSK Modulation ') %............................................................ %............................................................. % Converting the series data stream into four parallel data stream to form % four sub carriers S2P = reshape(qpsk_modulated_data, no_of_data_bits/M,M) Sub_carrier1 = S2P(:,1) Sub_carrier2 = S2P(:,2) Sub_carrier3 = S2P(:,3) Sub_carrier4 = S2P(:,4) figure(3), subplot(4,1,1),stem(Sub_carrier1),title('Subcarrier1'),grid on; subplot(4,1,2),stem(Sub_carrier2),title('Subcarrier2'),grid on; subplot(4,1,3),stem(Sub_carrier3),title('Subcarrier3'),grid on; subplot(4,1,4),stem(Sub_carrier4),title('Subcarrier4'),grid on; %.................................................................. %.................................................................. % IFFT OF FOUR SUB_CARRIERS %................................................................. %.............................................................. number_of_subcarriers=4; cp_start=block_size-cp_len; ifft_Subcarrier1 = ifft(Sub_carrier1) ifft_Subcarrier2 = ifft(Sub_carrier2) ifft_Subcarrier3 = ifft(Sub_carrier3) ifft_Subcarrier4 = ifft(Sub_carrier4) figure(4), subplot(4,1,1),plot(real(ifft_Subcarrier1),'r'), title('IFFT on all the sub-carriers') subplot(4,1,2),plot(real(ifft_Subcarrier2),'c') subplot(4,1,3),plot(real(ifft_Subcarrier3),'b') subplot(4,1,4),plot(real(ifft_Subcarrier4),'g') %........................................................... %........................................................... % ADD-CYCLIC PREFIX %.......................................................... %............................................................ for i=1:number_of_subcarriers, ifft_Subcarrier(:,i) = ifft((S2P(:,i)),16)% 16 is the ifft point for j=1:cp_len, cyclic_prefix(j,i) = ifft_Subcarrier(j+cp_start,i) end Append_prefix(:,i) = vertcat( cyclic_prefix(:,i), ifft_Subcarrier(:,i)) % Appends prefix to each subcarriers end A1=Append_prefix(:,1); A2=Append_prefix(:,2); A3=Append_prefix(:,3); A4=Append_prefix(:,4); figure(5), subplot(4,1,1),plot(real(A1),'r'),title('Cyclic prefix added to all the sub-carriers') subplot(4,1,2),plot(real(A2),'c') subplot(4,1,3),plot(real(A3),'b') subplot(4,1,4),plot(real(A4),'g') figure(11),plot((real(A1)),'r'),title('Orthogonality'),hold on ,plot((real(A2)),'c'),hold on , plot((real(A3)),'b'),hold on ,plot((real(A4)),'g'),hold on ,grid on %Convert to serial stream for transmission [rows_Append_prefix cols_Append_prefix]=size(Append_prefix) len_ofdm_data = rows_Append_prefix*cols_Append_prefix % OFDM signal to be transmitted ofdm_signal = reshape(Append_prefix, 1, len_ofdm_data); figure(6),plot(real(ofdm_signal)); xlabel('Time'); ylabel('Amplitude'); title('OFDM Signal');grid on; %...............................................................
Time Domain Data through Channel and AWGN
%............................................................. channel = randn(1,2) + sqrt(-1)*randn(1,2); after_channel = filter(channel, 1, ofdm_signal); awgn_noise = awgn(zeros(1,length(after_channel)),0); recvd_signal = awgn_noise+after_channel; % With AWGN noise figure(7),plot(real(recvd_signal)),xlabel('Time'); ylabel('Amplitude'); title('OFDM Signal after passing through channel');grid on; %...........................................................
MATLAB OFDM Receiver Code
%.......................................................... recvd_signal_paralleled = reshape(recvd_signal,rows_Append_prefix, cols_Append_prefix); %........................................................ %........................................................ % Remove cyclic Prefix %....................................................... %...................................................... recvd_signal_paralleled(1:cp_len,:)=[]; R1=recvd_signal_paralleled(:,1); R2=recvd_signal_paralleled(:,2); R3=recvd_signal_paralleled(:,3); R4=recvd_signal_paralleled(:,4); figure(8),plot((imag(R1)),'r'),subplot(4,1,1),plot(real(R1),'r'), title('Cyclic prefix removed from the four sub-carriers') subplot(4,1,2),plot(real(R2),'c') subplot(4,1,3),plot(real(R3),'b') subplot(4,1,4),plot(real(R4),'g') %................................................... %................................................... % FFT Of recievied signal for i=1:number_of_subcarriers, % FFT fft_data(:,i) = fft(recvd_signal_paralleled(:,i),16); end F1=fft_data(:,1); F2=fft_data(:,2); F3=fft_data(:,3); F4=fft_data(:,4); figure(9), subplot(4,1,1),plot(real(F1),'r'),title('FFT of all the four sub-carriers') subplot(4,1,2),plot(real(F2),'c') subplot(4,1,3),plot(real(F3),'b') subplot(4,1,4),plot(real(F4),'g') %................................ %.............................. % Signal Reconstructed %.................................. %.................................. % Conversion to serial and demodulationa recvd_serial_data = reshape(fft_data, 1,(16*4)); qpsk_demodulated_data = pskdemod(recvd_serial_data,4); figure(10) stem(data) hold on stem(qpsk_demodulated_data,'rx'); grid on;xlabel('Data Points');ylabel('Amplitude'); title('Recieved Signal with error')
MATLAB BER Code
close all; clear all; clc; n1=1; n2=14; FFT_SIZE=256; %CP=16; %% Data Generator Data_gen = randint(1,11,255) D=dec2bin(Data_gen); s=0; %% BER PARAMETERS EbNo=0:1:15; BER = zeros(1,length(EbNo)); numPackets=15; frmLen = 1000; for idx = 1: length(EbNo) for packetidx = 1 : numPackets %% Convolution Encoder conv_in=[]; for index =1:11 conv_in=[conv_in double(D(index,:))-48]; end conv_in=[conv_in 0 0 0 0 0 0 0 0]; %%8 bits padding DIN=conv_in; trel = poly2trellis(7, [171 133]); % Define trellis. code = convenc(conv_in,trel); clear conv_in; inter_out=code; %% BPSK Data Mapping mapper_out=mapping(inter_out',1,1); clear inter_out; %D=mapper_out; ifft_in=[0,mapper_out(1:96),zeros(1,32),zeros(1,31),mapper_out(97:192)] tx_data=ifft(ifft_in); clear ifft_in; %rx_data=awgn(tx_data,10,'measured'); %rx_data=awgn(tx_data,10,'measured'); %rx_data=awgn(tx_data,2,'measured'); %rx_data=awgn(tx_data,1,'measured'); rx_data = awgn(tx_data./sqrt(16), EbNo(idx) , 'measured'); rx_data = awgn(rx_data./sqrt(16), EbNo(idx) , 'measured'); rx_data = awgn(rx_data./sqrt(16), EbNo(idx) , 'measured'); rx_data=fft(rx_data); clear tx_data; rx_data1=[rx_data(1,2:97) rx_data(1,161:256)]; % taking out symbols for demapping % rx_data1=RECON; Demap_out=demapper(rx_data1,1,1); %%viterbi decoder vit_out=vitdec(Demap_out,trel,7,'trunc','hard'); clear Demap_out; DOUT=vit_out; %figure;plot(DOUT-DIN); [number,ratio] = biterr(DIN,vit_out); error(packetidx) = biterr(DIN,vit_out); end % End of for loop for numPackets BER21(idx) = sum(error)/(log2(4)*numPackets*frmLen); end h=gcf;clf(h); grid on; hold on; set(gca,'yscale','log','xlim',[EbNo(1), EbNo(end)],'ylim',[0 1]); xlabel('Eb/No (dB)'); ylabel('BER'); set(h,'NumberTitle','off'); set(h,'Name','BER Results'); set(h, 'renderer', 'zbuffer'); title('OFDM alone BER PLOTS'); semilogy(EbNo(1:end),BER21(1:end),'b-*'); %error=biterr(DOUT,DIN); %Grouping Bits and converting to Dec for RS Decoder
MATLAB MAPPING Function
function [map_out]=wimax_mapping(data,mode,fact) input_seq = data; switch mode case 1 b=fact*[1 -1]; case 2 b=fact*[1+1i -1+1i 1-1i -1-1i]; case 4 b=fact*[1+1i 1+3i 1-1i 1-3i 3+1i 3+3i 3-1i 3-3i -1+1i -1+3i -1-1i -1-3i -3+1i -3+3i -3-1i -3-3i]; case 6 b=fact*[3+3i 3+1i 3+5i 3+7i 3-3i 3-1i 3-5i 3-7i 1+3i 1+1i 1+5i 1+7i 1-3i 1-1i 1-5i 1-7i 5+3i 5+1i 5+5i 5+7i 5-3i 5-1i 5-5i 5-7i 7+3i 7+1i 7+5i 7+7i 7-3i 7-1i 7-5i 7-7i -3+3i -3+1i -3+5i -3+7i -3-3i -3-1i -3-5i -3-7i -1+3i -1+1i -1+5i -1+7i -1-3i -1-1i -1-5i -1-7i -5+3i -5+1i -5+5i -5+7i -5-3i -5-1i -5-5i -5-7i -7+3i -7+1i -7+5i -7+7i -7-3i -7-1i -7-5i -7-7i]; otherwise error('wrong choice'); end count=1; count1=1; for i=1:(ceil(length(input_seq)/mode)) temp=0; for j=1:mode temp=bitor(temp,bitshift(input_seq(count),(j-1))); count=count+1; if(count>length(input_seq)) break; end end map_out(count1)=b(temp+1); count1=count1+1; end
MATLAB Demapper Function
function [map_out]=wimax_demapper(data,mode,c) m_out=data; % mode=input('Enter 1 for BPSK 2 for QPSK 3 for 16QAM 4 for 64QAM '); % c=1; % m_out=[1 -1 1 -1 1 1 -1 1 -1 1 1 1 -1 1 1 1 1 1 1 1 -1 1 1 1 -1 -1 1 1 1 -1 -1 1 -1 -1 -1 -1 -1 -1 -1 1 1 -1 1 -1 1 1 -1 -1 1 1 1 -1 1 1 -1 1 -1 -1 -1 1 1 1 -1 -1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 -1 1 -1 -1 1 1 1 1 1 1 -1 1 -1 -1 -1 -1 -1 1 1 1 -1 1 1 1 -1 1 -1 -1 1 -1 1 -1 -1 -1 -1 1 -1 1 -1 1 1 1 -1 -1 -1 1 -1 -1 -1 1 -1 -1 -1 -1 -1 1 1 -1 1 1 -1 1 1 -1 1 -1 1 -1 -1 1 -1 -1 1 -1 1 -1 1 -1 1 -1 1 1 1 -1 -1 -1 1 1 -1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 1 1 1 -1 -1 -1 -1 1 -1 -1]%mapper_pilot_binary(mode,c); switch mode case 1 b=c*[1 -1]; case 2 b=c*[1+1i -1+1i 1-1i -1-1i]; case 4 b=c*[1+1i 1+3i 1-1i 1-3i 3+1i 3+3i 3-1i 3-3i -1+1i -1+3i -1-1i -1-3i -3+1i -3+3i -3-1i -3-3i]; case 6 b=c*[3+3i 3+1i 3+5i 3+7i 3-3i 3-1i 3-5i 3-7i 1+3i 1+1i 1+5i 1+7i 1-3i 1-1i 1-5i 1-7i 5+3i 5+1i 5+5i 5+7i 5-3i 5-1i 5-5i 5-7i 7+3i 7+1i 7+5i 7+7i 7-3i 7-1i 7-5i 7-7i -3+3i -3+1i -3+5i -3+7i -3-3i -3-1i -3-5i -3-7i -1+3i -1+1i -1+5i -1+7i -1-3i -1-1i -1-5i -1-7i -5+3i -5+1i -5+5i -5+7i -5-3i -5-1i -5-5i -5-7i -7+3i -7+1i -7+5i -7+7i -7-3i -7-1i -7-5i -7-7i]; otherwise error('wrong choice'); end count=1; for k=1:length(m_out) z=find( (abs(b-m_out(k))).^2-min(abs((b-m_out(k))).^2)==0); if length(z)==1 temp(k)=z-1; else temp(k)=z(randint(1,1,[1,length(z)]))-1; end end dmod_out = de2bi(temp,mode); map_out = reshape(dmod_out',1,mode*192);