You are on page 1of 3

clc;

%% set used SNR values thit lp cc gi tr SNR s dng


SNR=[0:2:14]';%%to vecto theo ct t 0->14 cch nhau 2, l chuyn v
snr=10.^(SNR/10);
%% we creat initial zero vectors for BER tao vecto 0
BER1=zeros(length(SNR),1); %%tao vecto ton 0 c 1 ct, s hng bng s hng
ca SNR
BER2=BER1;BER3=BER1;
%% we need a DS code, we creat a random, complex one, length Nc ( tao ma DS
ngau nhien)
Nc=32;
ds=(2*round(rand(Nc,1))-1)+1j*(2*round(rand(Nc,1))-1);
%%rand l hm to Nc gi tr ngu nhin theo phn b gausse, round l lm trn
plot([real(ds) imag(ds)]); %%v th real l phn thc imag l o
xlabel('thoi gian')
ylabel('bien do')
axis([0 Nc -1.1 1.1]); %%thit lp cc gii hn cho trc x , y axis([xmin xmax
ymin ymax])
title('real and imaginary parts of DS code');%%phn thc vo o ca DS
legend('real','imag'),pause
%% we used symbol energy normalized to 1, thus DS energy is normalized to 1
ds=ds/norm(ds);
%%norm l hm tnh modun
ds_energy=norm(ds)^2;
%monte carlo loop starts here
Nmax=1000;
Nerr=100;
for k=1:length(SNR)
for l=1:Nmax
%% data
Ns=100;
data=2*round(rand(Ns,1))-1;
if l==1 && k==1
figure(2)
plot(data),title('data')
xlabel('thoi gian')
ylabel('bien do')
axis([0 Ns -1.1 1.1]),pause
end
%% modulation
bpsk=data;
DS=kron(data,ds); %% If X is 2-by-3, then kron(X,Y) is
[ X(1,1)*Y X(1,2)*Y X(1,3)*Y
X(2,1)*Y X(2,2)*Y X(2,3)*Y ]
if l==1&&k==1
figure(3)
plot([real(DS) imag(DS)])
xlabel('thoi gian')
ylabel('bien do')
title('2 symbols of DS modulation signal'),
axis([0 2*Nc -1/sqrt(Nc) 1/sqrt(Nc)]),pause,
end
%% noise gereration
n=1/sqrt(2)*(randn(Ns,1)+1j*(randn(Ns,1)));
if l==1&&k==1
var_n=norm(n)^2,pause
end
%% noise for DS-BPSK

n2=1/sqrt(2)*(randn(Ns*Nc,1)+1j*randn(Ns*Nc,1));
%% AWGN BPSK
Bpsk=sqrt(snr(k))*data+n;
if l==1&&k==1
figure(4)
plot([real(Bpsk) data])
xlabel('thoi gian')
ylabel('gia tri')
legend('real part of signal','data'),
title('BPSK signal in noise'),pause
end
%% AWGN DS-BPSK
Ds=sqrt(snr(k))*DS+n2;
%% Rayleigh fading BPSK signal
taps=1/sqrt(2)*(randn(Ns,1)+1j*randn(Ns,1));
Bpsk_r=sqrt(snr(k))*abs(taps).*data+n;
if l==1&&k==1
figure(5)
plot([real(Bpsk_r) data])
xlabel('thoi gian')
ylabel('gia tri')
legend('real part of signal','data'),
title('Bpsk signal in noise & fading channel'),pause
end
%% difference between AWGN and RAYLEIGH channel
if l==1&&k==1
figure(6)
plot(abs([Bpsk Bpsk_r]));
xlabel('thoi gian')
ylabel('gia tri')
legend('AWGN','RAYLEIGH'),
title('BPSK in AWGN $ RAYLEIGH fading channel'),pause
end
%% demodulation
% BPSK
r1=real(Bpsk);
r2=real(Bpsk_r);
% DS-BPSK
for v=1:Ns
r3(v)=real(ds'*DS((v-1)*Nc+1:v*Nc));
end
r3=r3(:);
% demodulation symbols are actually MF output peaks
if l==1&&k==1
figure(7)
plot(real(filter(conj(ds(Nc:-1:1)),1,DS)))
%%conj hm tr v gt phc, filter th chu
legend('without noise')
title('DS-BPSK signal after MF over 6 symbols'),
axis([0 6*Nc -3 3]),pause
end
% different demodulation symbols
if l==1&&k==1
figure(8)
plot([r1 r2 r3])
xlabel('thoi gian')
ylabel('gia tri')

legend('AWGN','Rayleigh','DS')
title('demodulated symbols'),pause
end

% AWGN

d1=find(r1>=0);d2=find(r1<0); %%find lc ra cc gi tr tha mn k

trong ()

r1(d1)=1;r1(d2)=-1;
d1=find(r2>=0);d2=find(r2<0);
r2(d1)=1;r2(d2)=-1;
d1=find(r3>=0);d2=find(r3<0);
r3(d1)=1;r3(d2)=-1;
if l==1&&k==1
figure(9)
plot([r1 r2 r3])
xlabel('thoi gian')
ylabel('bien do')
legend('AWGN','Rayleigh','DS')
axis([0 Ns -1.1 1.1]),
title('demodulated symbols after hard decision')
pause
end
% BER analysis
Ber1=length(find((data-r1)~=0));
Ber2=length(find((data-r2)~=0));
Ber3=length(find((data-r3)~=0));
if l==1&&k==1
errors=[Ber1 Ber2 Ber3],pause
end
% we add errors
BER1(k)=BER1(k)+Ber1;
BER2(k)=BER2(k)+Ber2;
BER3(k)=BER3(k)+Ber3;
% we stop MC
if BER1(k)>Nerr & BER2(k)>Nerr & BER3>Nerr,break
end
end
BER1(k)=BER1(k)/Ns/l;
BER2(k)=BER2(k)/Ns/l;
BER3(k)=BER3(k)/Ns/l;
end
BER=[SNR BER1 BER2 BER3];
The_awgn=.5*erfc(sqrt(2*snr)/sqrt(2));
The_rayl=.5*(1-sqrt(snr./(1+snr)));
figure(10)
semilogy(SNR,[The_awgn The_rayl BER1 BER2 BER3]) %%hm v th trc y chia
theo logarit
xlabel('SNR[dB]')
ylabel('BER')
axis([0 SNR(length(SNR)) 1e-6 .5])
grid on %%bt li
legend('Theor AWGN','Theor RAY','AWGN','RAY','DS-BPSK')

You might also like