You are on page 1of 13

H v tn

: Hong Vn H

Lp

: TVT6 K55

Mssv

: 20101451

BI TP MATLAB
Bi 1
1.1:
t_a = 0.05;
x = -4:t_a:4;
p = (1/sqrt(2*pi))*(-x.^2/2);
check = trapz(x,p);
plot(x,p);
title('\fontsize{12}PDF of a Gaussian distributed random variable');
xlabel('x','Fontsize',12);
ylabel('P(x)','Fontsize',12);

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);
%calcuate 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;

%-----------------------------------------------------------%Generate a 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);
%calculate the history of the process y
stem(x2,c/n/(x2(2)-x2(1)));
%calculation "c/n/(x2(2)-x2(1))" is to change from the
%history diagram to the PDF
tltle('Gaussian distributed PDF');
xlabel('X');
ylabel('P(X)');
legend('theoratical','experimental');
hold off;

Bi 4.
4.1
%===============================
%Caculation of simultion parameters
%===============================
clear
f_m=91;
b=1;
N1=9;
N2=10;

%Maximal Doppler frequency


%Variance of in-phase or quadrature component
%Number of sinusoids for the in-phase component
%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 used in the exsercies 4.3 anh 4.4

4.2
%=============================
%a function which creates the deterministic
%process g(t)
%Save this funtion to a file name "g.m"
%=============================
function y=g(c,f,th,t)
y=zero(size(t));
for n=1:length(f);
y=y+c(n)*cos(2*pi*f(n).*t+th(n));
end;

4.3
%===========================================
%Creata deterministic process by Rice method
%===========================================
clear;
load ex4p1_Res f1 f2 c c2 th1 th2
f_s=270800;
T_sim=0.4;
t=0:1/f_s:T_sim;

% the carrier frequency in Hertz


% simulation time in second
% discrete time interval

g1=g(c1,f1,th1,t);
"g.m"

% generation of the process g1 by using


% from exercise 4.2

g2=g(c2,f2,th2,t);
g=g1+j*g2;
alpha=abs(g);
alpha_dB=20*log10(alpha);
plot(t,alpha_dB);
title('The channel amplitude in dB');
xlabel('t');
ylabel('\alpha(t)');
legend('\alpha(t) in dB',0);

4.4
clear;
load ex4p1_Res f1 f2 c1 c2 th1 th2
f_s=50000;
T_sim=20;
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=langht(alpha);
x=0:0.1:3;
b=hist(alpha,x);
figure(1);
stem(x,b/n/(x(2)-x(1)));
hold on;
k=0;

the function

ohm_p=2;
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)','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');
ylabel('P_{g1}(x)');
legend('p_{g1}(x)','Gaussian distribution(Theory)');
hold off;

4.5
clear;
load ex4p1_Res f1 f2 c c2 th1 th2
f_s=1000;
T_s=1/f_s;
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);
xlabel('\tau in second');
ylabel('\phi_{gIgI}(\tau)');
legend('\phi_{gIgI}(\tau) of gI');
save ex4p5_Res phi_g1g1_select f_s phi_g1g1;

4.6
clear;
f_m=91;
b=1;
N1=9;
Tau_max=0.08;
T_a=0.001;
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)=(c(n)^2/2)*cos(2*pi*f1(n).*tau);
end
phi_g1g1_theory=sum(xs);
plot(tau,fay);
hold on;
f_c=900e6;
c_0=3e8;
v=109.2e3/3600;
f_m=v*f_c/c_0;
ohm_p=2;
t=0:0.001:tau_max;
z=2*pi*f_m*t;
phi_gIgI=(ohm_p/2)*besselj(0,z);
plot(tau,phi_gIgI,'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;

4.8
clear;
fm=91;
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:N)/(N1+1);
fs=1000;
t_sim=(10E3:20E3)/fs;
g_t_tilde=zeros(size(t_sim));
for k=1:length(f_1_n);
g1_t_tilde=g_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);
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;
Omega_p=2;
f=(-fm):1:(fm);
S_f=Omega_p/2/pi/fm./sqrt(1-(f/fm).^2);
plot(f,S_f,'r-');
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);
legend('Simulation,Reference Model,Simulation Model');
xlabel('Doppler frequency, f in Hz');
ylabel('PSD,S_{g_1 g_1}(f)');

Bi 5:
5.1
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;
plot(S,'*');
hold on;
t=0:0.01:2*pi;
plot(exp(j*t),'r');
xlabel('\phi(t)');
ylabel('S_m');
title('The comples signal-space diagram for 4_QPSK');

5.2
clear;
load ex5p1_Res;
Es=var(S);
Eb=Es/2;
SNR_db=6;
N_0=Eb/10^(SNR_db/10);
N=sqrt(N_0/2)*(randn(size(s))+j*randn(size(S)));
R=S+N;
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;

Bi 6:
6.1
%========================================================================
% A Function which demodulates the QPSK symbols and counts the error bits
% and counts the error bits
%save this function to a file name "ex6_1.m"
%========================================================================
function y=ex6_1(SNR_db,S,x)
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)
d = abs(S_m-NS(i));
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)
R(2*i-1)=1;
R(2*i)=1;
elseif md==d(4)
R(2*i-1)=1;
R(2*i)=0;
end
end
c=0;
for i=1:length(x)
if R(i)~=x(i);
c=c+1;
end
end
y=c;
%=================================
%Main function of the exercisr 6.1
%=================================
clear all;
load ex5p1_Res S x;
% S:Transmitted QPSK symbols
% x:transmitted bit sequence
SNR_db=0:2:8;
% Signal to noise ratio
for i=1:length (SNR_db)
c(i)=ex6_1(SNR_db(i,S,x);
end
BEP=c/length(x);
semilogy(SNR_db,BEP,'.--')
title('The bit error probability')
xlabel('SNR in db')
ylabel('P_b')
legend('P_b')
save ex6p1_Res c BEP

6.2
%========================================================================
% Comparison of simulation result of bit error probablitity(BEP) with
% theoretical results
%========================================================================
clear;
SNR_db = 0:8;%Signal to noise ratio vector
SNR_db_simulation = 0:2:8;
for i=1:length(SNR_db)
SNR(i) = 10^(SNR_db(i)/10);
gamma_b(i)=SNR(i);

p_b(i)=erfc(sqrt(2*gamma_b(i))/aqrt(2))/2;
%BEP of QPSK
end
semilogy(SNR_db,p_b,'ro--')
hold on
load ex6p1_Res c BEP;%Simulation result fromexercire 6.1
semilogy(SNR_db_simulation,BEP,'x--')
title('The bit error probability')
xlabel('SNR in db')
ylabel('P_b')
legend('Theory','Simulation')
hold off

6.3
%========================================================================
% A function which equalizes the received symbols according a known channel
% then demodulates QPSK symbols and counts the symbols
%========================================================================
function chann_1 = receiver(SNR_db,S_m,FS,x,s,g);
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)));
NFS = (FS+N0)./g;
theta_m = [pi/4.3*pi/4,5*pi/4,7*pi/4];
S_m = exp(j*theta_m);
for i=1:length(S)
d = abs(S_m-NFS(i));
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)
R(2*i-1)=1;
R(2*i)=1;
elseif md==d(4)
R(2*i-1)=1;
R(2*i)=0;
end
end
c=0;
for i=1:length(x)
if R(i)~=x(i);
c=c+1;
end
end
chann_1=c;

%=================================
%Main function of the exercisr 6.3
%=================================
clear;
load ex5p1_Res;%result from the ecercise 5.1
S=S(1:20000);
x=x(1:40000);
%================================
%Parameters for channel simulator
%================================
f_m=91;%maximum Doppler frequency in Hz
b= 1/2;
N1=9;
N2=N1+1;
f1=f_m*sin(pi/2/N1*((1:N1)-1/2));
c1=sqrt(2*b/N1)*ones(size(f1));
th1=rand(size(f1))*2*pi;
f1=f_m*sin(pi/2/N2*((1:N2)-1/2));
c1=sqrt(2*b/N2)*ones(size(f2));
th1=rand(size(f2))*2*pi;
f_s= 270800;
T_symb=1/f_s;
t=(0:length(S)-1)*T_symb;
g1=g(c1,f1,th1,t);
g2=g(c2,f2,th2,t);
g=g1+j*g2;
FS=g.*S;
%==================================
%Parameters for reference simulator
%==================================
theta_m = [pi/4.3*pi/4,5*pi/4,7*pi/4];
S_m = exp(j*theta_m);
for i=1:length (S)/4
gS_m(4*i-3:4*i)=S_m.*g(4*i-3:4*i);
end
SNR_db=0:5:30;
for i=1:length (SNR_db)
c(i)=cha(SNR_db(i,S,x);
end
BEP=c/length(x);
semilogy(SNR_db,BEP,'.--')
title('The bit error probability of QPSK over a fading channel')
xlabel('SNR in db')
ylabel('P_b')

6.4
%=================================
%BEP of QPSK over a fading channel

%=================================
clear;
b=1/2;
SNR_db=0:5:30;
Gamma_averrage = 2*b*10^(SNR_db/10);
p_b=(1-sqrt(Gamma_average./(1+Gamma_average)))/2;
semilogy(SNR_db,BEP,'r.--')
hold on
load ex6p3_Res BEP
semilogy(SNR_db,BEP,'bo')
hold off
title('BEP of slow flat Rayleigh fading channel')
xlabel('\gamma_b')
ylabel('P_b')
legend('BEP of slow flat Rayleigh fading channel(Theory)','--(Simulation)')

Bi 7:
%==============================================================
% A function performs QPSK demodulation, Viterbi decoding,
% and calculation of bit error probability
% save this function to a file name ex7.m
%==============================================================
function y=ex7(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)
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)f[0 1]
tblen=12; % the traeback depth
r=videc(R,trellis,tblen,opmode,dectype);
% :: Calculation of Bit Error Probability ::
c=0;
for i=l:length(r)
if r(i) ~= meg(i)
c=c+l;
end
end
y=c;
%====================
% Main program of exercise 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=l+v, v=number of shift registers
codegen=[7 5];
%condegenerator g(l)=(l,l,l)->7,g(2)=(l,0,l)->5
trellis=poly2trellis(conlen,codegen);
x=convenc(meg,trellis); % the coded bit stream
% ::QPSK Modulation::
for i=l:2:length(x)
if x(i)==0 && x(i+l)==0
S((i+l)/2)=exp (j*pi/4);
elseif x(i)==0 && x(i+l)==l
S((i+l)/2)=exp(j*3*pi/4);
elseif x(i)==l && x(i+l)==l
S((i+l)/2)=exp(j*5*pi/4);
elseif x(i)==l && x(i+l)==0
S((i+l)/2)=exp(j*7*pi/4);
end
end
% S is the modulated complex signal

SNR_db=0:5;
for i=l:length(SNR_db)
c(i)=ex7(SNR_db(i),S,meg,trellis);
and
end
errors in the

% ex7 will demodulate,decode


%calculate the number of
% received signal

BEP=c/length(meg);
semilogy(SNR_db,BEP,'.')
hold on
SNR_db=0:8;
load ex6pl_Res % result from exercise 6.1
BEP2=c/length(x);
semilogy(SNR_db,BEP2,'r.')
title('The bit error probability')
xlabel('SNR in db')
ylabel('P_b')
legend('P_b with coder','P_b without coder')
hold off

You might also like