N=10^6;%Number of symbols
b=rand(1,N)>0.5;%Generate Equiprobable Binary Sequence
s=2*b-1;%Convert to Polar: 1->1 and 0->-1
SNRdB=[0:20];%Signal-to-Noise Ratio in dB
for ii=1:length(SNRdB)
n=1/sqrt(2)*(randn(3,N)+j*randn(3,N));%Generate Noise with Zero-Mean and Unity Variance
h=1/sqrt(2)*(randn(3,N)+j*randn(3,N));%Complex Gaussian Random Variable with zero mean, i.e.:|h| is Rayleigh
y=zeros(2,N);%The Received Matrix at R and D to be Equilized
hEq=zeros(2,N);%The Equilization Matrix
temp=h(1,:).*s+10^(-SNRdB(ii)/20)*n(1,:);%The Received Signal at the First Hop
y(1,:)=h(3,:).*s+10^(-SNRdB(ii)/20)*n(3,:);%The Received Signal at the Direct Path
y(2,:)=h(2,:).*(temp./h(1,:))+10^(-SNRdB(ii)/20)*n(2,:);%The Received Signal at the Second Hop
hEq(1,:)=conj(h(3,:));
hEq(2,:)=conj(h(2,:));
hPower=sum(hEq.*conj(hEq),1);%Channel Power Matrix
yHat=sum(hEq.*y,1)./hPower;
bHat=real(yHat)>0;% The Estimated Binary Sequence
diff=xor(b,bHat);%Compute The XORing Between the Estimated and Origina Binary Sequences
nErr(ii)=length(find(diff));%Find the Number of Errors: the Locations of 1s.
end
BERSim=nErr/N;
BERTh=[0.160288
0.136645
0.114156
0.0933272
0.0745812
0.0582117
0.0443581
0.0330013
0.0239833
0.0170426
0.0118588
0.00809498
0.00543227
0.00359194
0.00234568
0.00151628
0.000972213
0.00061947
0.000392865
0.000248311
0.000156579]';
close all
figure
semilogy(SNRdB,BERSim,'bp-','LineWidth',2);
hold on
semilogy(SNRdB,BERTh,'ks-','LineWidth',2);
axis([0 20 10^-5 0.5]);
grid on
legend('Simulation','Theoretical')