PHỤ LỤC Code Matlab % MATLAB script for Problem 1.
Trang 1PHỤ LỤC Code Matlab
% MATLAB script for Problem 1
clear
SNRindB1=0:1:12;SNRindB2=0:0.1:12;
for i=1:length(SNRindB1),
smld_err_prb(i)=mophong1(SNRindB1(i));
end;
for i=1:length(SNRindB2),
SNR=exp(SNRindB2(i)*log(10)/10);
theo_err_prb(i)=Qfunct(sqrt(SNR));
end;
function [p]=mophong1(snr_in_dB)
E=1; SNR=exp(snr_in_dB*log(10)/10); sgma=E/sqrt(2*SNR); N=10000; for i=1:N,
temp=rand;
if (temp<0.5),
dsource(i)=0
else
dsource(i)=1;
end
end;
numoferr=0;
for i=1:N,
if (dsource(i)==0),
r0=E+gngauss(sgma);
r1=gngauss(sgma);
else
r0=gngauss(sgma);
r1=E+gngauss(sgma);
end;
if (r0>r1),
decis=0;
else
decis=1;
end;
if (decis~=dsource(i)),
numoferr=numoferr+1;
end;
end;
p=numoferr/N;
% MATLAB script for Problem 2
clear
SNRindB1=0:1:10; SNRindB2=0:1:10;
for i=1:length(SNRindB1),
smld_err_prb(i)=mophong2(SNRindB1(i));
end;
for i=1:length(SNRindB2),
SNR=exp(SNRindB2(i)*log(10)/10);
Trang 2
theo_err_prb(i)=Qfunct(sqrt(2*SNR));
end;
function [p]=mophong2(snr_in_dB)
E=1; SNR=exp(snr_in_dB*log(10)/10); sgma=E/sqrt(2*SNR); N=10000;
for i=1:N,
temp=rand;
if (temp<0.5),
dsource(i)=0;
else
dsource(i)=1;
end
end;
numoferr=0;
for i=1:N,
if (dsource(i)==0),
r=-E+gngauss(sgma);
else
r=E+gngauss(sgma);
end;
if (r<0),
decis=0;
else
decis=1;
end;
if (decis~=dsource(i)),
numoferr=numoferr+1;
end;
end;
p=numoferr/N;
% MATLAB script for Problem 3
clear
SNRindB1=0:1:12;SNRindB2=0:0.1:12;
for i=1:length(SNRindB1),
smld_err_prb(i)=mophong3(SNRindB1(i));
end;
for i=1:length(SNRindB2),
SNR=exp(SNRindB2(i)*log(10)/10);
theo_err_prb(i)=Qfunct(sqrt(SNR/2));
end;
function [p]=mophong3(snr_in_dB)
E=1;alpha_opt=1/2;SNR=exp(snr_in_dB*log(10)/10); sgma=E/sqrt(2*SNR);N=10000; for i=1:N,
temp=rand;
if (temp<0.5),
dsource(i)=0;
else
dsource(i)=1;
end
end;
numoferr=0;
for i=1:N,
if (dsource(i)==0),
r=gngauss(sgma);
else
Trang 3r=E+gngauss(sgma);
end;
if (r<alpha_opt),
decis=0;
else
decis=1;
end;
if (decis~=dsource(i)),
numoferr=numoferr+1;
end;
end;
p=numoferr/N;
% MATLAB script for Problem 4
clear
n0=.5*randn(100,1); n1=.5*randn(100,1); n2=.5*randn(100,1); n3=.5*randn(100,1); x1=1.+n0; y1=n1;
x2=n2; y2=1.+n3;
% MATLAB script for Problem 5
clear
SNRindB1=0:1:20; SNRindB2=5:0.1:20; M=16;
for i=1:length(SNRindB1),
smld_err_prb(i)=mophong5(SNRindB1(i));
echo off;
end;
for i=1:length(SNRindB2),
SNR_per_bit=exp(SNRindB2(i)*log(10)/10);
theo_err_prb(i)=(2*(M-1)/M)*Qfunct(sqrt((6*log2(M)/(M^2-1))*SNR_per_bit)); echo off;
end;
function [p]=mophong5(snr_in_dB)
M=16; d=1; SNR=exp(snr_in_dB*log(10)/10); sgma=sqrt((85*d^2)/(8*SNR)); N=10000; for i=1:N,
temp=rand;
index=floor(M*temp);
dsource(i)=index;
end;
numoferr=0;
for i=1:N,
r=(2*dsource(i)-M+1)*d+gngauss(sgma);
if (r>(M-2)*d),
decis=15; elseif (r>(M-4)*d),decis=14;
elseif (r>(M-6)*d),
decis=13; elseif (r>(M-8)*d), decis=12;
elseif (r>(M-10)*d),
decis=11; elseif (r>(M-12)*d), decis=10;
elseif (r>(M-14)*d),
decis=9; elseif (r>(M-16)*d), decis=8;
elseif (r>(M-18)*d),
decis=7; elseif (r>(M-20)*d),decis=6;
elseif (r>(M-22)*d),
decis=5; elseif (r>(M-24)*d),decis=4;
Trang 4elseif (r>(M-26)*d),
decis=3; elseif (r>(M-28)*d), decis=2;
elseif (r>(M-30)*d), decis=1;
else
decis=0;
end;
if (decis~=dsource(i)),
numoferr=numoferr+1;
end;
end;
p=numoferr/N;
% MATLAB script for Problem 6
M=4; initial_snr=0; final_snr=10; snr_step=1; tolerance=1e-7;minus_inf=-20; plus_inf=20; snr_in_dB=initial_snr:snr_step:final_snr;
SNRindB1=0:2:10; SNRindB2=5:0.1:20;
for i=1:length(SNRindB1),
smld_err_prb(i)=smldp510(SNRindB1(i));
end;
for i=1:length(snr_in_dB),
SNR_per_bit=exp(snr_in_dB(i)*log(10)/10);
snr=10^(snr_in_dB(i)/10);
theo_err_prb(i)=(2*(M-1)/M)*Qfunct(sqrt((6*log2(M)/(M^2-1))*SNR_per_bit)); end;
function [p]=mophong6(snr_in_dB)
M=4; E=1; SNR=exp(snr_in_dB*log(10)/10); sgma=sqrt(E^2/(4*SNR));N=10000; for i=1:N,
temp=rand;
if (temp<0.25),
dsource1(i)=0; dsource2(i)=0;
elseif (temp<0.5),
dsource1(i)=0; dsource2(i)=1;
elseif (temp<0.75),
dsource1(i)=1; dsource2(i)=0;
else
dsource1(i)=1; dsource2(i)=1;
end
end;
numoferr=0;
for i=1:N,
if ((dsource1(i)==0) & (dsource2(i)==0)),
r0=sqrt(E)+gngauss(sgma); r1=gngauss(sgma); r2=gngauss(sgma); r3=gngauss(sgma); elseif ((dsource1(i)==0) & (dsource2(i)==1)),
r0=gngauss(sgma); r1=sqrt(E)+gngauss(sgma); r2=gngauss(sgma); r3=gngauss(sgma); elseif ((dsource1(i)==1) & (dsource2(i)==0)),
r0=gngauss(sgma); r1=gngauss(sgma); r2=sqrt(E)+gngauss(sgma); r3=gngauss(sgma); else
r0=gngauss(sgma); r1=gngauss(sgma); r2=gngauss(sgma); r3=sqrt(E)+gngauss(sgma); end;
max_r=max([r0 r1 r2 r3]);
if (r0==max_r),
decis1=0; decis2=0;
elseif (r1==max_r),
decis1=0; decis2=1;
elseif (r2==max_r),
Trang 5decis1=1; decis2=0;
else
decis1=1; decis2=1;
end;
if (decis1~=dsource1(i)),
numoferr=numoferr+1;
end;
if (decis2~=dsource2(i)),
numoferr=numoferr+1;
end;
end;
p=numoferr/(2*N);
% MATLAB script for Problem 7
clear
initial_snr=0; final_snr=12; snr_step=0.75; tolerance=eps; plus_inf=20; snr_in_dB=initial_snr:snr_step:final_snr; SNRindB=0:2:10;
for i=1:length(SNRindB),
smld_err_prb(i)=mophong7(SNRindB(i));
end;
for i=1:length(snr_in_dB),
snr=10^(snr_in_dB(i)/10);
theo_err_prb(i)=1-quad8('bdt_int2',0,plus_inf,tolerance,[],snr,4);
end;
function [p]=smldP511(snr_in_dB)
M=4;E=1;SNR=exp(snr_in_dB*log(10)/10); sgma=sqrt(E^2/(4*SNR)); N=10000; for i=1:N,
temp=rand;
if (temp<0.25),
dsource(i)=0;
elseif (temp<0.5),
dsource(i)=1;
elseif (temp<0.75),
dsource(i)=2;
else
dsource(i)=3;
end
end;
numoferr=0;
for i=1:N,
if (dsource(i)==0)
r0=sqrt(E)+gngauss(sgma); r1=gngauss(sgma);
elseif (dsource(i)==1)
r0=gngauss(sgma); r1=sqrt(E)+gngauss(sgma);
elseif (dsource(i)==2)
r0=-sqrt(E)+gngauss(sgma); r1=gngauss(sgma);
else
r0=gngauss(sgma); r1=-sqrt(E)+gngauss(sgma);
end;
if (r0>abs(r1)),
decis=0; elseif (r1>abs(r0)),
decis=1;
elseif (r0<-abs(r1)),
Trang 6decis=2; else
decis=3;
end;
if (decis~=dsource(i)),
numoferr=numoferr+1;
end;
end;
p=numoferr/N;
% MATLAB script for Problem 8
clear
f_cutoff=2000; f_stopband=2500; fs=10000; f1=2*f_cutoff/fs; f2=2*f_stopband/fs; N=41; F=[0 f1 f2 1]; M=[1 1 0 0]; B=remez(N-1,F,M);
[H,W]=freqz(B);
H_in_dB=20*log10(abs(H));
pause % Press a key to see magnitude of H in dB
figure(1);
plot(W/(2*pi),H_in_dB,'LineWidth',1.5);
xlabel('f','FontSize',12);
ylabel('Magnitude of H in dB','FontSize',12);
title('DAP UNG TAN SO CUA BO LOC FIR','FontSize',10);
pause % Press a key to see phase of H
figure(2);
plot(W/(2*pi),(180/pi)*unwrap(angle(H)));
xlabel('f','FontSize',12);
title('DAP UNG PHA CUA BO LOC FIR','FontSize',10);
pause % Press a key to see the impulse response
figure(3);
plot(zeros(size([0:N-1])));
xlabel('n','FontSize',12);
ylabel('Magnitude of H in dB','FontSize',12);
title('DAP UNG XUNG CUA BO LOC FIR','FontSize',10);
hold;
stem([0:N-1],B);
% MATLAB script for Problem 9
clear
p=0.99; N=1000; d=5; A=[1 -2*p p^2]; B=(1-p)^2;
for i=1:N,
[white_noise_seq1(i) white_noise_seq2(i)] = gngauss;
end;
b1=filter(B,A,white_noise_seq1); b2=filter(B,A,white_noise_seq2); c=b1(d+1:N) +b2(1:N-d);
p=0.9; A=[1 -2*p p^2];
B=(1-p)^2;
for i=1:N,
[white_noise_seq1(i) white_noise_seq2(i)] = gngauss;
end;
new_b1=filter(B,A,white_noise_seq1); new_b2=filter(B,A,white_noise_seq2);
new_c=new_b1(d+1:N)+new_b2(1:N-d);
% MATLAB script for Problem 10
clear
N=52; noise_var=0.25; sigma=sqrt(noise_var);
for i=1:N,
Trang 7if (rand<0.5),
I(i)=0;
else
I(i)=1;
end;
end;
% for channel 1:
A=1;B=[0.1 -0.25 1 -0.25 0.1];
rec_sig1=filter(B,A,I); rec_sig1=rec_sig1([3:N]);
for i=1:N-2,
noise(i)=gngauss(sigma);
end;
y1=rec_sig1+noise;
% for channel 2:
A=1;B=[-0.2 0.5 1 0.5 -0.2];
rec_sig2=filter(B,A,I); rec_sig2=rec_sig2(3:N);
for i=1:N-2,
noise(i)=gngauss(sigma);
end;
y2=rec_sig2+noise;
% MATLAB script for Problem 11
clear
N=31; T=1; alpha=1/4; n=-(N-1)/2:(N-1)/2;
for i=1:length(n),
g_T(i)=0;
for m=-(N-1)/2:(N-1)/2,
g_T(i)=g_T(i)+sqrt(xrc(4*m/(N*T),alpha,T))*exp(j*2*pi*m*n(i)/N);
end;
end;
g_T=real(g_T) ;
n2=0:N-1;
[G_T,W]=freqz(g_T,1);
magG_T_in_dB=20*log10(abs(G_T)/max(abs(G_T)));
g_R=g_T;
imp_resp_of_cascade=conv(g_R,g_T);
H_in_dB=20*log10(abs(G_T));
pause
figure(1);
plot(W/(2*pi),magG_T_in_dB);
xlabel('f','FontSize',12);
ylabel('Normalized Magnitude','FontSize',12);
title('DAP UNG TAN SO CUA BO LOC PHAT FIR','FontSize',10);
% MATLAB script for Problem 12
clear
N=31; T=1; W=1/(2*T); n=-(N-1)/2:(N-1)/2;
for i=1:length(n),
g_T(i)=0;
for m=-(N-1)/2:(N-1)/2,
if ( abs((4*m)/(N*T)) <= W ),
g_T(i)=g_T(i)+sqrt((1/W)*cos((2*pi*m)/(N*T*W)))*exp(j*2*pi*m*n(i)/N); end;
end;
end;
Trang 8g_T=real(g_T) ; n2=0:N-1; [G_T,W]=freqz(g_T,1);
magG_T_in_dB=20*log10(abs(G_T)/max(abs(G_T)));
g_R=g_T;
imp_resp_of_cascade=conv(g_R,g_T);
% MATLAB script for Problem 13
clear
T=1; Fs=2/T; Ts=1/Fs; c_opt=[-2.2 4.9 -3 4.9 -2.2]; t=-5*T:T/2:5*T; t1=[0:T:20*T]; t2=[0:T:10*T]; x=1./(1+((2/T)*t).^2);
equalized_x=filter(c_opt,1,[x 0 0]);
equalized_x=equalized_x(3:length(equalized_x));
for i=1:2:length(equalized_x),
downsampled_equalizer_output((i+1)/2)=equalized_x(i);
end;
pause
figure(1);
stem(t,x);
title('XUNG DAU VAO EQUALIZER','FontSize',10);
pause
figure(2);
stem(t2,downsampled_equalizer_output);
title('XUNG DAU RA EQUALIZER','FontSize',10);
% MATLAB script for Problem 14
clear
T=1;
for n=-2:2,
for k=-2:2,
temp=0;
for i=-2:2, temp=temp+(1/(1+(n-i)^2))*(1/(1+(k-i)^2));
end;
X(k+3,n+3)=temp;
end;
end;
N0=0.01; Ry=X+(N0/2)*eye(5);
Riy=[1/5 1/2 1 1/2 1/5].';
c_opt=inv(Ry)*Riy;
t=-3:1/2:3;
x=1./(1+(2*t/T).^2);
equalized_pulse=conv(x,c_opt);
decimated_equalized_pulse=equalized_pulse(1:2:length(equalized_pulse)); t1=[-4:1:4];
stem(t1,decimated_equalized_pulse);
title('XUNG DAU RA EQUALIZER MMSE','FontSize',10);
% MATLAB script for Problem 15
clear
N=500; K=5;
actual_isi=[0.05 -0.063 0.088 -0.126 -0.25 0.9047 0.25 0 0.126 0.038 0.088]; sigma=0.01; delta=0.045;
Num_of_realizations=1000; mse_av3=zeros(1,N-2*K);
for j=1:Num_of_realizations,
Trang 9for i=1:N,
if (rand<0.5),
info(i)=-1;
else
info(i)=1;
end;
echo off ;
end;
if (j==1) ;
end
y=filter(actual_isi,1,info);
for i=1:2:N, [noise(i) noise(i+1)]=gngauss(sigma);
end;
y=y+noise;
estimated_c=[0 0 0 0 0 1 0 0 0 0 0];
for k=1:N-2*K,
y_k=y(k:k+2*K); z_k=estimated_c*y_k.'; e_k=info(k)-z_k; estimated_c=estimated_c+delta*e_k*y_k; mse(k)=e_k^2; end;
if (j==1) ;
end
mse_av3=mse_av3+mse;
end;
mse_av3=mse_av3/Num_of_realizations;