MATLAB Answers

Nested loop, How can i plot a BER performance for different dBs using a nested loop

8 views (last 30 days)
Lutfeyara Sweety
Lutfeyara Sweety on 16 Dec 2020
Answered: Shraddha Jain on 24 Dec 2020
How can i plot BER vs E_b/N for different signal to noise ratio? Here is mhy code and i have used nested loop but wouldnt be able to see the plot . I am a new user and help needed in it.
clear; clf
Ldata = 20000; %data length in simulation
Lc = 8; %SPreading factor vs data rate
%generate QPSK modulation symbol
data_sym = 2*round(rand(Ldata,1))-1+1i*(2*round(rand(Ldata,1))-1);
jam_data = 2*round(rand(Ldata,1))-1+1i*(2*round(rand(Ldata,1))-1);
%generating a walsh hadamard codes of length 8
Pcode = [1 -1 1 -1 -1 1 -1 1]' ;
%sPread the code
x_in = kron(data_sym, Pcode);
SIR = [5 8 10 20];
noise = randn(Ldata*Lc,1) + 1i*randn(Ldata*Lc,1);
jam_mod = kron(jam_data,ones(Lc,1));
clear jam_data;
[P,x] = pwelch(x_in,[],[],[4096], Lc,'twoside');
%clear jam_mod;
figure(1)
for j = 1:length(SIR)
for i = 1:10
BER = [];
BER_az = [];
Pj = 2*Lc / (10^(SIR(j)/10));
jammer = sqrt(Pj/2)*jam_mod.*exp(1i*2*pi*0.12*(1:Ldata*Lc)).';
[P,x] = pwelch(jammer+x_in,[],[],[4096], Lc,'twoside');
%clear jam_mod;
EB2N(i) = (i-1);
EB2N_num = 10^(EB2N(i)/10);
Var_n = Lc/(2*EB2N_num); %variance
signois = sqrt(Var_n); %standard deviation
awgnois = signois*noise;
y_out = x_in+awgnois+jammer;
Y_out = reshape(y_out,Lc,Ldata).';
clear y_out awgnois;
z_out = Y_out*Pcode;
%decision based on the sign of the samPles
dec1 = sign(real(z_out))+j*sign(imag(z_out));
%comPare against the original data to calcuate BER
BER = [BER;sum([real(data_sym)~=real(dec1);...
imag(data_sym)~=imag(dec1)])/(2*Ldata)];
BER_az = [BER_az;0.5*erfc(sqrt(EB2N_num))];
end
if j==1
figber = semilogy(EB2N,BER_az, 'k-');
hold on;
end
figber = semilogy(EB2N,BER);
clear BER;
clear BER_az;
legend('No jamming','SNR:-5 dB', 'SNR:-8 dB', 'SNR:-10 dB', 'SNR:-20 dB');
set(figber,'Linewidth',2);
xfont=xlabel('E_b/N in dB');
yfont = ylabel('bit error rate');
title('DSSS with sPreading gain 11');
end

Answers (1)

Shraddha Jain
Shraddha Jain on 24 Dec 2020
Hi Lutfeyara,
In order to display individual figure windows, use the figure command just before the semilogy command as,
figure
figber = semilogy(EB2N,BER);

Products

Community Treasure Hunt

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

Start Hunting!