SNS Lab14
SNS Lab14
(EE-223)
LABORATORY MANUAL
Spring 2019
LAB # 14
COMPUTING Z-TRANSFORM AND DISCRETE-TIME CONVOLUTION
USING MATLAB
1
Lab # 14: COMPUTING Z-TRANSFORM AND DISCRETE-TIME
CONVOLUTION USING MATLAB
Objective: In this lab, you will learn and explore:
Finding z-transform and inverse z-transform
o ztrans and iztrans command
Partial fraction decomposition of a discrete-time transfer function using residue command
zplane command for pole zero plot
Finding frequency response of a discrete-time system using freqz command
o fvtool (num,den) command
Computing discrete-time convolution
o conv (), filter() and impz () commands
Description:
z-Transform
In mathematics and signal processing, the z-transform converts a discrete-time signal, which is
a sequence of real or complex samples, into a complex frequency-domain representation. It is defined
as follows:
Unilateral Z-Transform:
In cases where 𝑥[𝑛] is defined only for n greater than or equal to zero, the single-sided or unilateral z-
transform is defined as
z-Transform in MATLAB
ztrans:
ztrans(f,n,z) computes the Z transform of the expression 𝑓 = 𝑓(𝑛) with respect to the index 𝑛 at the
point 𝑧.
Examples:
Code:
syms n
f = sin(n)
ztrans(f)
2
MATLAB Command:
iztrans:
iztrans(f,z,n) computes the inverse z-transform of the expression 𝐹 = 𝐹(𝑧) with respect to the
variable 𝑧 at the point 𝑛.
Example 1:
Code:
syms z
F = (z*sin(1))/(z^2 - 2*cos(1)*z + 1)
iztrans(F)
ans = sin(n)
The inputs to residue are vectors of coefficients of the polynomials 𝑏 = [𝑏𝑚 . . . 𝑏1 𝑏0 ] and
𝑎 = [𝑎𝑛 . . . 𝑎1 𝑎0 ]. The outputs are the residues 𝑟 = [𝑟𝑛 . . . 𝑟2 𝑟1], the poles 𝑝 = [𝑝𝑛 . . . 𝑝2 𝑝1 ], and
the constant k. For most textbook problems, 𝑘 is 0 or a constant.
Example 2:
Find the partial fraction expansion of the following transfer function 𝐻(𝑧) using residue
𝐵(𝑧) −4𝑧 + 8
𝐻(𝑧) = = 2
𝐴(𝑧) 𝑧 + 6𝑧 + 8
b = [-4 8];
a = [1 6 8];
[r,p,k] = residue(b,a)
-12
8
p = 2×1
-4
-2
k = []
𝑟
This represents the partial fraction expansion. We can write it in the form of (𝑧−𝑝) as follows:
−4𝑧 + 8 12 8
2
= − +
(𝑧 + 6𝑧 + 8) (𝑧 + 4) (𝑧 + 2)
3
zplane command in MATLAB:
zplane(z,p) plots the zeros specified in column vector z and the poles specified in column vector p in
the current figure window. The symbol 'o' represents a zero and the symbol 'x' represents a pole. The
plot includes the unit circle for reference. If z and p are matrices, then zplane plots the poles and zeros
in the columns of z and p in different colors. zplane(b,a), where b and a are row vectors, first
uses roots to find the zeros and poles of the transfer function represented by the numerator
coefficients b and the denominator coefficients a.
Stability of system:
A discrete-time linear time-invariant (LTI) system is said to be stable if all poles lie inside the unit
circle.
freqz(b,a) returns the frequency response using transfer function coefficients b and a.
fvtool Command:
fvtool is a graphical user interface (GUI) that allows you to analyze digital filters.
fvtool(B,A) launches the filter visualization tool and computes the magnitude response for the filter
defined by numerator and denominator coefficients in vectors B and A.
Note: -You can visualize all system plots like magnitude and phase responses, impulse and step
responses, group delay and phase delay plotd, pole zero plot etc.
fvtool([1 3/4],[1 0 -1/4])
4
impz() Command:-
impz command computes impulse response of digital filter
[h,n] = impz(b,a) computes the impulse response of the filter, where a and b are coefficients of
difference equation, while h and n are outputs of impz(), where h contains the result of impulse
response and n contains the indices.
filter() Command:-
filter() command applies one-dimensional digital filter.
Y = filter(B,A,X) filters the data in vector X with the filter described by vectors A and B to create the
filtered data Y. The filter implements the input signal X using the standard difference equation:
𝑎1 𝑦[𝑛] = 𝑏1 𝑥[𝑛] + 𝑏2 𝑥[𝑛 − 1] + . . . + 𝑏𝑛𝑏+1 𝑥[𝑛 − 𝑛𝑏 ] − 𝑎2 𝑦[𝑛 − 1] − . . . − 𝑎𝑛𝑎+1 𝑦[𝑛 − 𝑛𝑎 ]
If 𝑎1 is not equal to 1, filter normalizes the filter coefficients by 𝑎1
conv() Command:-
conv() command computes the convolution and polynomial multiplication.
C = conv(A, B) convolves vectors A and B. The resulting vector is of length length(A)+length(B)-1. If A
and B are vectors of polynomial coefficients, convolving them is equivalent to multiplying the two
polynomials.
Note:- impz() , filter() and conv() results in the same output only if 𝑥[𝑛] is unit impulse sequence, but
if 𝑥[𝑛] is other than impulse sequence, then impz is used to find impulse response ℎ[𝑛] of the system
while filter(b,a,x) and conv(x,h) perform convolution and the same result. The only difference of
filter() and conv() command is that output of filter has the same number of samples as that of 𝑥[𝑛]
while in conv command output samples are equal to length(x)+length(h)-1.
5
Computing Impulse Response h[n] from a difference equation
Consider following difference equation
𝑦[𝑛] − 0.5 𝑦[𝑛 − 1] = 𝑥[𝑛]
Compute impulse response ℎ[𝑛] of this difference equation on paper and then verify it with following
code.
6
h=impz(b,a);% Computing Impulse Response using Num and Den
coefficients
y_filter = filter(b,a,x)%
Computing System Response y[n]
using Filter command
y_conv = conv(x,h);% Computing
System Response y[n] using
conv command
n=0:length(x)-1;
subplot(211)
stem(n,y_filter,'filled');
title('System Response using
filter')
grid
xlabel('n','fontweight','bold')
ylabel('y[n]','fontweight','bold')
subplot(212)
stem(n,y_conv(1:length(x)),'filled')
title('System Response using conv')
xlabel('n','fontweight','bold')
ylabel('y[n]','fontweight','bold')
grid
Example 6 :Comparing Impulse Response 𝒉[𝒏] and System Response 𝒚[𝒏] when input
is impulse sequence 𝜹[𝒏] using Matlab:-
% Comparing Impulse Response h[n] and System Response/Convolution
y[n] in Time Domain
% When input signal is Impulse
clc;clear all;close all
% Let Difference Equation is y[n]-0.5y[n-1]=x[n]
a = [1 -0.5];% co-efficients of y[n]
b = [1];% co-efficients of x[n]
[h,n]=impz(b,a);% Computing Impulse Response using Num and Den
coefficients
x = [1 zeros(1,13)];% Defining x[n] as Impulse sequence
y_filter = filter(b,a,x);% Computing System Response y[n] using
Filter command
y_conv = conv(h,x);% Computing System Response y[n] using conv
command
7
subplot(311)
impz(b,a);% Computing and Plotting Impulse Response using
coefficients of Difference Equation
title('Impulse Response using filter');xlabel('n
(samples)','fontweight','bold');ylabel('Amplitude','fontweight','b
old')
title('Impulse Response h[n]','fontweight','bold');xlim([0
length(x)-1]);grid;subplot(312)
stem(n,y_filter,'filled');title('System Response y[n] using
filter');grid
xlabel('n','fontweight','bold')
ylabel('y[n]','fontweight','bold')
xlim([0 length(x)-1]);subplot(313)
stem(n,y_conv(1:length(x)),'filled')
title('System Response y[n] using conv')
xlabel('n','fontweight','bold')
ylabel('y[n]','fontweight','bold')
xlim([1 length(x)]-1)
grid
8
Example 7: Convolution:
𝑦[𝑛] = 𝑥[𝑘]ℎ[𝑛 – 𝑘]
Appendix:
Z Transform table:
9
10
11
12