clc;
clear all;
% identify bits for QPSK
BITS=2;
% define SNR range
EbNo=[0:2:30];
%define plotting axis
SNR_axis=[];
BER_axis=[];
%define number of monte-carlo runs
Num=1000;
%define correlation at the receiver
corr=0;
%set initial count
idx=1;
%define alg
alg='ML';
%define modulation
modulation='QPSK';
%define numbers of antennas
M=2;%rx antennas
N=2;%tx antennas
%parameters for wait bar
h = waitbar(0,'Please wait...');
wb=6.25;
%clear BER register
BER=[];
%commence SNR loop
for SNR=EbNo
errors=0;
%define standard deviation, sigma, for noise
sigma=0.5/(sqrt(N)*10^(SNR/10));
for iter=1:Num %commence iteration loop
%define a random Rayleigh channel
H=(randn(M,N)+j*randn(M,N))/sqrt(2);
%exercise correlation option, if any
if corr
R=chol([1 alpha;alpha 1]);%cholesky factor of correlation matrix, i.e. 'R' square root of correlation matrix
H=R*H; % R^0.5 *H -> correlation at receiver
end
H_save=H;%assign H to unalterable value
% modulated input data
tx_bits=randn(N,BITS)>0;
temp1=[];
for i=1:N
d1=tx_modulate(tx_bits(i,:),modulation);
temp1=[temp1; d1];
end
d=temp1;
%AWGN noise
AWGN_noise = sqrt(sigma)*(randn(M, N)+j*randn(M, N));
%receiver signal vector added to AWGN noise
r = (H_save*d)/sqrt(N) + sqrt(sigma)*(randn(M, 1)+j*randn(M, 1));
%ML algorithm
p=form_ref_matrix(BITS); %create file containing 2x2 sets of constellation symbols as reference
temp2=[];
temp3=H*p/sqrt(N);
temp4=16;%square(number of symbols in constellation (4 for QPSK)), since this is a 2x2 system
for i=1:temp4
temp2(:,i)=abs(r-temp3(:,i)).^2;
end
w=sum(temp2);
[y1 x1]=min(w);
a=[p(1,x1); p(2,x1)];
temp2=[];
w=[];
%count errors
errors(iter) = sum((sign(real(a))~=sign(real(d))) | sign(imag(a))~=sign(imag(d)));
end
BER(idx)=sum(errors)/(Num) ; % Calculate BER after completion of 'Num' runs
SER(idx)=BER(idx)*BITS; %calculate symbol error rate
idx=idx + 1; %increment count
waitbar(wb/100);
wb=wb+6.25;%increment wait bar
end %end of SNR loop
close(h);%terminate wait bar
SNR_axis=EbNo;
BER_axis=[BER_axis BER];
SER_axis=SER;
%plot BER
semilogy(SNR_axis,BER_axis,'b-*');
xlabel('SNR [dB]');
ylabel('BER/SER');
title('BER/SER Plots');
hold;
%plot SER
semilogy(SNR_axis,SER_axis,'b-o');
axis([0 30 1e-6 1]);
grid on;
legend('BER','SER');
hold off;
str=['VBLAST System-' '2 x 2 ' alg ' Algorithm with ' modulation ' Modulation'];
set(gcf,'NumberTitle','off');
set(gcf,'Name',str);
grid on