Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 36 ยท Sampling and Z-transform

Inverse Z-transform and Difference Equations

Invert z-transforms with partial fractions and power series, solve linear difference equations, and simulate digital filters with scipy.signal.lfilter and residuez.

A difference equation is the digital counterpart of a differential equation. Examples are a running average, a bank balance with interest, the Fibonacci numbers and every IIR filter. The z-transform turns the recursion into algebra, and the inverse z-transform turns the answer back into a sequence. This lesson shows three inversion methods and how to simulate the same system directly.

Difference equations and H(z)

A general linear constant-coefficient difference equation is

y[n] = -a1 y[n-1] - ... - aN y[n-N] + b0 x[n] + b1 x[n-1] + ... + bM x[n-M].

With zero initial conditions, the z-transform (using the delay rule) gives

H(z) = Y(z)/X(z) = (b0 + b1 z^(-1) + ... + bM z^(-M)) / (1 + a1 z^(-1) + ... + aN z^(-N)).

Reading the coefficients of the numerator and denominator gives you b and a for scipy.signal.lfilter, which runs the recursion.

Method 1: partial fractions

Consider y[n] = 0.5 y[n-1] + x[n]. Then H(z) = 1/(1 - 0.5 z^(-1)), and a unit impulse input gives h[n] = 0.5โฟ u[n], a causal exponential. For a more interesting system, y[n] = 1.5 y[n-1] - 0.5 y[n-2] + x[n] has

H(z) = 1 / (1 - 1.5 z^(-1) + 0.5 z^(-2)) = 1 / ((1 - z^(-1))(1 - 0.5 z^(-1))).

Partial fractions in z^(-1) give 2/(1 - z^(-1)) - 1/(1 - 0.5 z^(-1)), so h[n] = (2 - 0.5โฟ) u[n]. Notice the pole at z = 1 (on the unit circle): the impulse response does not decay but settles at 2, so the system is marginally stable. scipy.signal.residuez automates this expansion.

Python residuez versus the hand expansion

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.

Method 2: power series (long division)

Divide the numerator by the denominator in powers of z^(-1). The coefficients of the quotient are the samples. For X(z) = 1/(1 - 0.5 z^(-1)), dividing gives 1 + 0.5 z^(-1) + 0.25 z^(-2) + ..., so x[n] = 0.5โฟ. This method yields numbers quickly and needs no pole finding, but the pattern may be hard to spot. Divide in ascending powers for a right-sided signal, and in descending powers for a left-sided one.

Method 3: simulate it

Because lfilter simply runs the recursion, feeding it an impulse is an inversion method in itself. You can also write the recursion yourself, which clarifies what the filter does. The next example computes the first terms of the Fibonacci sequence from F[n] = F[n-1] + F[n-2]: its z-transform has poles at the golden ratio and its negative reciprocal, which explains the growth.

Python Fibonacci as an impulse response

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 impulse response 1, 1, 2, 3, 5, 8, ... confirms the pole outside the unit circle at the golden ratio: the system is unstable, and so the sequence grows without bound.

Zero state, zero input and total response

With nonzero initial conditions, the total response is the sum of the zero-input response (the system's own modes, driven by memory) and the zero-state response (the input, convolved with h[n]). Use the unilateral z-transform with the shift rule Z{y[n-1]} = z^(-1) Y(z) + y[-1] to include initial conditions, or pass zi to lfilter. The next example runs a leaky integrator y[n] = 0.9 y[n-1] + x[n] with an initial value and shows both pieces add up.

Python Zero-input plus zero-state response

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.

lfilter normalizes by a[0], and its b and a are coefficients of powers of z^(-1) in increasing delay. Do not paste coefficients of a polynomial in z with the highest power first without checking which convention you use.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. For y[n] = 0.5 y[n-1] + x[n], what is the impulse response?

    H(z) = 1 / (1 - 0.5 z^(-1)) inverts to 0.5โฟ for n >= 0 when the system is causal.
  2. 2. The system y[n] = y[n-1] + y[n-2] + x[n] is...

    The characteristic polynomial zยฒ - z - 1 has a root at the golden ratio, about 1.618, outside the unit circle.
  3. 3. How does power-series (long-division) inversion obtain x[n]?

    X(z) = ฮฃ x[n] z^(-n), so the coefficient of z^(-n) in the expansion is x[n].
  4. 4. The total response of a system with nonzero initial conditions is...

    By linearity the response splits into the part due to initial conditions and the part due to the input.

Summary

  • A linear difference equation maps to H(z) = B(z^(-1)) / A(z^(-1)).
  • Invert with partial fractions, power-series division or by simulating the impulse response.
  • Poles inside the unit circle mean decaying modes and a stable causal system.
  • lfilter runs the recursion; residuez expands H(z) in partial fractions.
  • Total response = zero-input response + zero-state response.

Next: Filter Design Basics