Skip to content
Tutorials Center
My learning

Topics

Lesson 42 of 68 ยท Series, Transforms and PDEs

The Z-Transform and Difference Equations

Learn the Z-transform, region of convergence, common pairs and the shift rule, then solve difference equations and check filter stability with Python.

The Laplace transform turns differential equations, which describe signals that change continuously, into algebra. But a great deal of engineering happens in discrete steps: audio samples, a bank balance updated monthly, a digital filter in a phone, a population counted once per year. Those systems obey difference equations, such as y[n] = 0.5*y[n-1] + x[n], where each value depends on earlier values. The tool that does for difference equations what Laplace does for differential equations is the Z-transform.

This lesson defines the transform, explains the region of convergence (ROC), lists the pairs you need, uses the shift property to solve difference equations, and shows how the poles of a system tell you whether it is stable. Everything is checked numerically with NumPy and SciPy. It pairs naturally with sequences and series and the Fourier transform.

Definition

For a sequence x[n] that is zero for n < 0, its (one-sided) Z-transform is

X(z) = x[0] + x[1]/z + x[2]/z**2 + ... = sum of x[n] * z**(-n)

where z is a complex number. It is a power series in 1/z. Each sample sits at its own "time slot": the coefficient of z**(-n) is the sample at time n. That is the whole idea, and it makes the properties easy to remember.

Because it is an infinite series, the sum converges only for some values of z. The set of such z is the region of convergence. For right-sided sequences (starting at n = 0) it is the outside of a circle: |z| > R.

An example you can verify

Take x[n] = a**n for n >= 0 with a = 0.5. The transform is a geometric series, 1 + (a/z) + (a/z)**2 + ..., which sums to 1 / (1 - a/z) = z / (z - a) whenever |a/z| < 1, that is |z| > |a|.

Python Check a transform pair by summing the series

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

All three test points lie outside the circle |z| = 0.5, so the series matches the closed form. At z = 0.3, inside the circle, the partial sums keep growing: the series diverges and the formula z/(z-a) says nothing useful about it.

Common pairs and properties

You rarely sum a series by hand. You learn a short table and a few rules.

Sequence x[n], n >= 0 X(z) ROC
unit impulse delta[n] 1 all z
unit step u[n] z / (z - 1) `
a**n z / (z - a) `
n * a**n a*z / (z - a)**2 `
cos(w*n) z*(z - cos(w)) / (z**2 - 2*z*cos(w) + 1) `

The rules that matter most:

  • Linearity: the transform of p*x[n] + q*y[n] is p*X(z) + q*Y(z).
  • Delay (shift): the sequence x[n-1] has transform X(z)/z. Delaying by one step multiplies by z**(-1). For a one-sided transform with initial value x[-1], add x[-1] (this is how initial conditions enter).
  • Convolution: the transform of the convolution of two sequences is the product X(z)*Y(z). This is why filters are described by a single function of z.
flowchart LR
    A["difference equation<br/>in n"] -- "Z-transform" --> B["algebra in z"]
    B -- "solve for Y(z)" --> C["Y(z) as a fraction"]
    C -- "partial fractions,<br/>inverse transform" --> D["sequence y[n]"]

The same recipe as with Laplace: transform, solve algebraically, transform back.

Solving a difference equation

Consider the first-order filter y[n] = 0.5*y[n-1] + x[n] with y[-1] = 0 and a unit impulse input x[n] = delta[n]. Transform both sides, using the delay rule y[n-1] -> Y(z)/z:

Y(z) = 0.5*Y(z)/z + 1

so Y(z) * (1 - 0.5/z) = 1 and Y(z) = z / (z - 0.5). From the table, y[n] = 0.5**n. The ratio H(z) = Y(z)/X(z) is called the transfer function of the system, and its inverse transform is the impulse response.

SciPy's lfilter runs exactly this kind of recursion. Its arguments b and a are the coefficients of the numerator and denominator in powers of 1/z: here H(z) = 1 / (1 - 0.5*z**(-1)), so b = [1] and a = [1, -0.5].

Python Impulse response from the transfer function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The step response settles at H(1) = 2. The "final value" of a constant input is the transfer function evaluated at z = 1, which is the gain at zero frequency (DC).

A classic: Fibonacci with the Z-transform

The Fibonacci numbers satisfy F[n] = F[n-1] + F[n-2] with F[0] = 0, F[1] = 1. Treat it as a system driven by a single impulse at n = 1: F[n] = F[n-1] + F[n-2] + delta[n-1]. Transforming gives

F(z) = F(z)/z + F(z)/z**2 + 1/z, so F(z) = z / (z**2 - z - 1).

The denominator has roots phi = (1 + sqrt(5))/2 and psi = (1 - sqrt(5))/2. Partial fractions give F(z) = (1/sqrt(5)) * (z/(z - phi) - z/(z - psi)), and using the table, F[n] = (phi**n - psi**n) / sqrt(5), which is Binet's formula.

Python Fibonacci by transfer function and Binet's formula

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The pole with the largest magnitude, phi, controls the long-term growth. Every pole contributes a term pole**n, so poles with |pole| > 1 explode, poles inside the unit circle die out.

Poles and stability

For a causal system, the impulse response is a sum of terms p**n over the poles p of H(z). So:

  • If all poles satisfy |p| < 1, the impulse response decays and the system is stable: bounded input always gives bounded output.
  • A pole with |p| > 1 makes the response grow without limit: unstable.
  • A pole exactly on the unit circle gives a response that never decays; such a system is marginally stable.

The unit circle |z| = 1 plays the role the imaginary axis plays in the Laplace world. Evaluating H(z) on the unit circle, z = exp(i*w), gives the frequency response of a digital filter, which links this lesson to the Fourier transform.

Python Stability from the poles

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first filter has gain 10 at w = 0 and about 0.526 at w = pi: it is a low-pass filter, because its pole sits near z = 1 (low frequency). A pole near z = -1 would favor the highest frequencies instead.

Inverse Z-transform in practice

Three methods cover almost everything:

  1. Table plus partial fractions. Divide X(z) by z, expand into simple fractions, multiply back by z, and read off the table. This is the method we used for Fibonacci.
  2. Power series (long division). Expand X(z) in powers of 1/z; the coefficients are the samples. This is exactly what lfilter does numerically.
  3. Contour integral. The formal inverse integrates around the unit circle in the complex plane; it is rarely done by hand.

The ROC matters for the inverse: the same fraction z/(z-a) is a**n for n >= 0 when |z| > |a| but a left-sided sequence when |z| < |a|. For causal systems always take the outside region.

Common mistakes

  • Mixing up z and 1/z when reading filter coefficients. In lfilter, the coefficients multiply z**0, z**(-1), z**(-2), ....
  • Forgetting the ROC and so choosing the wrong inverse.
  • Declaring stability by looking at zeros. Stability depends on poles only.
  • Applying the final value rule to an unstable system.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the Z-transform of the sequence x[n] = a^n for n >= 0?

    The transform is a geometric series 1 + a/z + (a/z)^2 + ..., which sums to z/(z - a) whenever |a/z| < 1.
  2. 2. What does delaying a sequence by one sample, x[n-1], do to its Z-transform (zero initial state)?

    Each sample moves from time slot n to n+1, so the coefficient of z^-n moves to z^-(n+1). That multiplies the whole series by z^-1.
  3. 3. A causal system has poles at 0.9, -0.5 and 1.2. Is it stable?

    Every pole must have magnitude below 1. The pole at 1.2 contributes a term 1.2^n that grows without bound.
  4. 4. Why does the Fibonacci sequence grow like the golden ratio phi to the power n?

    The roots of z^2 - z - 1 are phi and psi. The sequence is (phi^n - psi^n)/sqrt(5), and since |psi| < 1 the phi term dominates.

Summary

  • The Z-transform X(z) = sum x[n] z**(-n) turns sequences and difference equations into algebra; the ROC says where the series converges.
  • Key pairs: impulse 1, step z/(z-1), a**n gives z/(z-a); key rules: linearity, delay by 1/z, convolution becomes multiplication.
  • Solving a difference equation: transform, solve for Y(z), split with partial fractions, invert from the table.
  • The transfer function H(z) has the impulse response as its inverse transform; poles inside the unit circle mean stability.
  • SciPy's lfilter and tf2zpk run and analyze these systems numerically.

Next: handle differential equations whose coefficients are not constant in Wronskian, Variation of Parameters and Cauchy-Euler Equations.