MATLAB: Please rectify the error in the code

at end their is a error using semilogy "vectors must be of same length"debug4me

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.');

Best Answer

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