Re: AF Cooperative Diversity MATLAB Code
Here is the correct code:
clear all; close all; clc;
N=10^6;%Number of bits
b=rand(1,N)>0.5;%Generate 0s and 1s
Tx=2.*b-1;%NRZ
SNRdB=0:2:30;%SNR in dB
snr = 10.^(0.1 .* SNRdB);
nErr = zeros(1,length(SNRdB));
for ii=1:length(SNRdB)
sd = sqrt(1/(snr(ii)));
h0=(1/(sqrt(2))).*(randn(1,N)+1i.*randn(1,N));%the channel gain of the direct path.
h1=(1/(sqrt(2))).*(randn(1,N)+1i.*randn(1,N));%the channel gain of the first hop in the relay path.
h2=(1/(sqrt(2))).*(randn(1,N)+1i.*randn(1,N));%the channel gain of the second hop in the relay path.
n0=(1/(sqrt(2))).*(randn(1,N)+1i.*randn(1,N));%AWGN in direct path.
n1=(1/(sqrt(2))).*(randn(1,N)+1i.*randn(1,N));%AWGN in the first hop.
n2=(1/(sqrt(2))).*(randn(1,N)+1i.*randn(1,N));%AWGN in the second hop.
% B=1./abs(h1);%Amplification Gain
B=sqrt(snr(ii) ./ ((snr(ii) .* abs(h1).^2) + 1));
y0=(h0.*Tx)+(sd).*n0;%the received signal in the direct path.
y1=(h1.*Tx)+(sd).*n1;%the received signal in the first hop.
yAmp=B.*y1;%Amplification of y1
y2=h2.*yAmp+(sd).*n2;%received signal in the second hop
y=(conj(h0)./(sd)).*y0+((conj(h1).*conj(h2).*conj(B))./((abs(h2).^2+abs(B).^2+1).*(sd))).*y2;
bHat=real
>0.5;%The estimated bit sequence
nErr(ii)=size(find(bHat-b),2);%Extract the number of erros between b and bHat
end
BER=nErr./N;%Average BER
semilogy(SNRdB,BER);
grid on