clear;
clc;
avg_gama_b = [0 5 10 15 20 25 30 ];
limit = [1e2 1e3 1e3 1e4 1e4 1e4 1e5 ];
power_dist = 1;
power_dist_pmt = sqrt ( power_dist ./2 );
omega = sum(power_dist);
omega_db = 10*log10(omega);
eb_by_n0 = avg_gama_b - omega_db;
sim_point = length(eb_by_n0);
ser = zeros(1, sim_point); %vector of BER's
for j = 1:sim_point
sym_error = 0;
for o=1:limit(j)
b = rand();
if b > 0.5, b = 1; else b = -1; end
s = b;
signal_power = 10*log10(sum( s .^ 2 ));
noise_power = power(10, (signal_power - eb_by_n0(j))/10);
sigma_n = sqrt(noise_power);
n = (sigma_n/sqrt(2)) * (randn() + 1i * randn());
alpha = power_dist_pmt .* (randn() + 1i * randn());
% alpha = abs(alpha);
% n = sigma_n * randn();
r = n + alpha*s;
if real(r) > 0, b_hat = 1; else b_hat = -1; end
if b ~= b_hat
sym_error = sym_error +1;
end
end
ser(j) = sym_error / limit(j);
end
ser_matlab = berfading(eb_by_n0, 'psk', 2, 1);
figure, semilogy( avg_gama_b, ser,'-sb', eb_by_n0, ser_matlab,'-or'), xlabel('average SNR'), ylabel('Error Probability'),
title(['BPSK in Rayleigh'] );
grid on;
hleg1 = legend('my BPSK BER', 'MATLAB BPSK BER');
set(hleg1,'Location','SouthWest');
set(hleg1,'Interpreter','none');