MATLAB: What should I do if I am getting different output every time I run the code in MATLAB code to obtain Bit error rate of BPSK modulation for the given data-[1 0 0 0 0 1 1 1]

bit error ratebpskelectronics and communicationMATLABplease help

%BER of BPSK for given data=[1 0 0 0 0 1 1 1]
clc;
clear all;
close all;
%input data
N=8;
m=[1 0 0 0 0 1 1 1];
x=zeros(1, N); %preallocating variable
for i=1:N
if m(i)==0
x(i)=-1;
else
x(i)=1;
end
end
ber_sim=[]; %empty array for bit error rate
ber_theory_erfc=[]; %empty array for theoretical bit error rate
ber_theory_Q=[]; %empty array for Q equation
ber_chernoff=[]; % BER approximation for upper bound(chernoff bound)
for EbN0dB=0:1:15 %SNR value in dB
EbN0=10^(EbN0dB/10); %convert to normal scale
sigma=sqrt(1/(2*EbN0));
r=x+sigma.*randn(1,N); %AWGN channel with random numbers
m_cap=(r>0); %detected bits, threshold=0
%BER calculation
noe=sum(m~=m_cap); %number of errors, sum if m is not equal to m_cap
ber_sim1=noe/N; %bit error rate calculation with simulation values
ber_sim=[ber_sim ber_sim1];
ber_th_er=0.5*erfc(sqrt(EbN0)); %theoretical bit error rate calculation
ber_theory_erfc=[ber_theory_erfc ber_th_er];
ber_th_q=qfunc(sqrt(2*EbN0)); %bit error rate in terms of Q function
ber_theory_Q=[ber_theory_Q ber_th_q];
ber_cher=0.5*exp(-EbN0); %highest possible bit error rate
ber_chernoff=[ber_chernoff ber_cher];
end
EbN0dB=0:1:15;
semilogy(EbN0dB,ber_sim,'r*-',EbN0dB,ber_theory_erfc,'ko-',EbN0dB,ber_theory_Q,'g+-',EbN0dB,ber_chernoff,' m>-');
xlabel('Eb/N0(dB)');
ylabel('BER');
legend('Simulation','Theory(erfc)','Theory(Q)','Chernoff');
grid on;

Best Answer

Because of the following line (function randn)you are getting different results each time, may be random noise addition
r=x+sigma.*randn(1,N); %AWGN channel with random numbers
%............^
Related Question