ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINHTRƯỜNG ĐẠI HỌC BÁCH KHOA KHOA ĐIỆN – ĐIỆN TỬ ------BÁO CÁO THÍ NGHIỆM XỬ LÝ SỐ TÍN HIỆU BÀI 1: GIỚI THIỆU MATLAB CƠ BẢN... Viết một file-M thực
Trang 1ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH
TRƯỜNG ĐẠI HỌC BÁCH KHOA KHOA ĐIỆN – ĐIỆN TỬ
- -BÁO CÁO THÍ NGHIỆM XỬ LÝ SỐ TÍN HIỆU
BÀI 1: GIỚI THIỆU MATLAB CƠ BẢN
Trang 2THỰC HÀNH 1 Viết file-M thực hiện đoạn chương trình sau
- Code Matlab:
- Kết quả mô phỏng:
clc;
clear all;
x = 0:pi/20:2*pi; % define vector x from 0 to 2pi with step size pi/100
y = sin(x); % define vector y
subplot(1,2,1); %create a graph with 2 subgraphs in 1 row 2 columns % and with subplot position 1
plot(x,y,'r-'); % plot x versus y
axis ([0 2*pi -1 1]);
xlabel ('x (pi)');
ylabel ('y=sinx');
title ('Graph of continuous sine from 0 to 2pi');
subplot(1,2,2);
stem(x,y,'g-');
axis([0 2*pi -1 1]);
xlabel ('x (pi)');
ylabel ('y=sinx');
title ('Graph of discrete sine from 0 to 2pi');
Trang 3HÀNH 2.
Viết một
file-M
thực hiện
chương
trình sau.
- Code
Matlab:
- Kết quả
mô phỏng:
clc;
close all;
xn = [1, 2, 3, 4, 5, 6];
L = length(xn); %find the length of the sequence
Xk = zeros(1,L); %initialize an array of same size as that of input sequence
%DFT of the sequence
for k = 0:L-1
for n = 0:L-1
Xk(k+1) = Xk(k+1) + xn(n+1)*exp(-1j*2*pi*k*n/L);
end
end
% Using FFT Matlab
Xk_2 = fft(xn,L);
%Plotting input sequence
t=0:L-1;
subplot(1,3,1);
stem(t,xn);
ylabel ('Amplitude');
xlabel ('Time Index');
title('Input Sequence');
t=0:L-1;
% Find the magnitudes of individual DFT points
Xk_magnitude = abs(Xk);
Xk_2_magnitude = abs(Xk_2);
% plot the magnitude response
subplot(1,3,2);
stem(t,Xk_magnitude,'bo-'); hold on;
stem(t,Xk_2_magnitude,'r* '); hold on;
ylabel ('Amplitude');
xlabel ('K');
title('Magnitude Response');
% Find the phases of individual DFT points
Xk_phase = angle(Xk);
Xk_2_phase = angle(Xk_2);
% plot the magnitude sequence
subplot(1,3,3);
stem(t,Xk_phase,'bo-'); hold on;
stem(t,Xk_2_phase,'r* '); hold on;
ylabel ('Phase');
xlabel ('K');
title ('Phase Response');
legend('Computing', 'fft Matlab');
Trang 4THỰC HÀNH 3 Viết chương trình thwujc hiện biến đổi IDFT của chuỗi X(k) = [10, -2+2i, -2, -2-2i] theo 2 cách.
- Code Matlab:
- Kết quả
mô phỏng:
clc;
close all;
xk = [10,-2+2j,-2,-2-2j];
L = length(xk); %find the length of the sequence
Xn = zeros(1,L); %initialize an array of same size as that of input sequence
%IDFT of the sequence
for n = 0:L-1
for k = 0:L-1
Xn(n+1) = Xn(n+1) + (1/L)*xk(k+1)*exp(1j*2*pi*k*n/L);
end
end
% Using IDFT Matlab
Xn_2 = ifft(xk,L);
stem(0:L-1,Xn,'b-');
hold on;
stem(0:L-1,Xn_2,'r* ');
Trang 5THỰC HÀNH 4 Viết một file-M tìm ngõ ra y(n) của hệ thống nhân quả với ngõ vào x(n) = [1,3,5,3,6,3] và đáp ứng xung h(n) = [1,4,7,2,8] theo 2 cách.
- Code Matlab:
clc;
clear all;
xn = [1,3,5,3,6,3];
hn = [1,4,7,2,8];
% tinh tich chap
Lx = length(xn);
N = length(hn);
M = N - 1;
Ly = Lx + M;
yn = zeros(1, Ly);
for n = 0 : Ly-1
for m = max(0,n-Lx + 1):min(n,M) yn(n+1) = yn(n+1) + hn(m+1) * xn(n-m+1);
end end
fprintf('yn = ');
disp(yn);
% dung ham conv
yn_2 = conv(xn,hn);
fprintf('\n yn_2 =');
disp(yn_2);
stem(0:Ly-1,yn,'b-');
hold on;
stem(0:Ly-1,yn_2,'r* ');
Trang 6- Kết quả mô phỏng:
Trang 7THỰC HÀNH 5 Viết một file-M vẽ đáp ứng tần số của hệ thống có hàm truyền trên theo 2 cách.
- Code Matlab:
clc;
clear all;
w = 0:pi/10:pi;
% tinh toan dap ung tan so
H = (5 + 2*exp(-1j*w))./(1-0.8*exp(-1j*w));
H_manitude = abs(H);
H_phase = angle(H);
subplot(2,2,1);
semilogy(w,H_manitude,'r-');
grid on;
title('Amplitude tinh toan');
xlabel('w (pi)');
subplot(2,2,2);
plot(w,H_phase,'b-');
grid on;
title('Phase tinh toan');
xlabel('w (pi)');
% su dung ham matlab
a = [1,-0.8];
b = [5,2];
[H_matlab,w] = freqz(b,a);
H_matlab_mantitude = abs(H_matlab);
H_matlab_phase = angle(H_matlab);
subplot(2,2,3);
semilogy(w,H_matlab_mantitude,'r-');
grid on;
xlabel('w (pi)');
title('Amplitude ham matlab');
subplot(2,2,4);
plot(w,H_matlab_phase,'b-');
grid on;
xlabel('w (pi)');
title('Phase ham matlab');
Trang 8- Kết quả mô phỏng:
Trang 9THỰC HÀNH 6_1 Viết chương trình Matlab thực hiện yêu cầu sau:
Tạo 5 chu kỳ mẫu tín hiệu s1= cos 2 ( π f t1 ) với f1= 400 Hz, tần số lẫy mẫu 8000 Hz
- Code matlab:
clc;
clear all;
% Signal Genergating
Fs = 8e3; % Sampling frequency 8 kHz
Ts = 1/Fs; % Sampling period
F_xt = 400; % Frequency of signal 300 Hz
T_xt = 1/F_xt;
t = 0 : Ts : 5*T_xt;
xn = cos(2 * pi * F_xt * t);
N = length(xn); % DFT length = signal length
Xk = fft(xn, N); % DFT of signal
Xk_Man = abs(Xk);
Xk_Pha = angle(Xk);
%% Signal plot
figure(1);
subplot(1,2,1);
hold on;
plot(t, xn);
xlabel('Time (sec)');
ylabel('Amplitude');
subplot(1,2,2);
hold on;
plot(0:N-1, xn);
xlabel('Sampling index - n');
ylabel('Amplitude');
%% Spectrum plot
figure(2);
subplot(1,2,1);
stem((0:N/2-1)*Fs/N, Xk_Man(1:N) / N);
xlabel('Frequency (Hz)');
ylabel('Amplitude');
subplot(1,2,2);
plot((0:N-1)*Fs/N, Xk_Pha(1:N) );
xlabel('Frequency (Hz)');
ylabel('Phase');
Trang 10- Kết quả mô phỏng:
Trang 11THỰC HÀNH 6_2 Tạo mẫu tín hiệu
3
0 elsewhere
=
chu kỳ T.
- Code Matlab:
clc;
clear all;
Fs = 20;
Ts = 1/Fs;
T = 1;
t = 0 : Ts : T-Ts;
xn = [0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0];
L = length(xn);
Xk = fft(xn,L);
Xk_Man = abs(Xk);
Xk_Pha = angle(Xk);
%% Signal plot
figure(1)
subplot(1,2,1)
plot(t, xn);
xlabel('Time (sec)');
ylabel('Amplitude');
subplot(1,2,2)
plot(0:L-1,xn);
xlabel('Sampling index - n');
ylabel('Amplitude');
%% Spectrum plot
figure(2)
subplot(1,2,1)
stem((0:L-1)*Fs/L, Xk_Man(1:L) / L);
xlabel('Frequency (Hz)');
ylabel('Amplitude');
subplot(1,2,2)
stem((0:L-1)*Fs/L, Xk_Pha(1:L) );
xlabel('Frequency (Hz)');
ylabel('Phase');
Trang 12- Kết quả mô phỏng:
Trang 14THỰC HÀNH 6_3 Tạo tín hiệu sinc s4 = sinc 2 ( π f t4( − 0.5 ) )
với f4 = 40 Hz, 0 ≤ < t 1 (s)
và được lấy mẫu ở tần số 500 Hz.
- Code Matlab:
clc;
clear all;
Fs = 500;
Ts = 1/Fs;
F = 40;
T_sim = 1;
t = 0:Ts:1-Ts;
xn = sinc(2 * pi * F * (t-0.5));
N = length(xn);
Xk = fft(xn, N);
Xk_Man = abs(Xk);
Xk_Pha = angle(Xk);
%% Signal plot
figure(1)
subplot(2,1,1)
plot(t, xn);
grid on;
xlabel('Time (sec)');
ylabel('Amplitude');
title('Truoc khi lay mau ');
subplot(2,1,2)
plot(0:N-1, xn,'r-');
grid on;
xlabel('Sampling index - n');
ylabel('Amplitude');
title('Sau khi lay mau');
%% Spectrum plot
figure(2)
subplot(2,1,1)
stem((0:N-1)*Fs/N, Xk_Man(1:N) / N);
xlabel('Frequency (Hz)');
ylabel('Amplitude');
title('Bien do');
subplot(2,1,2)
stem((0:N-1)*Fs/N, Xk_Pha(1:N));
xlabel('Frequency (Hz)');
ylabel('Phase');
title('Pha');
Trang 15- Kết quả mô phỏng:
Trang 16THỰC HÀNH 7 Thực hiện việc thiết kế bộ lọc FIR chắn dải với các thông số như trong ví
dụ 1 Lấy các hệ số của bộ và viết chương trình vẽ đáp ứng tần số của bộ lọc trên.
- Code Matlab:
- Kết quả
mô
phỏng:
h = bs2700.tf.num;
[H_matlab,w] = freqz(h);
H_matlab_manitude = abs(H_matlab);
H_matlab_phase = angle(H_matlab);
subplot(2,1,1);
semilogy(w,H_matlab_manitude,'r'); %plot
xlabel('w(pi)');
title('do thitheo semilogy');
subplot(2,1,2);
plot(w,H_matlab_manitude,'b-');
xlabel('w(pi)');
title('do thi plot');
Trang 17THỰC HÀNH 8 Thực hiện việc thiết kế bộ lọc IIR chắn dải với các thông số như trong ví
dụ 2 Lấy các hệ số của bộ và viết chương trình vẽ đáp ứng tần số của bộ lọc trên.
- Code Matlab :
- Kết quả
mô
phỏng :
[z,p,k] = tf2zp(bs1750.tf.num, bs1750.tf.den);
sos = zp2sos(z,p,k);
[H_matlab,w] = freqz(sos);
H_matlab_mantitude = abs(H_matlab);
H_matlab_phase = angle(H_matlab);
subplot(2,1,1);
semilogy(w,H_matlab_mantitude,'r');
xlabel('w (pi)');
subplot(2,1,2);
plot(w,H_matlab_mantitude,'b-');
xlabel('w (pi)');
Trang 18THỰC HÀNH 10 Thiết kế bộ lọc FIR chắn đa dải 1000-1500 và 2500-3000, có bậc 62, tần số lấy mẫu là 10 kHz Sau đó vẽ đáp ứng tần số của bộ lọc.
- Code Matlab:
- Kết quả
mô
phỏng :
clc;
clear all;
f = [0 0.18 0.2 0.3 0.32 0.48 0.5 0.6 0.62 1];
m = [1 1 0 0 1 1 0 0 1 1];
n = 62;
cof = remez(n-1,f,m);
% frequency response with 256 points
[h, w] = freqz(cof,1,256);
% plot magnitude of the filter
plot(f * 5000,m, 'g'); hold on;
plot(w/pi*5000,abs(h), 'r-.');
Trang 19THỰC HÀNH 11 Thiết kế bộ lọc IIR chắn đa dải 1000-1500 và 2500-3000, có bậc 62, có tần số lấy mẫu là 10 kHz Sau đó vẽ đáp ứng tần số của bộ lọc.
- Code Matlab:
- Kết quả
mô
phỏng :
clc;
clear all;
f = [0 0.18 0.2 0.3 0.32 0.48 0.5 0.6 0.62 1];
m = [1 1 0 0 1 1 0 0 1 1];
n = 62;
[num, den] = yulewalk(n-1,f,m);
% frequency response with 256 points
[h w] = freqz(num,den,256);
% plot magnitude of the filter
plot(f * 5000,m, 'g');
hold on;
plot(w/pi*5000,abs(h), 'r-.');