MATLAB Answers

0

Please rectify the error in the code

Asked by Supratik Das on 14 Jun 2019
Latest activity Commented on by Star Strider
on 14 Jun 2019
clc;
clear all;
close all;
SNR=[0:1:14];
snr=10.^(SNR/10);
BER1=zeros(length(SNR),1);
BER2=BER1;
BER3=BER1;
Nmax=1000;
Nerr=100;
for k=1:length(SNR)
for l=1:Nmax
Ns=100;
data=2*round(rand(Ns,1))-1;
if l==1 && k==1
plot(data);title('data');axis([0 Ns -1.1 1.1]);
pause
end
bpsk=data;
n=1/sqrt(2)*(randn(Ns,1)+j*randn(Ns,1));
if l==1 && k==1
var_n=norm(n)^2;
end
bpsk=sqrt(snr(k))*data+n;
if l==1 && k==1
plot([real(bpsk) data]);
legend('real part of signal','data');
title('BPSK signal in noise');
pause
end
taps=1/sqrt(2)*(randn(Ns,1)+j*randn(Ns,1));
bpsk_r=sqrt(snr(k))*abs(taps).*data+n;
if l==1 && k==1
plot([real(bpsk_r) data])
legend('real part of signal','data');
title('BPSK signal in noise & fading channel');
pause
end
if l==1 && k==1
plot(abs([bpsk bpsk_r]))
legend('AWGN','RAYLEIGH');
title('BPSK in AWGN & Rayleigh fading channel');
pause
end
r1=real(bpsk);
r2=real(bpsk_r);
if l==1 && k==1
plot([r1 r2])
legend('AWGN','Rayleigh');
title('demodulated symbols');
pause;
end
d1=find(r1>=0);d2=find(r1<0);
r1(d1)=1;r1(d2)=-1;
d1=find(r2>=0);d2=find(r2<0);
r2(d1)=1;r2(d2)=-1;
if l==1 && k==1
plot([r1 r2])
legend('AWGN','Rayleigh');
axis([0 Ns -1.1 1.1]);
title('demodulated symbols after hard decisions');
pause;
end
Ber1=length(find((data-r1)~=0));
Ber2=length(find((data-r2)~=0));
if k==1 && l==1
errors=[Ber1 Ber2];
end
BER1(k)=BER1(k)+Ber1;
BER2(k)=BER2(k)+Ber2;
if BER1(k)>Nerr && BER2(k)>Nerr
break
end
end
BER1(k)=BER1(k)/Ns/l;
BER2(k)=BER2(k)/Ns/l;
end
BER=[SNR BER1(k) BER2(k)];
The_awgn=.5*erfc(sqrt(2*snr)/sqrt(2));
The_rayl=.5*(1-sqrt(snr./(1+snr)));
semilogy(SNR,[The_awgn The_rayl BER1(k) BER2(k)]);
xlabel('SNR [dB]');
ylabel('BER');
axis([0 SNR(length(SNR)) 1e-4 .5]);
grid on;
legend('Theor AWGN','Theor Rayl.','AWGN','Rayl.');

  2 Comments

at the end their is an error in using semilogy
"Vectors must be of same length"

Sign in to comment.

1 Answer

Answer by Star Strider
on 14 Jun 2019
 Accepted Answer

Try this instead:
semilogy(SNR,[The_awgn; The_rayl; BER1'; BER2']);

  4 Comments

Show 1 older comment
As always, my pleasure.
Sure.
You were trying to plot a matrix of two row vectors and two scalars as a function of a row vector. My changes created your dependent variables as a (4x15) matrix by changing them all to row vectors (eliminating the subscripts of the last two and transposing them) and then concatenating them.
Thank you for your explanation...it helped me a lot
As always, my pleasure.

Sign in to comment.