Please rectify the error in the code

1 次查看(过去 30 天)
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 个评论
Supratik Das
Supratik Das 2019-6-14
at the end their is an error in using semilogy
"Vectors must be of same length"

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2019-6-14
Try this instead:
semilogy(SNR,[The_awgn; The_rayl; BER1'; BER2']);
  4 个评论
Supratik Das
Supratik Das 2019-6-14
Thank you for your explanation...it helped me a lot

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Visualization and Data Export 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by