%================================
% This is the simulator for OFDM.
%================================
function SER = ofdm(SP)
numSymbols = SP.FFTsize; % The size of the data block is equal to the FFT size.
% Frequency domain version of the channel response.
H_channel = fft(SP.channel,SP.FFTsize);
for n = 1:length(SP.SNR),
errCount = 0; % Initialize the error count.
for k = 1:SP.numRun,
% Generate random data block.
tmp = round(rand(2,numSymbols));
tmp = tmp*2 - 1;
inputSymbols = (tmp(1,:) + 1i*tmp(2,:))/sqrt(2);%QPSK
% Perform OFDM modulation using IFFT.
TxSamples = sqrt(SP.FFTsize)*ifft(inputSymbols);
% Add CP.
ofdmSymbol = [TxSamples(numSymbols-SP.CPsize+1:numSymbols) TxSamples];
% Propagate through multi-path channel.
RxSamples = filter(SP.channel, 1, ofdmSymbol);
% Generate AWGN with appropriate noise power.
tmp = randn(2, numSymbols+SP.CPsize);
complexNoise = (tmp(1,:) + 1i*tmp(2,:))/sqrt(2);
noisePower = 10^(-SP.SNR(n)/10);
% Add AWGN to the transmitted signal.
RxSamples = RxSamples + sqrt(noisePower)*complexNoise;
% Remove CP.
EstSymbols = RxSamples(SP.CPsize+1:numSymbols+SP.CPsize);
% Convert the received signal into frequency domain.
Y = fft(EstSymbols, SP.FFTsize);
% Perform channel equalization in the frequency domain.
if SP.equalizerType == 'ZERO'
Y = Y./H_channel;
elseif SP.equalizerType == 'MMSE'
C = conj(H_channel)./(conj(H_channel).*H_channel+ 10^(-SP.SNR(n)/10));
Y = Y.*C;
end
% Perform hard decision detection.
EstSymbols = Y;
EstSymbols = sign(real(EstSymbols))+ 1i*sign(imag(EstSymbols));
EstSymbols = EstSymbols/sqrt(2);
% Check whether there is error.
I = find((inputSymbols-EstSymbols) == 0);
% Count the number of errors.
errCount = errCount + (numSymbols-length(I));
end
% Calculate the symbol error rate (SER).
SER(n,:) = errCount / (numSymbols*SP.numRun);
end