% Script for computing the Bit Error probability using OFDM modulation
clc;
clear all;
close all;
N = 512; % fft size
Nsd = 384; % number of data subcarriers
nBitPerSym = 428; % number of bits per OFDM symbol (same as the number of subcarriers for BPSK)
nSym = 100; % number of symbols
Nsp = 48 ; %Number of pilot subcarriers 4
ofdmBW = 5* 10^6 ; % OFDM bandwidth
deltaF = ofdmBW/N; %=20 MHz/64 = 0.3125 MHz
Tfft = 1/deltaF; % IFFT/FFT period = 3.2us
Tgi = Tfft/4;%Guard interval duration - duration of cyclic prefix
Tsignal = Tgi+Tfft; %duration of BPSK-OFDM symbol
Ncp = N*Tgi/Tfft; %Number of symbols allocated to cyclic prefix
Nst = 428; %Number of total used subcarriers
nBitsPerSym=Nst; %For BPSK the number of Bits per Symbol is same as num of subcarriers
u=1; %fr segment0
v=1; %for segment1
w=1; %for segment2
EbN0dB = [-10:10]; % bit to noise ratio
EsN0dB = EbN0dB + 10*log10(Nsd/N) + 10*log10(N/(Ncp+N));% converting to symbol to noise ratio
for ii = 1:length(EbN0dB)
% Transmitter
ipBit = rand(1,nBitPerSym*nSym) > 0.5; % random 1's and 0's
ipMod = 2*ipBit-1; % BPSK modulation 0 --> -1, 1 --> +1
ipMod = reshape(ipMod,nBitPerSym,nSym).'; % grouping into multiple symbols
% Assigning modulated symbols to subcarriers from [-26 to -1, +1 to +26]
xF = [zeros(nSym,42) ipMod(:,[1:nBitPerSym/2]) zeros(nSym,1) ipMod(:,[nBitPerSym/2+1:nBitPerSym]) zeros(nSym,41)];
s0=zeros(nSym,512);
s1=zeros(nSym,512);
s2=zeros(nSym,512);
sz=zeros(nSym,512);
sx=zeros(nSym,512);
sy=zeros(nSym,512);
%segmentation to three different segmanets namely s0,s1,s2
for s=0:2
for i=0:142
k = 43+s+(3*i);
if(k~=257 && k<472)
if(s==0)
for u=1:nSym
sx(u,k)=xF(u,k);
s0=sx+s0;
end;
else if (s==1)
for v=1:nSym
sy(v,k)=xF(v,k);
s1=sy+s1;
end;
else if (s==2)
for w=1:nSym
sz(w,k)=xF(w,k);
s2=sz+s2;
end;
end;
end;
end;
end;
end;
end;
for g=0:2
% Taking FFT, the term (nFFT/sqrt(nDSC)) is for normalizing the power of transmit symbol to 1
xtg = (N/sqrt(Nsd))*ifft(fftshift(sg.')).';
% Appending cylic prefix
xtg = [xtg(:,[385:512]) xtg];
% Concatenating multiple symbols to form a long vector
xtg = reshape(xtg.',1,nSym*640);
% Gaussian noise of unit variance, 0 mean
ntg = 1/sqrt(2)*[randn(1,nSym*640) + j*randn(1,nSym*640)];
% Adding noise, the term sqrt(80/64) is to account for the wasted energy due to cyclic prefix
ytg = sqrt(640/512)*xtg + 10^(-EsN0dB(ii)/20)*ntg;
% Receiver
ytg = reshape(ytg.',640,nSym).'; % formatting the received vector into symbols
ytg = ytg(:,[129:640]); % removing cyclic prefix
% converting to frequency domain
yFg = (sqrt(Nsd)/N)*fftshift(fft(ytg.')).';
yMod = yFg(:,[42+[1:nBitPerSym/2] 43+[nBitPerSym/2+1:nBitPerSym] ]);
% BPSK demodulation
% +ve value --> 1, -ve value --> -1
ipModHat = 2*floor(real(yMod/2)) + 1;
ipModHat(find(ipModHat>1)) = +1;
ipModHat(find(ipModHat<-1)) = -1;
% converting modulated values into bits
ipBitHat = (ipModHat+1)/2;
ipBitHat = reshape(ipBitHat.',nBitPerSym*nSym,1).';
% counting the errors
nErr(ii) = size(find(ipBitHat - ipBit),2);
end
simBer = nErr/(nSym*nBitPerSym)
%theoryBer = (1/2)*erfc(sqrt(10.^(EbN0dB/10)))
%figure(1);
%semilogy(EbN0dB,theoryBer);
%hold on
figure(1);
semilogy(EbN0dB,simBer);
xlabel('Eb/No, dB')
ylabel('Bit Error Rate')
title('Bit error probability curve for BPSK using OFDM')
end;