Chương trình mô phỏng: % Bai tap 2 % Đỗ Minh Châu % D10VT2 - 1021010141 % Matlab R2013a
%% ---Thiet lap tham so---
clc,clear,close all
Es=10;
n=5; %bac cua bo loc
N=1e6; % toc do bits
phi0=pi/2; % pha tin hieu phat
fs=5e6; % tan so lay mau
fc=1e6; % tan so song mang
ts=1/fs; % Thoi gian lay mau
Nsym=N/3; % Toc do symbols
%% Nhap tin hieu phat
d = randint(3,2000,[0 1]); % tao chuoi cac bit ngau nhien tuong ung voi cac ky hieu
h = modem.pskmod('M', 8, 'InputType', 'Bit'); % Dieu che 8PSK
y = modulate(h, d); % tao chuoi 8 ky hieu 8PSK tuong ung
t=0:ts:30e-5;
s1=zeros(1,length(t));
for l=1:length(t) % Vong lap bat dau thuc hien dieu che
s=0;
for k=1:100
a(k)=(t(l)-0.5*tsym-(k-1)*tsym)/tsym; s=s+y(k)*sqrt((2*Es)/tsym)*rect(a(k)); end
s1(l)=s*exp(j*phi0); %tin hieu phat tuong duong bang goc
end
s2 = raisedcosflt(s1,1.5e6,ts,0.5); % Bo loc redcosflt
sp = sqrt(2)*real(s1.*exp(j*2*pi*fc*t)); % Tin hieu thong dai 8PSk
plot(t,sp,'k')
axis([0 4e-5 -2000 2000])
title('Tin hieu thong dai 8PSK')
%Bieu dien tin hieu phat tuong duong bang goc 8PSK
figure
plot(t,real(s1),'r')
title('Dang tin hieu bang goc 8PSK') xlabel('Time')
ylabel('Bien do') grid on
axis([0 6e-5 -1500 1500])
%Bieu dien chom sao
h = scatterplot(y,1,0,'*k'); title('chom sao tin hieu 8-PSK')
%Bieu dien mau mat
eyediagram(real(s2),30);
title('mau mat tin hieu 8PSK')
% Bieu dien pho
figure
[f,Pf] = pho(t,real(s1)); plot(f,Pf,'m')
title('pho tin hieu bang goc 8PSK') xlabel('f-Hz')
ylabel('Pf') grid on
axis([-2e6 2e6 0 3e4])
%% Truyen qua kenh AWGN
SNRdB = 10; % dB
SNR = 10^(SNRdB/10);
varn = var(s1)/SNR; % computing variance of noise
figure
plot(t,real(s1_awgn),'r') grid on
title('Tin hieu 8PSK truyen qua AWGN') axis([0 6e-5 -1500 1500])
% Bieu dien chom sao
y_awgn = awgn(y,10,'measured'); h = scatterplot(y_awgn,1,0,'xg'); hold on;
scatterplot(y,1,0,'or',h);
title('chom sao tin hieu 8PSK truyen qua AWGN')
% Bieu dien mau mat
eyediagram(real(s1_awgn),40);
title('Mau mat tin hieu 8PSK truyen qua AWGN')
% Bieu dien pho
figure
[f,Pf] = pho(t,real(s1_awgn)); plot(f,Pf,'m');
title('Pho tin hieu 8PSK truyen qua AWGN') grid on
axis([-2e6 2e6 0 3e4])
%% ---Xu ly va khoi phuc tai bo thu---
h = modem.pskdemod('M', 8, 'OutputType', 'Bit'); % Giai dieu che 8PSK
ydemod = demodulate(h, y_awgn); % Khoi phuc chuoi bit ban dau
%bo loc raised cosin tai phia thu
sr = raisedcosflt(s1_awgn,1.5e6,ts,0.5); % Bo loc redcosflt
figure
plot(t,real(sr),'r')
title('tin hieu 8PSK duoc khoi phuc') grid on
axis([0 6e-5 -1500 1500])
%Bieu dien chom sao
h = scatterplot(sr,1,0,'xg');
title('chom sao tin hieu 8-PSK duoc khoi phuc')
% Bieu dien dang pho
figure
[f,Pf] = pho(t,sr); plot(f,Pf,'m')
title('Pho tin hieu bang goc 8PSk duoc khoi phuc') axis([-2e6 2e6 0 3e4])
grid on
%Bieu dien mau mat
eyediagram(real(sr),40)
title('mau mat tin hieu 8PSK duoc khoi phuc')
num_bit = 2000; %chieu dai tin hieu
max_run = 20; %so lan chay toi da cho mot tin hieu SNR
Es = 10; % nang luong ky hieu
SNRdB = 0:0.8:20; % Tin hieu nhieu vo tuyen (dB)
SNR = 10.^(SNRdB/10);
hand = waitbar(0,'uoc tinh Monte-carlo dang thuc hien vong lap ...');
for count=1: length(SNR) %Bat dau vong lap cho SNR khac
avgError = 0;
N0 = Es/SNR(count); % Tinh toan cong suat nhieu cho SNR
%EbNo = SNR - log10(3); % Tinh EbNo theo dB
for run_time = 1:max_run % Bat dau nhung vong lap khac nhau
waitbar((((count-1)*max_run)+run_time-1)/(length(SNRdB)*max_run)); Error = 0;
N = sqrt(N0/2)*randn(1,num_bit); % tao AWGN
y = randsrc(1,200,[1, -1, j, -j, 1/sqrt(2)*(-1-j), 1/sqrt(2)*(1+j), 1/ sqrt(2)*(-1+j), 1/sqrt(2)*(1-j)]);
Y = y + N; % tin hieu nhieu awgn
for k = 1:num_bit %thiet bi quyet dinh thuc hien quyet dinh loi
if (...
( angle( Y(k)) < pi/8 && real(Y(k))>0 && angle(Y(k) )>-pi/ 8 && y(k)~=1)|| ...
( angle( Y(k)) > pi/8 && real(Y(k))>0 && imag(Y(k))>0 && angle(Y(k) )<3*pi/8 && y(k)~=1/sqrt(2)*(1+j))|| ...
( angle( Y(k)) > 3*pi/8 && imag(Y(k))>0 && angle(Y(k) )<5*pi/8 && y(k)~=j )|| ...
( angle( Y(k)) > 5*pi/8 && real(Y(k))<0 && imag(Y(k))>0 && angle(Y(k) )<7*pi/8 && y(k)~=1/sqrt(2)*(-1+j))|| ...
( angle( Y(k)) >7*pi/8 && real(Y(k))<0 && angle(Y(k))<- 7*pi/8 && y(k)~= -1 )||...
( angle( Y(k)) > -7*pi/8 && real(Y(k))<0 && imag(Y(k))<0 && angle(Y(k) )<-5*pi/8 && y(k)~=1/sqrt(2)*(-1-j))|| ...
( angle( Y(k)) > -5*pi/8 && imag(Y(k))<0 && angle(Y(k) )<- 3*pi/8 && y(k)~=-j )||...
( angle( Y(k)) > -3*pi/8 && real(Y(k))>0 && imag(Y(k))<0 && angle( Y(k) )<-pi/8 && y(k)~=1/sqrt(2)*(1-j)) ...
)
Error = Error + 1; end
end
Error = Error/num_bit; %Tinh toan loi /bit
avgError = avgError + Error; % Tinh toan loi/bit cho nhung lan chay khac
end % ket thuc vong lap
BER_sim(count) = avgError/max_run; % tinh toan ber cho SNR
end % ket thuc vong lap cho SNR khac nhau
BER_th = erfc(sqrt(2*SNR)*sin(pi/16)); % tinh toan phan tich ber
close(hand); figure;
semilogy(SNRdB,BER_th,'r'); %Ve BER ly thuyet
hold on;
semilogy(SNRdB,BER_sim,'b*'); % Ve BER mo phong
xlabel('Es/N0 (dB)'); ylabel('BER');
legend('Ly thuyet','Simulation',3); axis([min(SNRdB) max(SNRdB) 10^(-5) 1]);
title('Sai so giua thuc te va ly thuyet'); hold on;