Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Bài tập Matlab Thông tin vô tuyến

Sinh viên thực hiện : Hồ Anh Văn

MSSV : 20102541

Giảng viên hướng dẫn : PGS.TS Nguyễn Văn Đức

CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercise 1.1

% ====================================================================

% Calculation of the probability density function (PDF) of a Gaussian

% distributed random variable

% ====================================================================

t_a = 0.05; % Sampling interval

x=-4:t_a:4; % Set x variable

% Calculation of the PDF of a Gaussian distributed random variable

p=(1/sqrt(2*pi)*exp(-x.^2/2));

check=trapz(x,p); % Intergration of p

% Remark : the intergration of P(x) for -4<=x<=4 must be equal to 1

plot(x,p);

title('\fontsize{12}PDF of a Gaussian distributed random variable');

xlabel('x','Fontsize',12);

ylabel('P(x)','FontSize',12)

CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercise 1.2

% =============================================================

% Comparison of Gaussian distributed PDF with simulation result

% =============================================================

clear; % clear all available variables

m_mu=0; % mean value

sigma_mu=1; % variance

n=1000000; % length of the noise vector

x=-4:0.05:4; % set x variable

p=(1/sqrt(2*pi)*sigma_mu)*exp(-(x-m_mu).^2/2*sigma_mu^2); % calculate the


Gaussian distributed PDF

check=trapz(x,p) % the intergration of P(x) for -4<=x<=4 must equal 1

plot(x,p,'r');

hold on;

% =============================================================

% Generation o random vector,and calculate its distribution

% =============================================================

y=randn(1,n);

m=mean(y) % mean value of the process y

variance=std(y)^2 % variance of the process y

x2=-4:0.1:4;

c=hist(y,x2); % calculated the history of the process y

stem(x2,c/n/(x2(2)-x2(1)));

% the calcutation "c/n/(x2(2)-x2(1))"is to change from the history diagram

% to the PDF

title('Gaussian distributed PDF');

xlabel('X');

CuuDuongThanCong.com https://fb.com/tailieudientucntt
ylabel('P(X)');

legend('theoretical','experimental');

hold off;

Exercise 2.1

% =======================================

% Bessel function -> time autocorrelation function of the mobile channel

% =======================================

clear;

f_c=900e6; % the carrier frequency in Hz

c_0=3e8; % the speed of light in m/s

v=109.2e3/3600; % the mobile station’s speed in m/s

f_m=v*f_c/c_0; % the maximum Doppler frequency

ohm_p=2; % the total received power

t=0:0.001:0.08; % the time interval in second(s)

CuuDuongThanCong.com https://fb.com/tailieudientucntt
phi_gIgI=(ohm_p/2)*besselj(0,2*pi*f_m*t); % the autocorrelation function

plot(t,phi_gIgI);

title('The autocorrelation function ACF');

xlabel('\tau');

ylabel('\phi_{gIgI}(\tau)');

legend('\phi_{gIgI}(\tau)');

phi_gIgI_0=(ohm_p/2)*besselj(0,0);

Exercise 2.2

% =========================

% Doppler spectrum

% =========================

clear;

f_c=900e6; % the carrier frequency in Hz

c_0=3e8; % the speed of light in m/s

CuuDuongThanCong.com https://fb.com/tailieudientucntt
v=109.2e3/3600; % the mobile station’s speed in m/s

f_m=v*f_c/c_0; % the maximum Doppler frequency

ohm_p=2; % the total received power

z=-100:1:100; % the time interval in second (s)

for i=1:201;

f=i-101;

if abs(f)<=f_m

S_gIgI(i)=(ohm_p/2*pi*f_m)/sqrt(1-(f/f_m)^2);

else

S_gIgI(i)=0;

end

end

plot(z,S_gIgI)

title('The power spectral density (PSD)')

xlabel('f')

ylabel('S_{gIgI}(f)')

legend('S_{gIgI}(f)')

CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercise 2.3

% ===============================

% Distribution Rice

% ===============================

clear;

k0=0;

k1=3;

k2=80;

k=k0; % the rice of factor k=s^2/2b_0

x=0:0.1:3; % the time interval in seconds

ohm_p=1; % the total received power

p_elfa=(2.*x.*(k+1)/ohm_p).*exp(-k-
((k+1).*x.^2/ohm_p)).*besseli(0,(2.*x.*sqrt((k+1)/ohm_p)));

plot(x,p_elfa)

hold on

k=k1;

p_elfa1=(2.*x.*(k+1)/ohm_p).*exp(-k-
((k+1).*x.^2/ohm_p)).*besseli(0,(2.*x.*sqrt((k+1)/ohm_p)));

plot(x,p_elfa1)

hold on

k=k2;

p_elfa2=(2.*x.*(k+1)/ohm_p).*exp(-k-
((k+1).*x.^2/ohm_p)).*besseli(0,(2.*x.*sqrt((k+1)/ohm_p)));

plot(x,p_elfa2,'g-.')

title('The Rice distribution')

xlabel('x')

ylabel('p_{\alpha}(x)')

legend('k=0','k=1','k=80')

hold off

CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercise 2.4

% ======================================

% The non-central chi/square Distribution

% ======================================

clear;

k0=0;

k1=3;

k2=80;

k=k0; % the rice of factor k=s^2/2b_0

x=0:0.01:3; % the time interval in second (s)

ohm_p=1; % the total received power

CuuDuongThanCong.com https://fb.com/tailieudientucntt
p_alpha=(k+1)/ohm_p.*exp(((-k-
(k+1)).*x./ohm_p)).*besseli(0,(2*sqrt(k*(k+1).*x./ohm_p)));

plot(x,p_alpha)

hold on

k=k1;

p_alpha1=(k+1)/ohm_p.*exp(((-k-
(k+1)).*x./ohm_p)).*besseli(0,(2*sqrt(k*(k+1).*x./ohm_p)));

plot(x,p_alpha1,'r.')

k=k2;

p_alpha2=(k+1)/ohm_p.*exp(((-k-
(k+1)).*x./ohm_p)).*besseli(0,(2*sqrt(k*(k+1).*x./ohm_p)));

plot(x,p_alpha2,'g-')

title('The non-central chi-square Distribution')

xlabel('x')

ylabel('p_{\alpha^2}(x)')

legend('k=0','k=1','k=80')

hold off

CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercise 3.1

% ============================================

% The level crossing rate of the Rice processes

% ============================================

clear;

k0=0; % the Rice factor

k1=3;

k2=10;

R=0:0.05:4.5; % the vector of amplitude levels

ohm_p=1; % the total received power

rau=R./sqrt(ohm_p);

k=k0; % The Rice factor k=s^2/2b_0

L_R=sqrt(2*pi*(k+1)).*rau.*exp((-k-
(k+1)).*rau.^2).*besseli(0,(2.*rau.*sqrt(k*(k+1))));

plot(R,L_R)

CuuDuongThanCong.com https://fb.com/tailieudientucntt
hold on

k=k1;

L_R1=sqrt(2*pi*(k+1)).*rau.*exp((-k-
(k+1)).*rau.^2).*besseli(0,(2.*rau.*sqrt(k*(k+1))));

plot(R,L_R1,'r-')

k=k2;

L_R2=sqrt(2*pi*(k+1)).*rau.*exp((-k-
(k+1)).*rau.^2).*besseli(0,(2.*rau.*sqrt(k*(k+1))));

plot(R,L_R2,'k-')

title('Level Crossing Rate LCR)')

xlabel('R')

ylabel('L_R(R)/f_m')

legend('k=0','k=1','k=10')

hold off

CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercise 3.2

% ==========================================

% The average duration of fading of the Rice processes

% ==========================================

clear;

k0=0;

k1=3;

k2=10;

R=0.0001:0.05:4.51;

ohm_p=1; % the total received power

rau=R./sqrt(ohm_p);

k=k0; % The Rice factor k=s^2/2b_0

L_R=sqrt(2*pi*(k+1)).*rau.*exp((-k-
(k+1)).*rau.^2).*besseli(0,(2.*rau.*sqrt(k*(k+1))));

for r=0:0.05:4.5

x=linspace(0,r);

a=0:0.05:r;

i=length(a);

p=(2.*x.*(k+1)/ohm_p).*exp(-k-
((k+1).*x.^2/ohm_p)).*besseli(0,(2.*x.*sqrt(k*(k+1)/ohm_p)));

CDF(i)=trapz(x,p);

end

ADF=CDF./L_R;

plot(R,ADF)

hold on

k=k1; % the rice factor k=s^2/2b_0

L_R1=sqrt(2*pi*(k+1)).*rau.*exp((-k-
(k+1)).*rau.^2).*besseli(0,(2.*rau.*sqrt(k*(k+1))));

for r=0:0.05:4.5

CuuDuongThanCong.com https://fb.com/tailieudientucntt
x=linspace(0,r);

a=0:0.05:r;

i=length(a);

p=(2.*x.*(k+1)/ohm_p).*exp(-k-
((k+1).*x.^2/ohm_p)).*besseli(0,(2.*x.*sqrt(k*(k+1)/ohm_p)));

CDF1(i)=trapz(x,p);

end

ADF1=CDF1./L_R1;

plot(R,ADF1,'r.')

hold on

k=k2;

L_R2=sqrt(2*pi*(k+1)).*rau.*exp((-k-
(k+1)).*rau.^2).*besseli(0,(2.*rau.*sqrt(k*(k+1))));

for r=0:0.05:4.5

x=linspace(0,r);

a=0:0.05:r;

i=length(a);

p=(2.*x.*(k+1)/ohm_p).*exp(-k-
((k+1).*x.^2/ohm_p)).*besseli(0,(2.*x.*sqrt(k*(k+1)/ohm_p)));

CDF2(i)=trapz(x,p);

end

ADF2=CDF2./L_R2;

plot(R,ADF2,'g-')

axis([0 2 0 3])

title('The average duration of fading (ADF)')

xlabel('R')

ylabel('t(R)*f_m')

legend('k=0','k=1','k=10')

hold off

CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercise 3.3.1

% ==================================

% Nakagami distribution

% ==================================

clear;

m1=0.5; % The Nakagami shape factor

m2=1;

m3=4;

m4=20;

ohm_p=1;

x=0:0.05:3;

m=m1;

p_alpha1=(2*m^m.*x.^(2*m-1)/gamma(m)*ohm_p^m).*exp(-m.*x.^2/ohm_p);

plot(x,p_alpha1)

hold on

CuuDuongThanCong.com https://fb.com/tailieudientucntt
m=m2;

p_alpha2=(2*m^m.*x.^(2*m-1)/gamma(m)*ohm_p^m).*exp(-m.*x.^2/ohm_p);

plot(x,p_alpha2,'r.')

m=m3;

p_alpha3=(2*m^m.*x.^(2*m-1)/gamma(m)*ohm_p^m).*exp(-m.*x.^2/ohm_p);

plot(x,p_alpha3,'g-')

m=m4;

p_alpha4=(2*m^m.*x.^(2*m-1)/gamma(m)*ohm_p^m).*exp(-m.*x.^2/ohm_p);

plot(x,p_alpha4,'m--')

hold off

title('Nakagami distribution p_{\alpha}(x)')

xlabel('x')

ylabel('p_{\alpha}(x)')

legend('m=0.5','m=1','m=4','m=20')

CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercise 3.3.2

% =====================================================
% The Rice and Nagakami distribution
% =====================================================
clear;
k=2; % the rice factor k=s^2/2b_0
x=0:0.01:3; % the time interval in seconds
ohm_p=1; % the total received power
p_elfa=(2.*x.*(k+1)/ohm_p).*exp(((-k-
(k+1)).*x./ohm_p)).*besseli(0,(2*sqrt(k*(k+1).*x./ohm_p)));
plot(x,p_elfa)
hold on
m=1.8;
p_alpha1=(2*m^m.*x.^(2*m-1)/gamma(m)*ohm_p^m).*exp(-m.*x.^2/ohm_p);
plot(x,p_alpha1,'r-.')
hold off
title('Rice and Nagakami Distribution p_{\alpha}(x)')
xlabel('x')
ylabel('p_{\alpha}(x)')
legend('Rice p_{\alpha}(x) with k=2','Nakagami p_{\alpha}(x) with m=1.8')

CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercise 4.1

% ==============================================
% Calculation of simulation parameters
% ==============================================

clear;
f_m=91; % Maximal Doppler frequency
b=1; % Variance of in-phase or quadrature component
N1=9; % Number of sinusoids for the in-phase component
N2=10; % Number of sinusoids for the quadrature component
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
% these result will be use in the exercise 4.3 and 4.4

Exercise 4.2

% ===================================================
% a function which creates the determisistic process g(t)
% save this function to a file name "g.m"
% ===================================================
function y=g(c,f,th,t)
y=zeros(site(t));
for n=1:length(f);
y=y+c(n)*cos(2*pi*f(n).*t+th(n));
end

Exercise 4.3

% ===========================================
% Crate a deterministic process by Rice method
% ===========================================
clear;
load ex4p1_Res f1 f2 c1 c2 th1 th2
f_s = 270800; % the carrier frequency in Hz
T_sim = 0.4; % Simulation time in second
t = 0:1/f_s:T_sim; % discrete time interval
g1 = g(c1, f1, th1, t); % generation of the process g1 by using the function
"g.m" from the excercise 4.2
g2 = g(c2, f2, th2, t);
g = g1+j*g2;
alpha = abs(g);
alpha_dB = 20*log10(alpha);
plot(t, alpha_dB);

CuuDuongThanCong.com https://fb.com/tailieudientucntt
title('The channel amplitude in dB');
xlabel('t');
ylabel('\alpha(t)');
legend('\alpha(t) in dB', 0);

Exercise 4.4

% =========================================================
% Comparision of theoretical Gaussian and Reyleigh distribution with
% simulations results
% =========================================================
clear;
load ex4p1_Res f1 f2 c1 c2 th1 th2
f_s = 50000; % the carrier frequency in Hz
T_sim = 20; % simulation time in seconds
t = 0:1/f_s:T_sim;
g1 = g(c1, f1, th1, t);
g2 = g(c2, f2, th2, t);
g = g1+j*g2;
alpha = abs(g);
g_mean = mean(g);
g_variance = var(g);
g1_mean = mean(g1);
g1_variance = var(g1);
alpha_mean = mean(alpha);
alpha_variance = var(alpha);
n = length(alpha);
x = 0:0.1:3; % the time interval in seconds
b = hist(alpha, x);

figure(1);
stem(x, b/n/(x(2)-x(1)));
hold on;
k = 0; % the rice factor k=s^2/2b_0
ohm_p = 2; % the total received power
p_alpha = (2*x*(k+1)/ohm_p).*exp(-k-((k+1)*x.^2/ohm_p)).*besseli(0,
(2*x.*sqrt(k*(k+1)/ohm_p)));
plot(x, p_alpha, 'r');
title('The PDF of {\alpha}(x)');
xlabel('x');
ylabel('P_{\alpha}(x)');
legend('p_{\alpha}(x)', 'Rayleigh distribution (Theory)');
hold off;

figure(2);
n1 = length(g1);
x1 = -4:0.1:4;
c = hist(g1, x1);
stem(x1, c/n1/(x1(2)-x1(1)));
hold on;
p = (1/sqrt(2*pi))*exp(-x1.^2/2);
plot(x1, p, 'r');
title('The PDF of g1 process');
xlabel('x');

CuuDuongThanCong.com https://fb.com/tailieudientucntt
ylabel('P_{g1}(x)');
legend('p_{g1}(x)', 'Gaussian distribution (Theory)');
hold off;

Exercise 4.5

% =============================================================
% Autocorrelation result of g1 process
% =============================================================
clear;
load ex4p1_Res f1 f2 c1 c2 th1 th2
f_s = 1000; % the carrier frequency in Hz
T_s = 1/f_s; % the sampling time in seconds
I1 = 10000*T_s;
I2 = 20000*T_s;
t = I1:T_s:I2;
g1 = g(c1, f1, th1, t);
phi_g1g1 = xcorr(g1, 'biased');
i = 1:81;
x = (i-1)*T_s;
phi_g1g1_select = phi_g1g1(10001:10081);
plot(x, phi_g1g1_select);
title('The autocorrelation function ACF of g1');
xlabel('\tau in seconds');
ylabel('\phi_{g1g1}(\tau)');
legend('\phi_{g1g1}(\tau) of g1');
save ex4p5_Res phi_g1g1_select f_s phi_g1g1;

Exercise 4.6

% ========================================================
% Simulation result of ACF of g1 in comparison with theoretical result
% ========================================================
clear;
f_m = 91; % Maximum Doppler frequency
b = 1;
N1 = 9; % Number of sinusoids
tau_max = 0.08;
t_a = 0.001; % Sampling interval
for n = 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

tau = 0:t_a:tau_max;
k = 1:length(tau);
for n = 1:N1
x(n, k) = (c1(n).^2/2)*cos(2*pi*f1(n).*tau);
end
fay = sum(x);

tau_s = -tau_max:t_a:tau_max;

CuuDuongThanCong.com https://fb.com/tailieudientucntt
k = 1:length(tau_s);
for n = 1:N1
xs(n, k) = (c1(n).^2/2)*cos(2*pi*f1(n).*tau_s);
end
phi_g1g1_theory = sum(xs);

plot(tau, fay);
hold on;
f_c = 900e6; % The carrier frequency in Hz
c_0 = 3e8; % The speed of light in m/s
v = 109.2e3/3600; % The mobile station's speed in m/s
f_m = v*f_c/c_0; % The maximum doppler frequency
ohm_p = 2; % The total received power
t = 0:0.001:tau_max;
z = 2*pi*f_m*t;
phi_g1g1 = (ohm_p/2)*besselj(0, z); % The autocorrelation function
plot(tau, phi_g1g1, 'r');

load ex4p5_Res;
N = length(phi_g1g1);
phi_g1g1_s = phi_g1g1(N/2:N/2+tau_max/t_a);

plot(tau, phi_g1g1_s, 'k');


title('The autocorrelation function (ACF) of the process g1');
xlabel('\tau in seconds');
ylabel('\phi_{g1g1}(\tau)');
legend('\phi_{g1g1}(\tau) Simulation model (Theory)',
'\phi_{g1g1}(\tau) Reference model'; '\phi_{g1g1}(\tau) Simulation');
hold off;

Exercise 4.8

% ==========================================================
% Comparision of the PSD of the low-pass fading signal generated by the
% Rice's method with the refrence PSD
% ==========================================================
fm = 91; % Doppler frequency in Hz
b = 1;
N1 = 9;
f_1_n = fm*sin(pi*((1:N1)-1/2)/(2*N1));
c_1_n = sqrt(2*b/N1)*ones(size(f_1_n));
theta_1_n = 2*pi*(1:N1)/(N1+1);
fs = 1000; % Sampling frequency
t_sim = (10E3:20E3)/fs; % Time interval

g1_t_tilde = zeros(size(t_sim));
for k = 1:length(f_1_n)
g1_t_tilde = g1_t_tilde+c_1_n(k)*cos(2*pi*f_1_n(k)*t_sim+theta_1_n(k));
end

r_g1_g1 = xcorr(g1_t_tilde, 'biased');


PSD_S = fft(r_g1_g1);
len_S = length(PSD_S);

CuuDuongThanCong.com https://fb.com/tailieudientucntt
f_PSD = (-(len_S-1)/2:(len_S-1)/2)/len_S*fs;
plot(f_PSD, fftshift(abs(PSD_S)/len_S), 'b--');
hold on;

% Reference Model
Omega_p = 2;
f = (-fm):1:(fm);
S_f = Omega_p/2/pi/fm./sqrt(1-(f/fm).^2);
plot(f, S_f, 'r-');

% Theoretical result of simulation model


f_tilde = [-f_1_n(N1:-1:1), f_1_n];
S_mm_tilde = [c_1_n(N1:-1:1).^2, c_1_n.^2]/4;
stem(f_tilde, S_mm_tilde, 'm:x');
hold off;

ax = axis;
ax(1) = -100;
ax(2) = 100;
ax(3) = 0;
ax(4) = 0.08;
axis(ax);
xlabel('Doppler frequency, f in Hz');
ylabel('PSD, S_{g_1g_1}(f)');
legend('Simulation', 'Reference model', 'Simulation model');

CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercise 5.1

% =============================================
% QPSK Modulation
% =============================================
clear;
x = round(rand(1, 10000));
for i = 1:2:length(x)
if x(i)==0 & x(i+1)==0
S((i+1)/2)=exp(j*pi/4);
elseif x(i)==0 & x(i+1)==1
S((i+1)/2)=exp(j*3*pi/4);
elseif x(i)==1 & x(i+1)==1
S((i+1)/2)=exp(j*5*pi/4);
elseif x(i)==1 & x(i+1)==0
S((i+1)/2)=exp(j*7*pi/4);
end
end
save ex5p1_Res S x; % will be use in 5.2
plot(S,'*');
hold on;
t=0:0.01:2*pi;
plot(exp(j*t),'r--');
xlabel('\phi(t)');
ylabel('S_m');
title('The complex signal-space diagram for 4-QPSK');

CuuDuongThanCong.com https://fb.com/tailieudientucntt
Exercise 5.2

% =====================================================
% QPSK Modulation in process of noise
% =====================================================
clear;
load ex5p1_Res;
Es=var(S); % symbol energy
Eb=Es/2; % bit energy
SNR_db=6; % signal to noise ratio in db
N_0=Eb/10^(SNR_db/10);
N=sqrt(N_0/2)*(randn(Size(S))+j*randn(size(S)));
R=S+N; % add noise to the signal
plot(R,'.');
hold on;
plot(S,'r*');
hold on;
t=0:0.01:2*pi;
plot(exp(j*t),'r--');
legend('S_m','S');
xlabel('I');
ylabel('Q');
title('The complex signal-space diagram of 4-QPSK');
hold off;

Exercise 7

% ===========================================================
% A function of perform QPSK demodulation,Viterbi decoding
% and calculation of bit error probability
% and save this function to a file name "cha2.m"
% ===========================================================

function y=cha2(SNR_db,S,meg,trellis) % for ex7


% QPSK Demodulation
Es=var(S);
Eb=Es/2;
N_0=Eb/10^(SNR_db/10);
N0=sqrt(N_0/2)*(randn(size(S))+j*randn(size(S)));
NS=S+N0;
theta_m=[pi/4,3*pi/4,5*pi/4,7*pi/4];
S_m=exp(j*theta_m);
for i=1:length(S)
% calculate the distance of the received symbol to all possible
% reference symbols
d=abs(S_m-NS(i));
% QPSK Demodulation
md=min(d);
if md==d(1);
R(2*i-1)=0;
R(2*i)=0;
elseif md==d(2)
R(2*i-1)=0;
R(2*i)=1;
elseif md==d(3)

CuuDuongThanCong.com https://fb.com/tailieudientucntt
R(2*i-1)=1;
R(2*i)=1;
elseif md==d(4)
R(2*i-1)=1;
R(2*i)=0;
end
end
% Viterbi Decoder
opmode='trunc';% the operation mode
dectype='hard';% the representation type in the code(x)& [01]
tblen=12;% the traceback depth
r=vitdec(R,trellis,tblen,opmode,dectype);

% Calculation of Bit Error Probablity


c=0;
for i=1:length(r)
if r(i)~=meg(i)
c=c+1;
end
end
y=c;

% ===============================================
% Main program of excercise 7
% ===============================================

clear;
k=3;
R_c=1/2;

% Bit source
meg=round(rand(1,5000)); % the input bit stream

%Convolutional coder
conlen=3; % constraintlength=k=1+v,v=number of shift register
codegen=[7 5]; % codegenerator g(1)=(1,1,1)->7,g(2)=(1,0,1)->5
trellis=poly2trellis(conlen,codegen);
x=convenc(meg,trellis); % the coded bit stream

% QPSK Modulation
for i=1:2:length(x)
if x(i)==0 & x(i+1)==0
S((i+1)/2)=exp(j*pi/4);
elseif x(i)==0 & x(i+1)==1
S((i+1)/2)=exp(j*3*pi/4);
elseif x(i)==1 & x(i+1)==1
S((i+1)/2)=exp(j*5*pi/4);
elseif x(i)==1 & x(i+1)==0
S((i+1)/2)=exp(j*7*pi/4);
end
end
% S is the modulation complex signal

SNR_db=0:5;
for i=1:length(SNR_db)

CuuDuongThanCong.com https://fb.com/tailieudientucntt
c(i)=cha2(SNR_db(i),S,meg,trellis);
% cha2 will demodulate ,decode and calculate the number of error in the
% received signal
end
BEP=c/length(meg);
semilogy(SNR_db,BEP,'.--')
hold on
SNR_db=0:8;
load ex6p1_Res % result from excercise 6.1
BEP2=c/length(x);
semilogy(SNR_db,BEP2,'r.--')
title('The bit error probablity')
xlabel('SNR in db')
ylabel('P_b')
legend('P_b with coder','P_b without coder')
hold off

Exercise 8.1

% ============================================
% OFDM Modulator
% NFFT:FFT length
% chnr:number of subcarrier
% G:guard length
% Save this program to a file name "OFDM_Modulator.m"
% for use in the main function
% ============================================
function [y] = OFDM_Modulator(data,NFFT,G);
chnr=length(data);
N=NFFT;
x=[data,zeros(1,NFFT-chnr)]; % Zero padding
a=ifft(x); % fft
y=[a(NFFT-G+1:NFFT),a]; % insert the guard interval

% ============================================
% OFDM Modulator
% NFFT:FFT length
% chnr:number of subcarrier
% G:guard length
% N_P : channel inpulse response length
% Save this program to a file name "OFDM_Demodulator.m"
% for use in the main function
% ============================================

function[y]=OFDM_Demulator(data,chnr,NFFT,G);
% insert the guard interval
x_remove_guard_interval=[data(G+1:NFFT+G)];
x=fft(x_remove_guard_interval);
y=x(1:chnr); % Zero removing

% ==================================
% Main function of excercise 8.1
% ==================================
clear all;
NFFT=64; %FFT length
G=9; % Guard interval length

CuuDuongThanCong.com https://fb.com/tailieudientucntt
M_ary=16; % Multilevel of M-ary symbol
t_a=50*10^(-9); % Sampling duration of HiperLAN/2
load rho.am-ascii; % load discrete multi-path channel profile
rho=[100,0.6095,0.4945,0.3940,0.2371,0.19,0.1159,0.0699,0.0462]
h=sqrt(rho);
N_P=length(rho);
H=fft([h,zeros(1,NFFT-N_P)]);
NofOFDMSymbol=100; % Number of OFDM symbol
length_data=(NofOFDMSymbol)*NFFT; % The total data length

% ------------------------
% Source bites
% ------------------------

source_data=randint(length_data,sqrt(M_ary));

% ------------------------
% bit to symbol coder
% ------------------------

symbol=bi2de(source_data);

% ------------------------
% QAM modulator in base band
% ------------------------
QAM_Symbol=dmodce(symbols,1,1,'qam',M_ary);

% ------------------------
% Preparing data pattern
% ------------------------
Data_Pattern=[];% Transmitted Signal before IFFT
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=[]; % Set the counter of symbol error ratio to be a empty vector
snr_min=0;
snr_max=25;
step=1;
for snr=snr_min:step:snr_max;
snr=snr-10*log10((NFFT+G)/NFFT);% Miss matching effect
rs_frame=[];% A Matrix of received signal
for i=0:NofOFDMSymbol-1;
% OFDM modulator
OFDM_signal_tem=OFDM_Modulator(Data_Pattern(i+1,:),NFFT,G);
% The received signal over multi-path channel is created by a
% convolutinal operation.
rs=conv(OFDM_signal_tem,h);
% Additive noise is add
rs=awgn(rs,snr,'measured','dB');
rs_frame=[rs_frame;rs];
clear OFDM_signal_tem;

CuuDuongThanCong.com https://fb.com/tailieudientucntt
end;
% ------------------------
% Receiver
% ------------------------
Receiver_Data=[];% Prepare a matrix for received data symbols
d=[]; % Demodulated symbols
data_symbol=[];
for i=1:NofOFDMSymbol;
if(N_P>G+1)&(i>1)
% If it is not first symbol and the length of CIR is longer than
% the guard interval length,then the ISI term must be taken into
% accout
previous_symbol=rs_frame(i-1,:);
% The previous OFDM symbol
ISI_term=previous_symbol(NFFT+2*G+1:NFFT+G+N_P+1);
% Position from NFFT+2G+1:NFFT+G+N_P-1 is ISI term
ISI=[ISI_term,zeros(1,length(previous_symbol)-length(ISI_term))];
rs_i=rs_frame(i,:)+ISI;
% the ISI term is added to the current OFDM symbol
else
rs_i=rs_frame(i,:);
end;

% ------------------------
% OFDM Demodulator
% ------------------------
Demolated_signal_i=OFDM_Demodulator(rs_i,NFFT,NFFT,G);

% ------------------------
% OFDM Equalization
% ------------------------
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';
% Calculation of error symbols
[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');

Exercise 8.3

% ====================================================================
% Monte Carlo method for a time varriant channel modelling
% save this program to a file name "MGM_channel_model.m"
% for use in the main function
% ====================================================================

CuuDuongThanCong.com https://fb.com/tailieudientucntt
function
[h,t_next]=MCM_channel_model(u,initial_time,number_of_summations,symbol_durat
ion,f_dmax,channel_coefficients);
t=initial_time;
Channel_Length=length(channel_coefficients);
h_vector=[];
for k=1:Channel_Length;
u_k=u(k,:);% A random variable
phi=2*pi*u_k;%Phase coefficients are created
f_d=f_dmax*sin(2*pi*u_k);% Doppler frequency after Monte Carlo method is
created

h_tem=channel_coefficients(k)*1/(sqrt(number_of_summations))*sum(exp(j*phi).*
exp(j*2*pi*f_d*t));
h_vector=[h_vector,h_tem];
end;
h=h_vector;
t_next=initial_time+symbol_duration; % Coherent time for the next symbol

% ======================================================================
% Main function : OFDM system over a time-variant channel
% ======================================================================

clear all;
NFFT=64; % FFT length
G=9;% Guard interval length
M_ary=16;% Multilevel of M-ary symbol
t_a=50*10^(-9);% Sampling duration of HiperLAN/2
load rho.am-ascii;% Load discrete multi-path channel profile
N_P=length(rho);% Length of channel impulse response

% ======================================================================
% Parameters for Monte Carlo channel
% ======================================================================

symbol_duration=NFFT*t_a;% OFDM symbol duration


number_of_summations=40;% Number of summations for Monte Carlo method
f_dmax=50.0;% Maximum Doppler frequency in Hz
NofOFDMSymbol=1000;% Number of OFDM symbols
length_data=(NofOFDMSymbol)*NFFT;%The total data length

% ======================================================================
% Source bites
% ======================================================================

source_data=randint(length_data,sqrt(M_ary));

% ======================================================================
% bit to symbol coder
% ======================================================================

symbol=bi2de(source_data);

CuuDuongThanCong.com https://fb.com/tailieudientucntt
% ======================================================================
% QAM modulator in base band
% ======================================================================

QAM_Symbol=dmodce(symbols,1,1,'qam',M_ary);

% ======================================================================
% Preparing data pattern
% ======================================================================

Data_Pattern=[]; % Transmitted Signal before IFFT


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;
Number_Relz=50;
ser_relz[];
for number_of_relialization=1:Number_Relz;
u=rand(N_P,number_of_summations);
% A random variable for Monte Carlo method
ser=[]; % Set the counter of symbol error ratio to be a empty vector
snr_min=0;% minimum of SNR in dB
snr_max=25;% maximum of SNR in dB
step=1;
for snr=snr_min:step:snr_max;
snr=snr-10*log10((NFFT+G)/NFFT);
% Miss matching effect caused by using guard interval
rs_frame=[];% A matrix of received signal
h_frame=[];% A matrix of the CIR
initial_time=0;% initial time for time-variant channel modelling
for i=0:NofOFDMSymbol-1;
% OFDM Modulator
OFDM_signal_tem=OFDM_Modulator(Data_Pattern(i+1,:),NFFT,G);
[h,t]=MCM_channel_mode(u,initial_time,number_of_summations,symbol_duration,f_
dmax,rho);
% Monte Carlo channel modelling
h_frame=[h_frame;h];
% The received signal over multi-path channel is created by a convolutional
% operation
rs=conv(OFDM_signal_tem,h);
% Additive noise is added
rs=awgn(rs,snr,'measured','dB');
rs_frame=[rs_frame;rs];
clear OFDM_signal_tem;
end;

% ======================================================================
% Receiver
% ======================================================================

Receiver_Data=[];% Prepare a matrix for received data symbols

CuuDuongThanCong.com https://fb.com/tailieudientucntt
d=[];% Equalized symbols
data_symbol=[];% a vector of demodulator symbols
for i=1:NofOFDMSymbol;
if(N_P>G+1)&(i>1)
% If it is not the first symbol and the length of CIR is longer than
the guard interval length,then the ISI term must be taken into acount
previous_symbol=rs_frame(i-1,:);
% previous OFDM symbol
ISI_term=previous_symbol(NFFT+2*G+1:NFFT+G+N_P+1);
% the position from NFFT+2G+1:NFFT+G+N_P-1 is ISI term
ISI=[ISI_term,zeros(1,length(previous_symbol)-length(ISI_term))];
rs_i=rs_frame(i,:)+ISI;
% the ISI term is added to the current OFDM symbol
else
rs_i=rs_frame(i,:);
end;

% ======================================================================
% OFDM Demodulator
% ======================================================================

Demolated_signal_i=OFDM_Demodulator(rs_i,NFFT,NFFT,G);

% ======================================================================
% OFDM Equalization
% ======================================================================

h=h_frame(i,:);% CIR corresponds to i-th OFDM symbol


H=fft([h,zeros(1,NFFT-N_P)]);
% corresponding channel transfer function
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';
% Calculation of error symbols
[number,ratio]=symerr(symbols,data_symbol);
ser=[ser,ratio];
end;
ser_relz=[ser_relz;ser];
end;
ser=sum(ser_relz)/Number_Relz;
snr=snr_min:step:snr_max;
semilogy(snr,ser,'bo');
ylabel('SER');
xlabel('SNR in dB');

CuuDuongThanCong.com https://fb.com/tailieudientucntt

You might also like