Performance Analysis of BPSK in the presence of AWGN Noise and Rayleigh Fading Channel

x_bits= randi([0 1],1,1000000);   %   Input  bits
hq = modem.pskmod('M', 2, 'PhaseOffset', 0, 'SymbolOrder', 'Gray', 'InputType', 'Bit');
 %Modulation Object for      BPSK
x=modulate(hq, x_bits);                  %    BPSK symbols
LTx=length(x);                            %   length of symbol vector
Eb_No=[0:1:10];                          %   energy per bit talen in (dB) which is a conventional way
LE=length(Eb_No);                    

% Eb/No;                                 %  This line does not make ay sense  makes no sense

%Es_No=Eb_No+10*log10(1/2); appropriate relation is Es/N=(Eb/N)*m where m
                            %is number of bits per symbol tatat depends on
                            %modultion order. For e.g. for BPSK these two
                            %are same , for QPSK or QAM Es/N=(Eb/N)*2. When
                            %we convert in (dB) we may write as:


%   Es_No=Eb_No+10*log10(m);

Es_No=Eb_No;   %  BPSK

% Es_No (dB) = 10 log10(Symbol_POwer/Noise_Power)
%Symbol_Power/Noise_Power=10^(Es_No./10)
%Usually symbol power is normalized i.e. Symbol_Power=1 in M-PSK or others
No=1./(10.^(Es_No./10)); % Noise power or Noise variance (sigma square)

ber=[];
for i=1:LE

 % m+s*randn(1,N) genrates N random numbers with mean as (m) and standard
 % deviation as (s). see matlab help for randn
 % As we lnow white noise have zero mean(m=0) and variance as No (s=sqrt(No);


 % is you use  Es_No=Eb_No+10*log10(m); then appropriate expression shpoul
 % be,   nx=sqrt(No(i)*m)*randn(1,LTx);

nx=sqrt(No(i))*randn(1,LTx);  % For BPSK
% y=x+nx(1:length(x)); it is better to generate noise vector equal to
% symbol vector. Here in this case these are same so we can write:

y=x+nx;

 hq = modem.pskdemod('M', 2, 'PhaseOffset', 0,'SymbolOrder','Gray', 'OutputType', 'Bit','DecisionType', 'hard decision');
 y_bits = demodulate(hq, y);

 x_bits=x_bits(:);
 y_bits=y_bits(:);


  s=0;
       for j=1:length(x_bits)
           if x_bits(j) ~= y_bits(j)
              s=s+1;
           end
       end

br=s/length(x_bits);
ber=[ber br];
end
semilogy(Es_No,ber);



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Rayleigh




ber=[];
for i=1:LE


nx=sqrt(No(i))*randn(1,LTx);  % For BPSK


% rayleigh_chn=raylrnd(1,1,LTx);   %   works in MATLAB 2013 , dontt know
% about others or
rayleigh_chn=abs(randn(1,LTx)+1j*randn(1,LTx));

y=x.*rayleigh_chn+nx;

 hq = modem.pskdemod('M', 2, 'PhaseOffset', 0,'SymbolOrder','Gray', 'OutputType', 'Bit','DecisionType', 'hard decision');
 y_bits = demodulate(hq, y);

 x_bits=x_bits(:);
 y_bits=y_bits(:);


  s=0;
       for j=1:length(x_bits)
           if x_bits(j) ~= y_bits(j)
              s=s+1;
           end
       end

br=s/length(x_bits);
ber=[ber br];
end
hold on;
semilogy(Es_No,ber,'r');