N=1000000;
snr=0:1:25;
for m=1:size(snr,2)
efade = 0;
egaus = 0;
No=10^(-snr(m)/10); % PSD of AWGN
sigma= sqrt(No/2); % Standard deviation
for n=1:N
s=(-1)^round(rand);
x=randn/sqrt(2);
y=randn/sqrt(2);
p=x^2 + y^2; % Fading Power
a=sqrt(p); % Fading Amplitude
r=a*s + sigma*randn; % faded signal + AWGN
rg = s + sigma*randn; % unfaded signal + AWGN
if (r>0)
shat=+1;
else
shat=-1;
end
if (rg>0)
sh=+1;
else
sh=-1;
end
if (shat ~= s)
efade = efade + 1;
end
if (sh ~= s)
egaus = egaus + 1;
end
end
pe(m)=efade/N; % BER flat Rayleigh fading
peg(m)=egaus/N; % BER AWGN only
fprintf('%f \t %e\n',snr(m),pe(m));
end
semilogy(snr,pe,'-')
hold on
semilogy(snr,peg,':')
legend('Rayleigh channel','AWGN channel');
ylabel('Bit Error Rate');
xlabel('Average E_b/N_0 (dB)');
title('Perfomance of BPSK over flat-Rayleigh and AWGN channels');
axis([ snr(1) snr(end) 9.5e-6 1])