rs_i=rs_framei,:+ISI; else rs_i=rs_framei,:; end; %Giai ma OFDM Demodulated_signal_i=OFDM_Demodulatorrs_i,NFFT,NFFT,G; d=Demodulated_signal_i./H; demodulated_symbol_i=ddemodced,1,1,''''QAM''''
Trang 1Ph l c ụ ụ
Mã ngu n Matlab và các k t qu mô ph ng ồ ế ả ỏ
Bài s 1: Mô ph ng kênh Rayleigh theo ph ố ỏ ươ ng pháp Rice
Bài 1.1: Tính các thông s f, C, ố θ:
%================================
%Tinh cac thong so f, C, th
%================================
clear
f_m=91; %Tan so Doppler lon nhat
b=1; %Phuong sai cua qua trinh g(t)
N1=9; %So phan tu g(t) phan thuc mo phong
N2=10; % So phan tu g(t) phan ao mo phong
for n=1:1:N1;
c1(n)=sqrt(2*b/N1);
f1(n)=f_m*sin(pi*(n-0.5)/(2*N1));
th1(n)=2*pi*n/(N1+1);
end
for n=1:1:N2;
c2(n)=sqrt(2*b/N2);
f2(n)=f_m*sin(pi*(n-0.5)/(2*N2));
th2(n)=2*pi*n/(N2+1);
end
save ex4p1_Res f1 f2 c1 c2 th1 th2
Bài 1.2: L p hàm g.m đ tính toán ậ ể
%=======================================
%Ham tao qua trinh g(t)
%=======================================
function y=g(c,f,th,t)
y=zeros(size(t));
for n=1:length(f);
y=y+c(n)*cos(2*pi*f(n).*t+th(n));
end;
Bài 1.3: Vi t hàm đ đ a ra biên đ kênh ế ể ư ộ và v hàm : ẽ
%=====================================
%Tinh bien do kenh va ham alpha
%=====================================
clear;
load ex4p1_Res f1 f2 c1 c2 th1 th2
f_s=270800; %Tan so lay mau
T_sim=0.4; %Thoi gian mo phong
t=0:1/f_s:T_sim; % Thoi gian mo phong t
g1=g(c1,f1,th1,t);
g2=g(c2,f2,th2,t);
g=g1+j*g2;
alpha=abs(g);
alpha_dB=20*log10(alpha);
Hoàng S Tân – MSSV: 20083473 – L p ĐT3-K53 ỹ ớ
Trang 2title('Bien do kenh (dB)');
xlabel('\alpha(t)');
legend('\alpha(t)in dB',0);
Đ th : ồ ị
Bài 1.4: xác đ nh các thông s giá tr trung bình, đ bi n đ i, hàm m t đ xác su t (PDF): ị ố ị ộ ế ổ ậ ộ ấ
%======================================
%Ham tinh gia tri trung binh va
% do bien doi cua g1(t), g(t), alpha(t)
%Ham mat do xac suat cua cac qua trinh
% bang bieu do
%======================================
clear;
load ex4p1_Res f1 f2 c1 c2 th1 th2
f_s=50000; %Tan so lay mau
T_sim=20; %Thoi gian mo phong
t=0:1/f_s:T_sim;
g1=g(c1,f1,th1,t);
g2=g(c2,f2,th2,t);
g=g1+j*g2;
Trang 3mean_g=mean(g); %Gia tri trung binh cua g
mean_g1=mean(g1); % Gia tri trung binh cua g1
mean_alpha=mean(alpha); % Gia tri trung binh cua alpha
var_g=var(g); %Do bien doi cua g
var_g1=var(g1); % Do bien doi cua g1
var_alpha=var(alpha); % Do bien doi cua alpha
n=length(alpha);
x=0:0.1:3; %Thoi gian tinh bang giay
b=hist(alpha,x); %Bieu do mat do xac suat cua alpha
figure(1);
stem(x,b/n/(x(2)-x(1)));%Ve tin hieu roi rac
hold on;
k=0; %He so k'=s^2/(2*b_0)
total_p=2; %Cong suat nhan duoc
alpha_p=(2.*x.*(k+1)/total_p).*exp(-k-((k+1).*x.^2/total_p)).*besseli(0, (2.*x.*sqrt(k*(k+1)/total_p)));
plot(x,alpha_p,'r');
title('Mat do xac suat cua alpha(x)');
xlabel('x');
ylabel('P_{\alpha}(x)');
legend('p_{\alpha}(x)' 'Phan bo Rayleigh', );
hold off;
figure(2);
n1=length(g1);
x1=-4:0.1:4;
c=hist(g1,x1); %Bieu do mat do xac suat cua g1
stem(x1,c/n1/(x1(2)-x1(1)));
hold on;
p=(1/sqrt(2*pi))*exp(-x1.^2/2);
plot(x1,p,'r');
title('Mat do xac suat PDF cua g1');
xlabel('x');
ylabel('P_{g1}(x)');
legend('p_{g1}(x)' 'Phan bo Gaussian', );
hold off;
Đ th : ồ ị
Hoàng S Tân – MSSV: 20083473 – L p ĐT3-K53 ỹ ớ
Trang 5Hoàng S Tân – MSSV: 20083473 – L p ĐT3-K53 ỹ ớ
Trang 6Bài s 2: Mô hình kênh Fadding phân t p đa đ ố ậ ườ ng theo ph ươ ng pháp
Monte Carlo
Bài 2.1: Mô hình kênh phân t p đa đ ậ ườ ng :
Hàm mô hình Monte Carlo
%==========================================================================
%Ham mo hinh kenh phan tap da duong theo phuong phap Monte Carlo
%========================================================================== function [h,t_next]
=MCM_channel_model(u,initial_time,number_of_summations,symbol_duration,f_dmax ,channel_coefficients);
%u la bien ngau nhien duoc dung de roi rac tan so, pha cua hieu uong Doppler
%number_of_summation: so luong hoac pha roi rac duoc su dung trong
%phuong phap Monte Carlo
%symbol+duration la khoang thoi gian lay mau cua tin hieu
%f_fmax La tan so Doppler lon nhat
t=initial_time; %thoi gian bat dau quan sat kenh
Channel_Length=length(channel_coefficients); % Chieu dai dap ung kenh
h_vector=[]; %Vector mo hinh CIR
for k=1:Channel_Length;
u_k=u(k,:);
%Mot bien ngau nhien cua k
phi=2*pi*u_k;
%tan so Doppler roi rac su dung trong Monte Carlo
f_d=f_dmax*sin(2*pi*u_k);
h_tem=channel_coefficients(k)*1/
(sqrt(number_of_summations))*sum(exp(j*phi).*exp(j*pi*f_d*t));
h_vector=[h_vector,h_tem];
end;
h=h_vector;
t_next=initial_time+symbol_duration;
end
Ch ươ ng trình chính:
%==========================================================================
%Chuong trinh chinh mo phong phuong phap Monte Carlo
%==========================================================================
clear all;
%Dat cac thong so
NFFT=64; %Chieu dai FFT
t_a=8.0000e-007; %Thoi gian lay mau tin hieu 1/t_a
T_S=NFFT*t_a; %Thoi gian quan sat
number_of_summations=40;
f_dmax=0.0; %Tan so Doppler lon nhat
channel_profile=[1.0000,0.8487,0.7663,0.7880,0.6658,0.6658,0.5174,0.0543,0.04 56];
%He so cong suat tre
rho=channel_profile;
N_P=length(rho); %Chieu dai dap ung kenh
u=rand(N_P,number_of_summations);
%Bien ngau nhien
Trang 7h=[]; %vector CIR
H=[]; %vector CTF
for i= 1:100;
h_i=MCM_channel_model(u,initial_time,number_of_summations,T_S,f_dmax,rho); h_i_tem=[h_i,zeros(1,NFFT-N_P)];
H_i_tem=fft(h_i_tem);
H_i=[H_i_tem(NFFT/2+1:NFFT),H_i_tem(1:NFFT/2)];
initial_time=initial_time+T_S;
h=[h;h_i];
H=[H;H_i];
end;
figure(2);
mesh(abs(H))
xlabel('f in B=1.25MHz')
ylabel('t in 1.024e-0.4s')
zlabel('H(f,t)')
title('f_{D,max}=0.0Hz')
L n l ầ ượ t thay b ng 0.0Hz, 5.0Hz và 500.0Hz ta có k t qu mô ph ng: ằ ế ả ỏ
Đ th : ồ ị
Hoàng S Tân – MSSV: 20083473 – L p ĐT3-K53 ỹ ớ
Trang 9Hoàng S Tân – MSSV: 20083473 – L p ĐT3-K53 ỹ ớ
Trang 10Bài 2.2: So sánh hàm t t ự ươ ng quang th i gian c a kênh cho 2 tr ờ ủ ườ ng h p: ợ
%==========================================================================
%chuong trinh uoc luong ham tu tuong tuong quan thoi gian cua kenh
%==========================================================================
clear all;
NFFT=64;
t_a=8.0000e-007;
T_S=NFFT*t_a;
number_of_summations=40;
f_dmax1=90.0;
f_dmax2=9.0;
channel_profile=[1.0000,0.8487,0.7663,0.7880,0.6658,0.5644,0.5174,0.0543,0.04 65];
rho=channel_profile;
N_P=length(rho);
u=rand(N_P,number_of_summations);
T_S=NFFT*t_a;
NofSymbol=1000;
t=0.1;
H_sequence1=[];
Trang 11[h1]=MCM_channel_model(u,t,number_of_summations,T_S,f_dmax1,rho); [h2]=MCM_channel_model(u,t,number_of_summations,T_S,f_dmax2,rho); h_extended1=[h1,zeros(1,NFFT-length(h1))];
h_extended2=[h2,zeros(1,NFFT-length(h2))];
H1=fft(h_extended1);
H2=fft(h_extended2);
H_f11=H1(3);
H_f12=H2(3);
H_sequence1=[H_sequence1,H_f11];
H_sequence2=[H_sequence2,H_f12];
t=t+T_S;
end;
time_autoc1=xcorr(H_sequence1,'coeff');
time_autoc2=xcorr(H_sequence2,'coeff');
tau1=-(NofSymbol-1)*T_S:T_S:(NofSymbol-1)*T_S;
tau2=-(NofSymbol-1)*T_S:T_S:(NofSymbol-1)*T_S;
plot(tau1,real(time_autoc1),'k.');
xlabel('\Delta t (s)');
ylabel('Tuong quan thoi gian R(\Delta t)');
hold on;
plot(tau2,real(time_autoc2),'k.');
legend('f_{D,max}=9.0Hz' 'f_{D,max}=90.0Hz', );
Đ th : ồ ị
Hoàng S Tân – MSSV: 20083473 – L p ĐT3-K53 ỹ ớ
Trang 12Bài s 3: Mô ph ng h th ng OFDM qua kênh vô tuy n ố ỏ ệ ố ế
Bài 3.1: mô ph ng h th ng đa sóng mang t n tr c giao (OFDM) s d ng các thông s l y t ỏ ệ ố ầ ự ử ụ ố ấ ừ chu n HyperLAN/2: ẩ
Code hàm “OFDM_Modulator”
function [y] = OFDM_Modulator(data,NFFT,G)
chnr=length(data);
N=NFFT;
x=[data,zeros(1,NFFT-chnr)];
a=ifft(x);%fft
y=[a(NFFT-G+1:NFFT),a];%chen khoang bao ve
end
Code hàm “OFDM_Demodulator”
function [y] = OFDM_Demodulator(data,chnr,NFFT,G)
%Chen khoang bao ve
x_remove_gaurd_interval=[data(G+1:NFFT+G)];
x=fft(x_remove_gaurd_interval);
y=x(1:chnr);
end
Code ch ươ ng trình chính:
%=============================
%Chuong trinh chinh
Trang 13clear all;
NFFT=64; %Chieu dai NFFT
G=9; %Khoang bao ve
M_ary=16; %Dang dieu che QAM
t_a=50*10^(-9);%Thoi gian lay mau
channel_profile=[1.0000,0.8487,0.7663,0.7880,0.6658,0.5644,0.5174,0.0543,0.04 65];
rho=channel_profile;
h=sqrt(rho);
N_P=length(rho);
H=fft([h,zeros(1,NFFT-N_P)]);
NofOFDMSymbol=100; %So ky tu cua OFDM
length_data=(NofOFDMSymbol)*NFFT; %Chieu dai tong cong cua du lieu
%Bits nguon
source_data=randint(length_data,sqrt(M_ary));
%Bit de ma hoa
symbols=bi2de(source_data);
%Dieu che QAM trong dai tan co so
QAM_Symbol=dmodce(symbols,1,1,'qam',M_ary);
%Chuan bi lay mau du lieu
Data_Pattern=[];
for i=0:NofOFDMSymbol-1;
QAM_tem=[];
for n=1:NFFT;
QAM_tem=[QAM_tem,QAM_Symbol(i*NFFT+n)];
end;
Data_Pattern=[Data_Pattern;QAM_tem];
clear QAM_tem;
end;
ser=[];%Dat so ky tu bi loi la vector rong
snr_min=0;
snr_max=25;
step=1;
for snr=snr_min:step:snr_max;
snr=snr-10*log10((NFFT-G)/NFFT);
rs_frame=[];%Ma tran tin hieu nhan
for i=0:NofOFDMSymbol-1;
%Dieu che OFDM
OFDM_signal_tem=OFDM_Modulator(Data_Pattern(i+1,:),NFFT,G);
%Tin hieu nhan duoc qua kenh da duong duoc tao ra boi phep chap
rs=conv(OFDM_signal_tem,h);
%Nhieu duoc them vao
rs=awgn(rs,snr,'measured' 'dB', );
rs_frame=[rs_frame;rs];
clear OFDM_signal_tem;
end;
%Nhan tin hieu
Receiver_Data=[];%Chuan bi ma tran de nhan ky tu
d=[];%Ky tu giai ma
data_symbol=[];
for i=1:NofOFDMSymbol;
if(N_P>G+1)&(i>1)
previous_symbol=rs_frame(i-1,:);
ISI_term=previuos_symbol(NFFT+2*G+1:NFFT+G+N_P-1);
Hoàng S Tân – MSSV: 20083473 – L p ĐT3-K53 ỹ ớ
Trang 14rs_i=rs_frame(i,:)+ISI;
else
rs_i=rs_frame(i,:);
end;
%Giai ma OFDM
Demodulated_signal_i=OFDM_Demodulator(rs_i,NFFT,NFFT,G);
d=Demodulated_signal_i./H;
demodulated_symbol_i=ddemodce(d,1,1,'QAM',M_ary);
data_symbol=[data_symbol,demodulated_symbol_i];
end;
data_symbol=data_symbol';
%Tinh toan ky tu loi
[number,ratio]=symerr(symbols,data_symbol);
ser=[ser,ratio];
end;
snr=snr_min:step:snr_max;
semilogy(snr,ser,'bo');
ylabel('SER');
xlabel('SNR in dB');
K t qu mô ph ng: ế ả ỏ
Trang 15Bài 3.2: Thay G=0 (không có kho ng b o v ) và công su t tr : ả ả ệ ấ ễ
channel_profile=[1.0000,0.6095,0.4945,0.3940,0.2371,0.1900,0.1159,0.0699,0.0462];
ta có đ th SER c a h th ng: ồ ị ủ ệ ố
Hoàng S Tân – MSSV: 20083473 – L p ĐT3-K53 ỹ ớ