The Z-transform and digital filters

Lecture 4 · Digital Signal Processing · University of Klagenfurt, 2022

Nunzio Alexandro Letizia

This lecture

Lecture 4 of 6

  1. The Z-transform
  2. Digital filters: transfer function, poles and zeros, frequency response
  3. Implementing a filter: the direct forms

From Digital Signal Processing at the University of Klagenfurt, first taught in 2020: this is the edition of summer 2022, so some of it may be a little dated. All lectures.

The Z-transform

01

Definition

From now on, for convenience, T=1. The Z-transform X(z) of the discrete-time signal x(n) is

X(z)=\sum_{n=-\infty}^{+\infty}x(n)\,z^{-n},\qquad z\in\mathbb{C}

It comes with the idea of convergence: we ask for absolute convergence,

\sum_{n=-\infty}^{+\infty}\left|x(n)\,z^{-n}\right|<\infty

and the set of z where it holds is the region of convergence (ROC).

  • If x(n)=0 for all n<n_0 and X(z) converges for some z_0, then it converges for all |z|>|z_0|.
  • If x(n)=0 for all n>n_0 and X(z) converges for some z_0, then it converges for all |z|<|z_0|.

Properties

X(z)=\mathcal{Z}[x]=\sum_nx(n)\,z^{-n}. On the unit circle it is the DTFT: X\left(e^{j2\pi f}\right)=\mathcal{F}[x(n)].

linearity \mathcal{Z}[\alpha_1x_1(n)+\alpha_2x_2(n)]=\alpha_1X_1(z)+\alpha_2X_2(z)
flipping \mathcal{Z}[x(-n)]=X\left(\frac1z\right)
conjugation \mathcal{Z}[x^*(n)]=X^*(z^*)
time shift \mathcal{Z}[x(n-n_0)]=z^{-n_0}X(z)
scaling in z \mathcal{Z}[a^nx(n)]=X\left(\frac za\right)
upsampling \mathcal{Z}\left[x\left(\frac na\right)\right]=X(z^a), with zeros where \frac na is not an integer
derivative \mathcal{Z}[n\,x(n)]=-z\,\dfrac{dX(z)}{dz}
convolution \mathcal{Z}[x_1*x_2(n)]=X_1(z)\cdot X_2(z)

Ex. 1

Compute the Z-transform X(z) of the discrete-time signals

  1. x(n)=\delta(n-n_0)
  2. x(n)=a_0\delta(n)+a_1\delta(n-1)+\dots+a_N\delta(n-N), that is a_n for 0\le n\le N and 0 elsewhere
  3. x(n)=a^n\,1_0(n)
  4. x(n)=-b^n\,1_0(-1-n), that is -b^n for n<0 and 0 for n\ge0
  5. y(n)=h*x(n), where h(n)=\delta(n)+0.2\,\delta(n-1) and x(n)=a^n\,1_0(n)

Ex. 1: solutions

1. X(z)=z^{-n_0}, for all z\neq0 if n_0>0.

2. X(z)=a_0+a_1z^{-1}+\dots+a_Nz^{-N}=\sum_{n=0}^{N}a_nz^{-n}: a polynomial in z^{-1}, converging for z\neq0.

3. A geometric series in q=az^{-1}:

X(z)=\sum_{n=0}^{\infty}\left(az^{-1}\right)^n=\frac{1}{1-az^{-1}},\qquad |z|>|a|

4. With m=-n:

X(z)=-\sum_{m=1}^{\infty}\left(b^{-1}z\right)^m=1-\frac{1}{1-b^{-1}z}=\frac{1}{1-bz^{-1}},\qquad |z|<|b|

The same expression as item 3, with a different ROC: the formula alone does not say whether the signal is causal or anticausal.

5. H(z)=1+0.2z^{-1}, so Y(z)=\dfrac{1+0.2z^{-1}}{1-az^{-1}}=-\dfrac{0.2}{a}+\dfrac{1+\frac{0.2}{a}}{1-az^{-1}}, |z|>|a|, and for a\neq0

y(n)=-\frac{0.2}{a}\,\delta(n)+\left(1+\frac{0.2}{a}\right)a^n\,1_0(n)

Assignment 2

Solve the four exercises.

  1. Compute the Fourier transform X(f) of x(t)=\mathrm{rect}(2t)\cdot1(t).
  2. Compute the discrete-time Fourier transform of x(kT)=\mathrm{triangle}(kT), with T=0.5 s. Sketch its magnitude and check it in Matlab.
  3. Compute the discrete Fourier transform X(hF) of x(n)=[1,0,1,0,1,0,\dots], of N terms, and show it is correct in Matlab for a generic N.
  4. Compute the Z-transform X(z) of x(n)=\cos(2\pi fn+\varphi)\cdot1_0(n).

Assignment 2: the solution

a. \mathrm{rect}(2t) is 1 on \left[-\frac14,\frac14\right); the step keeps \left[0,\frac14\right): \ x(t)=\mathrm{rect}\left(\frac{t-1/8}{1/4}\right) and X(f)=\frac14\,\mathrm{sinc}\left(\frac f4\right)e^{-j\pi f/4}.

b. Only three samples are non-zero, x(\pm T)=\frac12 and x(0)=1:

X(f)=T\sum_kx(kT)\,e^{-j2\pi fkT}=\frac12\left[\frac12e^{j\pi f}+1+\frac12e^{-j\pi f}\right]=\frac12+\frac12\cos(\pi f)=\cos^2\left(\frac{\pi f}{2}\right)

real and non-negative, periodic with period \frac1T=2 Hz.

c. x(n)=\frac12\left[1+(-1)^n\right]: the DFT of the ones is N\delta(h), the one of (-1)^n is the exam exercise of lecture 3. So, for N even, X=\left[\frac N2,0,\dots,0,\frac N2,0,\dots,0\right] with the second line at h=\frac N2; for N odd, X(0)=\frac{N+1}{2} and X(h)=\frac12+\frac j2\tan\left(\frac{\pi h}{N}\right) otherwise.

d. Two geometric series, one per exponential of the cosine:

X(z)=\frac12\left(\frac{e^{j\varphi}}{1-e^{j2\pi f}z^{-1}}+\frac{e^{-j\varphi}}{1-e^{-j2\pi f}z^{-1}}\right)=\frac{\cos\varphi-\cos(2\pi f-\varphi)\,z^{-1}}{1-2\cos(2\pi f)\,z^{-1}+z^{-2}},\qquad|z|>1

with two poles on the unit circle, at e^{\pm j2\pi f}.

Assignment 2 in Matlab

N = 200;
x = zeros(1,N);
X_th = zeros(1,N);
for i=1:N
    x(i) = (1+(-1)^(i-1))/2;
    if mod(N,2) == 0
        X_th(i) = 0;
    else
        X_th(i) = (1+1j*tan(pi*(i-1)/N))/2;
    end
end
X_th(1) = X_th(1)+N/2;
if mod(N,2) == 0
    X_th(N/2+1) = X_th(N/2+1)+N/2;
end
X = fft(x);
plot(abs(X),'LineWidth',4)
% ...

Exercise c: the DFT computed by fft against the formula, for any N.

No saved run survives. What to expect for N=200: two lines of height 100, at indices 1 and 101 (Matlab counts from 1), with the formula on top of them. For an odd N, a line of height \frac{N+1}{2} at index 1 and a curve that grows near h=\frac N2.

The script compares magnitudes only, not phases.

Digital filters

02

LTI systems and the ARMA model

The Z-transform is convenient because a convolution in time becomes a multiplication of polynomials in z. A general discrete-time system

x(n) h(n) y(n)

has the input-output relation, with x(n) and y(n) causal,

y(n)=\underbrace{\sum_{k=1}^{N}a_k\,y(n-k)}_{\text{AR, autoregressive}}+\underbrace{\sum_{m=0}^{M}b_m\,x(n-m)}_{\text{MA, moving average}}

How can we identify the impulse response h(n)?

Ex. 2

Find the impulse response h(n) of the systems below, with x and y causal and x(n)=[1,0,\dots,0]:

  1. (FIR) \ y(n)=x(n)-x(n-1)+\frac14x(n-2)
  2. (IIR) \ y(n)=\frac12y(n-1)+x(n)+\frac12x(n-1)

1. Feed the impulse: y=\left[1,\,-1,\,\frac14,\,0,\dots\right]. The response stops after three samples: a finite impulse response, h(n)=\delta(n)-\delta(n-1)+\frac14\delta(n-2), with b_0=1, b_1=-1, b_2=\frac14.

2. Feed the impulse: y=\left[1,\,1,\,\frac12,\,\frac14,\,\frac18,\dots\right]. The feedback keeps it going forever: an infinite impulse response. With the transfer function of the next slide,

H(z)=\frac{1+\frac12z^{-1}}{1-\frac12z^{-1}}=-1+\frac{2}{1-\frac12z^{-1}}\ \Longrightarrow\ h(n)=-\delta(n)+2\left(\frac12\right)^n1_0(n),\qquad|z|>\frac12

The transfer function

Take the Z-transform of the general input-output relation:

Y(z)=\sum_{k=1}^{N}a_k\,z^{-k}\,Y(z)+\sum_{m=0}^{M}b_m\,z^{-m}\,X(z)

therefore

Y(z)=X(z)\cdot\frac{\sum_{m=0}^{M}b_m\,z^{-m}}{1-\sum_{k=1}^{N}a_k\,z^{-k}}=X(z)\cdot\frac{B(z)}{A(z)}=X(z)\cdot H(z)

H(z) is the transfer function of the filter: the roots of B(z) are its zeros, the roots of A(z) its poles. For the IIR filter of Ex. 2:

  • one zero at z=-\frac12, one pole at z=\frac12;
  • the causal ROC, |z|>\frac12, contains the unit circle: the filter is stable.

A causal filter is stable when all its poles are inside the unit circle.

The unit circle with a zero at minus one half and a pole at one half on the real axis.

The frequency response

The frequency response of a filter is the transfer function on the unit circle:

\tilde H(f)=H\left(e^{j2\pi f}\right),\qquad |\tilde H(f)|=\left|\frac{B\left(e^{j2\pi f}\right)}{A\left(e^{j2\pi f}\right)}\right|

With B(z)=c\prod_m\left(1-z_mz^{-1}\right) and A(z)=\prod_k\left(1-p_kz^{-1}\right), and \left|e^{j2\pi f}\right|=1,

|\tilde H(f)|=|c|\,\frac{\prod_m\left|e^{j2\pi f}-z_m\right|}{\prod_k\left|e^{j2\pi f}-p_k\right|}

the product of the distances of the point e^{j2\pi f} from the zeros, over the product of its distances from the poles. Walk around the circle: near a zero the response dips, near a pole it peaks.

The unit circle with two zeros near minus one and two poles inside the circle; segments join a point on the circle to each of them.

Ex. 3

  1. The output of a filter is \ y(n)=\frac12x(n)+\frac12x(n-2).
    • Find the transfer function H(z) and plot its poles and zeros on the complex plane.
    • Sketch the magnitude and the phase of the frequency response \tilde H(f).
    • Compute the output when the input is x(n)=\cos\left(\frac{2\pi}{4}n\right). What does the filter do to it?
  2. The output of a filter is \ y(n)=2y(n-1)+\frac13y(n-2)+x(n)+\frac12x(n-1)+\frac14x(n-2). Find its impulse response h(n).

Ex. 3: solution of item 1

H(z)=\frac12+\frac12z^{-2}=\dfrac{z^2+1}{2z^2}: zeros at z=\pm j, a double pole at z=0. On the unit circle,

\tilde H(f)=\frac12+\frac12e^{-j4\pi f}=e^{-j2\pi f}\,\frac{e^{j2\pi f}+e^{-j2\pi f}}{2}=\cos(2\pi f)\,e^{-j2\pi f}

so |\tilde H(f)|=|\cos(2\pi f)|, and the phase is -2\pi f (a delay of one sample), with jumps of \pi at f=\frac14 and \frac34, where the cosine changes sign.

Left: zeros at plus and minus j on the unit circle, a double pole at the origin. Right: the magnitude, a rectified cosine with zeros at one quarter and three quarters.

The input \cos\left(\frac{2\pi}{4}n\right) sits exactly at f=\frac14, on the zero z=j: for all n the output is 0. Started at n=0, x(n)=[1,0,-1,0,1,\dots], only a transient survives, y(n)=\frac12\delta(n). The filter is a notch: it removes the frequency \frac14.

Ex. 3: solution of item 2

H(z)=\frac{1+\frac12z^{-1}+\frac14z^{-2}}{1-2z^{-1}-\frac13z^{-2}}=-\frac34+\frac{\frac74-z^{-1}}{1-2z^{-1}-\frac13z^{-2}}=-\frac34+\frac{A}{1-p_1z^{-1}}+\frac{B}{1-p_2z^{-1}}

The poles solve 3z^2-6z-1=0: p_{1,2}=1\mp\frac{2}{\sqrt3}, with |p_1|\approx0.15 and |p_2|\approx2.15; the residues are

A=\frac{14-3\sqrt3}{16}\approx0.55,\qquad B=\frac{14+3\sqrt3}{16}\approx1.20\qquad(A+B=\tfrac74)

Each ROC gives a different h(n):

  • |z|>|p_2|: \ h(n)=-\frac34\delta(n)+\left(Ap_1^n+Bp_2^n\right)1_0(n), causal and unstable: h=[1,\ 2.5,\ 5.58,\ 12,\dots], what the recursion produces with x, y causal;
  • |p_1|<|z|<|p_2|: \ h(n)=-\frac34\delta(n)+Ap_1^n1_0(n)-Bp_2^n1_0(-n-1), stable, the ROC contains the unit circle, but not causal;
  • |z|<|p_1|: anticausal.

Two poles on the real axis, one near the origin and one outside the unit circle; the annulus between them is shaded and contains the unit circle.

Implementing a filter

03

The building blocks

How do we build the system \ y(n)=\sum_{k=1}^{N}a_k\,y(n-k)+\sum_{m=0}^{M}b_m\,x(n-m)\ in hardware? With four blocks:

block input output
sum x(n), y(n) x(n)+y(n)
product with a constant a x(n) a\cdot x(n)
delay z^{-1} x(n) x(n-1)
filter H(z) x(n) x*h(n)

In the diagrams that follow, an arrow labelled with a coefficient is a product, a z^{-1} on an arrow is a delay, and arrows that meet are summed.

Direct form I

In the direct form I, the feedforward part (MA, B(z)) and the feedback part (AR, 1/A(z)) are separate blocks, in series.

Direct form I: on the left a chain of delays on the input, each tap multiplied by b and summed; on the right a chain of delays on the output, each tap multiplied by a and fed back into a second sum.

It needs M+N delays.

Direct form II and its transpose

Direct form II. Swap the two blocks: \frac{1}{A(z)} first, then B(z). Both now delay the same signal w(n), so the two delay chains merge into one.

Direct form II: one central chain of delays; taps go left through a coefficients into the input sum and right through b coefficients into the output sum.

Direct form II transposed. Reverse every arrow, and swap input and output: the transfer function does not change.

Direct form II transposed: the input enters on the right and is distributed through b coefficients; the output is fed back through a coefficients; the delays sit between the sums of a central column.

Both need only \max(M,N) delays.

Finding H(z) from a diagram

The inverse problem: given a network, find its transfer function. Name the signal at the output of every sum, w_0(n),\dots,w_4(n), write one equation per node, and take the Z-transform. In the example of the lecture:

\begin{cases}w_0(n)=x_1(n)\\w_1(n)=x_1(n-1)+2w_4(n)\\w_2(n)=3w_4(n)\\w_3(n)=x_1(n)+2w_1(n)+w_2(n-1)\\w_4(n)=w_3(n-1)\end{cases}\ \xrightarrow{\ \mathcal{Z}\ }\ \begin{cases}W_0(z)=X_1(z)\\W_1(z)=z^{-1}X_1(z)+2W_4(z)\\W_2(z)=3W_4(z)\\W_3(z)=X_1(z)+2W_1(z)+z^{-1}W_2(z)\\W_4(z)=z^{-1}W_3(z)\end{cases}

In matrix form, with the output a combination of the nodes:

\begin{cases}\mathbf{W}=\mathbf{F}\mathbf{W}+\mathbf{A}X\\Y=\mathbf{B}\mathbf{W}\end{cases}\ \Longrightarrow\ \begin{cases}\mathbf{W}=(\mathbf{I}-\mathbf{F})^{-1}\mathbf{A}X\\Y=\mathbf{B}(\mathbf{I}-\mathbf{F})^{-1}\mathbf{A}X\end{cases}\ \Longrightarrow\ H(z)=\mathbf{B}(\mathbf{I}-\mathbf{F})^{-1}\mathbf{A}

Ex. 4

  1. A simple low-pass filter is the running average of M consecutive samples, y(n)=\frac1M\sum_{m=0}^{M-1}x(n-m). With M=8:
    1. sketch its impulse response; b. find its DC gain \tilde H(0); c. find the zeros of the transfer function and sketch the zero/pole diagram; d. from it, sketch |\tilde H(f)|.
  2. The output of a filter is y(n)=\alpha^2y(n-2)+x(n).
    1. Find \alpha such that the system is stable; b. find the poles and sketch the zero/pole diagram; c. sketch |\tilde H(f)| varying \alpha; d. find the impulse response h(n).
  3. A sound source is inside an empty room, and a single wall produces an echo: y(t)=\alpha\,y(t-\tau)+x(t), with \tau the propagation time and \alpha the attenuation. Let F_s be the sampling frequency, and x_s, y_s the sampled signals.
    1. Find H(z) between X_s(z) and Y_s(z); b. find its zeros and poles; c. sketch |\tilde H(f)| varying \alpha; d. filter a noise source and listen to the output.

Ex. 4: the running average

a. h(n)=\frac18 for n=0,\dots,7, and 0 elsewhere. b. \tilde H(0)=\sum_mb_m=8\cdot\frac18=1.

c. A geometric series:

H(z)=\frac18\sum_{m=0}^{7}z^{-m}=\frac18\cdot\frac{1-z^{-8}}{1-z^{-1}}

The zeros are the 8th roots of unity, z_k=e^{j\frac{2\pi}{8}k}, except z=1, cancelled by the denominator: 7 zeros on the unit circle, and 7 poles at z=0.

d. |\tilde H(f)|=\dfrac18\left|\dfrac{\sin(8\pi f)}{\sin(\pi f)}\right|: 1 at f=0, zero at f=\frac k8. A low-pass filter, and not a sharp one.

Left: seven zeros on the unit circle at multiples of 45 degrees, none at z equal to one, and the poles at the origin. Right: the magnitude, a main lobe at zero frequency and small side lobes between the zeros at multiples of one eighth.

Ex. 4: the second filter

H(z)=\dfrac{1}{1-\alpha^2z^{-2}}: poles at \pm\alpha, a double zero at z=0. a. Causal and stable if |\alpha|<1.

c. |\tilde H(f)|=\dfrac{1}{\sqrt{1+\alpha^4-2\alpha^2\cos(4\pi f)}}: from \frac{1}{1+\alpha^2} at f=\frac14,\frac34 to \frac{1}{1-\alpha^2} at f=0,\frac12. Flat for \alpha=0, peaks that grow without bound as |\alpha|\to1.

d. Partial fractions, H(z)=\frac{1/2}{1-\alpha z^{-1}}+\frac{1/2}{1+\alpha z^{-1}}, |z|>|\alpha|:

h(n)=\frac{\alpha^n+(-\alpha)^n}{2}\,1_0(n)=\begin{cases}\alpha^n&n\ge0\text{ even}\\0&\text{otherwise}\end{cases}

Left: two poles on the real axis at plus and minus alpha. Right: the magnitude for three values of alpha, with peaks at zero and one half that grow with alpha.

Ex. 4: the echo

a. With \tau=NT a whole number of sampling periods, N=\tau F_s: \ y_s(n)=\alpha\,y_s(n-N)+x_s(n) and

H(z)=\frac{1}{1-\alpha z^{-N}}

b. z^N=\alpha: for 0<\alpha<1, N poles equally spaced on a circle of radius \alpha^{1/N}, z_i=\alpha^{1/N}e^{j\frac{2\pi}{N}i}, and N zeros at z=0.

c. |\tilde H(f)|=\dfrac{1}{\sqrt{1+\alpha^2-2\alpha\cos(2\pi fN)}}: a comb, with peaks \frac{1}{1-\alpha} at f=\frac kN and dips \frac{1}{1+\alpha} halfway between. The echo reinforces the frequencies whose period fits the delay. (Item d, a Matlab task, has no surviving solution.)

Left: N poles equally spaced on a circle just inside the unit circle. Right: the magnitude, a comb of N equal peaks per period.

Assignment 3

Write a Matlab function to filter a recorded voice. The voice, sampled at 8192 Hz, is distorted by a harmonic component at f_0=1 kHz. The function is

function y = Filter_Harmonic(x, Fs, f0, type)

where x is the distorted voice, Fs the sampling frequency, f0 the frequency of the disturbance, type either 'FIR' (finite impulse response) or 'IIR' (infinite impulse response), and y the filtered, clean voice.

For both the FIR and the IIR implementation:

  • listen to the output, and sketch the block diagram;
  • plot the zeros and poles of the filter in the complex plane;
  • plot the magnitude of the frequency response \tilde H(f).

What are the main differences between the two approaches?

Assignment 3: the solution

f_0 = 1000;
T = 1/Fs;
% ...
harm = V_0*cos(2*pi*f_0*T.*k+phi);   % V_0 = 0.2, phi = pi/15
y_d = y+harm;
% ...
b = [1 -2*cos(2*pi*f_0*T) 1];
a = [1 0 0];
zplane(b,a)
f = 0:T:1;
w = 2*pi*f;
figure
[H_FIR,~] = freqz(b,a,w);
plot(w,abs(H_FIR))
% ...
b = [1 -2*cos(2*pi*f_0*T) 1];
a = [1 -1.98*cos(2*pi*f_0*T) 0.99^2];
zplane(b,a)
% ...
error_FIR = norm(y_FIR-y);
error_IIR = norm(y_IIR-y);

With \theta_0=2\pi\frac{f_0}{F_s}\approx0.767 rad, both filters put two zeros on the unit circle at e^{\pm j\theta_0}:

  • FIR: H(z)=1-2\cos\theta_0\,z^{-1}+z^{-2}, two delays, always stable, linear phase; but the notch is wide, and the filter colours the whole voice.
  • IIR: the same zeros, and two poles just inside them at 0.99\,e^{\pm j\theta_0}. Away from f_0 each pole cancels its zero, so the response is flat except for a narrow notch, at the cost of feedback and a non-linear phase.

The code is mine; the comparison is added for the web version. No saved run survives: expect a smaller error for the IIR filter. The FIR gain is not normalised to 1 at DC, and the second zplane draws over the FIR magnitude.