% STBC-OFDM simulation
% we assumed no ISI, i.e cyclic prefix is enough lenght and the fade is constant
% accross the whole OFDM symbol.
% the simulator operate on the output of the FFT (receiver) and
% introudes the effect of FFT on the FSC (i.e correlation)
% programmer: Samir Alghadhban
%
clear all;
format short e;
% Simualtion setup Start
disp('this simulation uses Alamouti STBC for each group with two transmit antennas')
disp('with SGINC algorithm with FN ordering after nulling')
disp('choose either:')
disp('1- Frame Error Rate simulation')
disp('2- Bit Error Rate simulation')
disp('3- OFDM Symbol Error Rate')
disp('4- Symbol Error Rate')
disp('5- FER per subcarrier')
disp('6- GER ')
disp('7- GER per subcarrier')
SimType=input('Input your selection >> ');
Nc=input('Input FFT size (OFDM symbol length) power of 2 >>');
L=input('Input number of paths in the FSC >> ');
% disp('Select the ordering method')
% disp('1- No Ordering')
% disp('2- Frobenius Norm ordering before nulling')
% disp('3- Frobenius Norm ordering After nulling, best performance')
% ordering=input('Input your selection >> ');
Trail=input('Input max number of errors counted in the simulation >> ');
NumOfFrames=input('Input minumum number of frames >> ');
K=1;%input('Input number of groups >> ');
MRx=input('Input number of receive antennas >> ');
SNR=input('Input SNR range in dB >> ');
SNRLength=length(SNR);
% NTx=2; % Number of transmit antennas
% T=2; % STBC Length
disp('Choose STBC Code')
disp('1- AlAmouti Code')
disp('2- G4 1/2 rate Orthogonal Code')
STBCCode=input('Input your selection >> ');
% % % %
% STBC Code
switch STBCCode
case 1
NTx=2;
T=2;
case 2
NTx=4;
T=8;
end
disp('choose modulation type')
disp('1- QPSK')
disp('2- 8QAM')
disp('3- 16QAM')
disp('4- 64QAM')
disp('5- 256QAM')
scheme=input('Input your selection >> ');
% Initializing
BER=zeros(1,SNRLength);
MSNR=zeros(1,SNRLength);
HV=zeros(T*MRx,NTx*K,Nc);
AWGN=zeros(T*MRx,Nc);
Y=zeros(T*MRx,Nc);
X=zeros(NTx*K,Nc);
XInd=zeros(NTx*K,Nc);
% Modulation Type
[SignalSet,BitMapping,SQAM,ES]=choosesignalset(scheme);
qset=length(SignalSet);
% % % %
% SNR Loop
for jj=1:SNRLength
ER=zeros(NTx*K,Nc); %% Initilize Error
SP=0; %% Initilize Signal Power
NP=0; %% Initilize Noise Power
%SymError=0;
Bits=0;
Frame=0;
Error=0;
RV=0;
Sigma2=ES/2*10^(-SNR(jj)/10); %% noise variance per dimension
while ((Error<Trail)|((Frame<NumOfFrames)&(Error>=Trail)))
% Multi-group Modulator
XInd=ceil(qset*rand(NTx*K,Nc)); % transmitted signal's index from all the groups
X=SignalSet(XInd); % Map to signal set
% Frame or Bit counter
Frame=Frame+1;
Bits=Bits+BitsCalc(NTx,K,Nc,SimType,qset);
% Multi-group MIMO Channel generation for the transmission of two periods
H=MIMOOFDMChannel(K,MRx,NTx,Nc,L); % MIMO OFDM channel after FFT H(MRx,NTx,Nc)
switch STBCCode
case 1
HV=VirtualMIMOAlamouti(K,MRx,NTx,Nc,H); % virtual MIMO for Alamouti code
case 2
HV=VirtualMIMOG4(T,MRx,NTx,Nc,H); % virtual MIMO for G4 code
end
AWGN=sqrt(Sigma2)*randn(T*MRx,Nc)+i*sqrt(Sigma2)*randn(T*MRx,Nc);
% % % %
% Received Signal after FFT
% Iterate for each subcarrier
for nc=1:Nc
Y(:,nc)=HV(:,:,nc)*X(:,nc)+AWGN(:,nc);
SP=SP+real(trace((HV(:,:,nc)*X(:,nc))'*(HV(:,:,nc)*X(:,nc)))/(MRx*T)); %% Signal Power per Receive Antenna
NP=NP+real(AWGN(:,nc)'*AWGN(:,nc)/(MRx*T)); %% Noise Power per Receive Antenna
ER(:,nc)=STBC_Detector(Y(:,nc),HV(:,:,nc),SignalSet,XInd(:,nc),BitMapping,NTx,MRx,K,STBCCode);
end % end subcarrier loop
% count errors
Error=Error+ERCalc(ER,SimType,K,NTx,Nc);
% Fade and Noise Power Measurements
% % % %
% SGINC Algorithm and error counter
end % While Loop
BER(jj)=Error/Bits
SNR(jj)
MSNR(jj)=10*log10(SP/NP)
end % SNR Loop
figure;
semilogy(SNR,BER);
title(sprintf('STBC-OFDM at NTx=%2.0f M_R=%2.0f L=%2.0f at %2.0fbps/Hz',[NTx MRx L K*log2(qset)]));
xlabel('Es/N0');
switch SimType
case 1
ylabel('FER');
case 2
ylabel('BER');
case 3
ylabel('OFDM SER');
case 4
ylabel('SER');
case 5
ylabel('FER per subcarrier');
case 6
ylabel('GER');
case 7
ylabel('GER per subcarrier');
end