0% found this document useful (0 votes)
2 views

Lab 2

DSP-HCMUT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab 2

DSP-HCMUT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

HO CHI MINH CITY UNIVRSITY OF

TECHNOLOGY

SUBJECT: DIGITAL SIGNAL PROCESSING


MATLAB TUTORIAL LAB REPORT

Matlab Extra Tutorial Lab 2 Report

Student’s name: Võ Hoàng Phúc


Student’s ID: 2151029
Number: 17 – Sheet 2
Theory class: TT02 – 4PM Friday class
Lab class: TT03 – group 7
Report Extra Tutorial Lab 2
Student’s name: Võ Hoàng Phúc – Student’s ID: 2151029

DIGITAL SIGNAL PROCESSING LAB

1. Question 1

a.
- Method
Using the delay property of the z-transform, from the given difference equation, we
have:
( 1−0.95 z −1−0.35 z−2+ 0.462 z −3 −0.351 z −4 ) Y ( z ) =( 0.5−0.75 z−1−1.2 z −2 +0.4 z−3−1.2 z−4 ) X (z )
The delay property:

To find the impulse response h(n), we have to find H(z) = Y(z)/X(z) first, then apply
the inverse z-transform on H(z).
- Code
% The range of n
n = 0:100;
% H(z) = Y(z)/X(z)
syms z
H_z_Num = 0.5 - 0.75*z^(-1) - 1.2*z^(-2) + 0.4*z^(-3) -
1.2*z^(-4)
H_z_Denom = 1 - 0.95*z^(-1) - 0.35*z^(-2) + 0.462*z^(-3)
- 0.351*z^(-4)
H_z = H_z_Num/H_z_Denom
% Apply the inverse z-transform on H(z) to get h(n) - a
function of n
h_n = iztrans(H_z)
% Subtitute the values of n into h(n)
h_n = double(subs(h_n,sym('n'),n));
% Plotting
Report Extra Tutorial Lab 2
Student’s name: Võ Hoàng Phúc – Student’s ID: 2151029

plot(n,h_n,'b')
title('Question 1a');
xlabel('n');
ylabel('h(n)');
grid on;

- Matlab simulation

b.
- Method
Zeros and poles are the roots of the numerator and the denominator of H(z)
respectively.
- Code
% Define the coefficients of the numerator and the
denominator of H(z)
H_z_Num = [0.5 -0.75 -1.2 0.4 -1.2];
H_z_Denom = [1 -0.95 -0.35 0.462 -0.351];
% Solve the numerator for zeros and solve the denominator
for poles
Zeros = roots(H_z_Num)
Report Extra Tutorial Lab 2
Student’s name: Võ Hoàng Phúc – Student’s ID: 2151029

Poles = roots(H_z_Denom)
% Plot the pole-zero diagram
zplane(Zeros,Poles);
title('Question 1b');
xlabel('Real axis');
ylabel('Imaginary axis');
legend('Zeros','Poles');
grid on;
After executing the code, we get the values for zeros and poles:
Report Extra Tutorial Lab 2
Student’s name: Võ Hoàng Phúc – Student’s ID: 2151029

- Matlab simulation

c.
- Method
Use the Matlab built-in function named filter, the description of filter:
y = filter(b,a,x) filters the input data, x, using a rational transfer function defined by
the numerator and denominator coefficients b and a, respectively.
In this case, we subtitute b and a as the numerator and denominator coefficients of
H(z).
- Code
syms n;
% Define the function
x_n = (n + 1)^2 * (0.8^n) * (n >= 0);
% Generate values for n and evaluate the function
n = 0:100;
x_n_values = double(subs(x_n,sym('n'),n));
% Plot the original signal
plot(n,x_n_values,'b');
Report Extra Tutorial Lab 2
Student’s name: Võ Hoàng Phúc – Student’s ID: 2151029

hold on;
% Filter the signal
Poly_Num = [0.5 -0.75 -1.2 0.4 -1.2];
Poly_Denom = [1 -0.95 -0.35 0.462 -0.351];
y_n_values = filter(Poly_Num,Poly_Denom,x_n_values);
% Plot the filtered signal
plot(n,y_n_values,'r--');
title('Question 1c');
xlabel('n');
ylabel('Amplitude');
legend('Input', 'Output');
grid on;

- Matlab simulation

2. Question 2
Report Extra Tutorial Lab 2
Student’s name: Võ Hoàng Phúc – Student’s ID: 2151029

a.
- Method
Firstly, we generate the random white noise as described using rand function:
In general, you can generate N random numbers in the interval (a,b) with the
formula r = a + (b-a).*rand(N,1)
Secondly, compute the x(n)
Finally, using the Matlab built-in function to compute the zero state response y(n), as
mentioned in Question 1c
y = filter(b,a,x) filters the input data, x, using a rational transfer function defined by
the numerator and denominator coefficients b and a, respectively.
In this case b = 1/10 * ones(1, 10) and a = 1 due to running average filter y(n)
- Code
% Define the range and length
n = 0:100;
lower_bound = -0.1;
upper_bound = 0.1;
% Generate random white noise uniformly distributed over
[-0.1, 0.1]
v_n = lower_bound + (upper_bound - lower_bound) .*
rand(length(n), 1);
v_n = v_n';
% Generate x(n)
x_n = exp(-n/20) .* cos(pi*n/10) .* (n >= 0) + v_n;
% Define the filter coefficients
h = 1/10 * ones(1, 10);
% Use the filter function to generate y(n)
y_n = filter(h, 1, x_n);

% Plot the results


figure(1)
Report Extra Tutorial Lab 2
Student’s name: Võ Hoàng Phúc – Student’s ID: 2151029

plot(n, x_n, 'b');


hold on
plot(n, y_n, 'r--');
title('Question 2a');
xlabel('n');
ylabel('Amplitude');
legend('x(n)','y(n)')
grid on;
hold off

- Matlab simulation

b.
- Method
Instead of using filter function to compute the y(n), in this part we will use the conv
function. After the simulation, we can see that y(n) generated by the conv function
has a wider range compared to y(n) generated by the filter function.
- Code
Report Extra Tutorial Lab 2
Student’s name: Võ Hoàng Phúc – Student’s ID: 2151029

% Use the conv function to generate y(n)


y_n_conv = conv(x_n, h);
n_conv = 0:length(y_n_conv)-1;
% Plot the results
figure(2)
plot(n, x_n, 'b');
hold on
plot(n, y_n, 'r--');
plot(n_conv, y_n_conv, 'k:','Linewidth',1.2);
title('Question 2b');
xlabel('n');
ylabel('Amplitude');
legend('x(n)','y(n) filter','y(n) conv')
grid on;
hold off

- Matlab simulation
Report Extra Tutorial Lab 2
Student’s name: Võ Hoàng Phúc – Student’s ID: 2151029

3. Question 3

a.
- Method
Using the delay property of the z-transform, from the given difference equation, we
have:
( 1−1.76 z −1+1.1829 z−2−0.278 z−3 ) Y ( z ) =( 0. 0181+0. 0543 z−1+ 0.0543 z −2+0 .0181 z−3 ) X (z )
The delay property:

To find the impulse response h(n), we have to find H(z) = Y(z)/X(z) first, then apply
the inverse z-transform on H(z).

- Code
% The range of n
n = 0:100;
% H(z) = Y(z)/X(z)
syms z
H_z_Num = 0.0181 + 0.0543*z^(-1) + 0.0543*z^(-2) +
0.0181*z^(-3)
H_z_Denom = 1 - 1.76*z^(-1) + 1.1829*z^(-2) - 0.278*z^(-
3)
H_z = H_z_Num/H_z_Denom
% Apply the inverse z-transform on H(z) to get h(n) - a
function of n
h_n = iztrans(H_z)
% Subtitute the values of n into h(n)
h_n = double(subs(h_n,sym('n'),n));
% Plotting
plot(n,h_n,'b')
title('Question 3a');
xlabel('n');
ylabel('h(n)');
grid on;
Report Extra Tutorial Lab 2
Student’s name: Võ Hoàng Phúc – Student’s ID: 2151029

- Matlab simulation

b.
- Method
Zeros and poles are the roots of the numerator and the denominator of H(z)
respectively.
- Code
% Define the coefficients of the numerator and the
denominator of H(z)
H_z_Num = [0.0181 0.0543 0.0543 0.0181];
H_z_Denom = [1 -1.76 1.1829 -0.278];
% Solve the numerator for zeros and solve the denominator
for poles
Zeros = roots(H_z_Num)
Poles = roots(H_z_Denom)
% Plot the pole-zero diagram
zplane(Zeros,Poles);
title('Question 3b');
xlabel('Real axis');
ylabel('Imaginary axis');
legend('Zeros','Poles');
Report Extra Tutorial Lab 2
Student’s name: Võ Hoàng Phúc – Student’s ID: 2151029

grid on;
After executing the code, we get the values for zeros and poles:

- Matlab simulation
Report Extra Tutorial Lab 2
Student’s name: Võ Hoàng Phúc – Student’s ID: 2151029

c.
- Method
To plot the magnitude and phase response of this filter, we use the Matlab built-in
function named fvtool with the syntax:
fvtool(b,a,’analysis’,’magnitude’) for the magnitude response
fvtool(b,a,’analysis’,’phase’) for the phase response
With b, a are the coefficients of the numerator and denominator of H(z) respectively.
- Code
% Filter coefficients
H_z_Num = [0.0181 0.0543 0.0543 0.0181];
H_z_Denom = [1 -1.76 1.1829 -0.278];

% Plot magnitude response


fvtool(H_z_Num,H_z_Denom,'Analysis','magnitude');
title('Magnitude Response')

% Plot phase response


fvtool(H_z_Num,H_z_Denom,'Analysis','phase');
title('Phase Response')

- Matlab simulation
Report Extra Tutorial Lab 2
Student’s name: Võ Hoàng Phúc – Student’s ID: 2151029

Based on the magnitude response, we can conclude that this filter is a lowpass filter

You might also like