My ber values are 0.2509 for snr=3 and ber=0.2537 for snr=4... which are wrong values.. As snr increases, ber shld decrease.. Kindly help me in rectifying the problem in my code. I have taken no. of bits = 10^5 and no of samples per bit as 20... My matlab code is as follows:
clc;
close all;
clear all;
no_of_bits=10000;
no_of_samples=20;
threshold = 0.5;
snr=[-3:1:20];
A1=5; % Amplitude
Tb = 1; % Bit duration
fc = 3/Tb; % Carrier frequency
t1=linspace(0, 1, no_of_samples);
t2=t1/fc;
rand('state',123);
w=cos(2*pi*fc*t2);
for i=1:10;
ber(i)=0;
end
for k=1:length(snr)
sn(k)=snr(k)+6;
err(k)=0;
A = rand(1,no_of_bits); % Generate a random sequence
for i=1:no_of_bits
if (A(i)>=threshold)
A(i)=1;
else
A(i)=0;
end
end
d = A(1:no_of_bits); % Binary sequence
for j=1:length(d)
if (d(j)==1)
askmod=A1.*cos(2*pi*fc*t2);
else
askmod=0.*cos(2*pi*fc*t2);
end
askmod;
noise = (10^(-sn(k)/20))*randn(1,no_of_samples);
received = askmod + noise;
v=received.*cos(2*pi*fc*t2);
z=trapz(v);
baskout=[];
if (z<0)
baskout(j)=0;
else
baskout(j)=1;
end
baskout;
if (d(j)~=baskout(j))
err(k)=err(k)+1;
end
end
ber(k)=err(k)/(no_of_bits)
sn(k)
end
% plot
close all
figure
semilogy(sn,ber);
axis([3 26 10^-5 10^0]);
grid on;
xlabel('snr');
ylabel('Bit Error Rate');
title('Bit error probability curve for BASK modulation');