clc
clear all
close all
%% Modulator
HModulator = comm.BPSKModulator ;
%% Demodulator
HDemodulator= comm.BPSKDemodulator ;
%% Error rate
Error_rate  = comm.ErrorRate;
%% data
data = randi([0 1] , 10000 , 1 ) ;
%% Counter
k = 1 ;
%% %%%%%%%%% For Loop %%%%%%%%%%
for EbN0 = 0 : 10
    %% AWGN
    HAWGNChannel = comm.AWGNChannel('NoiseMethod' , 'Signal to noise ratio (Eb/No)'  , 'EbNo' , EbN0 ) ;
    %% Modulation
    modData = step(HModulator , data) ;
    %% AWGN channel
    noisyData = step(HAWGNChannel,modData);
    %% DeModulation
    rwData = step(HDemodulator , noisyData) ;
    %% Error
    z = Error_rate(rwData, data);
    BER(k) = sum(rwData~=data)  /  length(data) ;
    EbN0_BER (k) = EbN0 ;
    k = k + 1 ;
end
%% plot : noisyData
plot( real(noisyData),imag(noisyData) , 'r*');
hold on
grid on
plot([-1 1],[0 0],'ko','MarkerSize',14,'MarkerFaceColor','k')
%% Plot : Bit  Error Rate : Simulation
figure ;
semilogy(EbN0_BER , BER ,'ro-' );
grid on
xlabel('EbN0');
ylabel('Bit Error Rate');
%% Plot : Bit  Error Rate : Theory
M = 2 ;
ber_theory = berawgn(EbN0_BER,'psk',M,'diff');
hold on
semilogy(EbN0_BER , ber_theory ,'b*-');
legend('Simulation' , 'Theory')




