The z-Transform and Discrete Transfer Functions
A vendor library hands you a filter as two coefficient arrays, b = [1, 1] and a = [1, -0.5], and nothing else. What is its gain at DC and at half the sampling rate? What is its impulse response in closed form? If you put two such filters in series, what are the coefficients of the combination? The z-transform answers all of these with polynomial algebra, the way the Laplace transform did for continuous systems. This lesson is the practical toolkit: transfer functions from difference equations, the frequency response on the unit circle, inverse transforms with scipy.signal.residuez, and cascades by polynomial multiplication. The derivations and more pairs are in The z-Transform.
Definition and the delay operator
The one property that does most of the work: a delay of one sample multiplies the transform by , so . Applying it term by term to the difference equation gives
The coefficient arrays of lfilter(b, a, x) are the transfer function, in ascending powers of . Convolution becomes multiplication, , and is the z-transform of the impulse response.
Pairs you will use (all for causal sequences, valid for larger than the largest pole magnitude, the region of convergence):
| ROC | ||
|---|---|---|
| all | ||
Frequency response. Setting (the unit circle) gives , the frequency response. Two points are worth evaluating by hand for any filter: (DC, ) gives the DC gain , and (, i.e. ) gives the Nyquist gain .
Link to the s-plane. Sampling gives , so a continuous pole corresponds to a discrete pole : the left half-plane maps inside the unit circle, the axis onto it, with . The next lesson, Poles, Zeros and Stability in z, uses this map to read filters geometrically.
Worked example: the vendor filter
Given: b = [1, 1], a = [1, -0.5], , i.e. .
Find: , the DC and Nyquist gains, the pole and zero, the impulse response in closed form.
Solve:
- : zero at , pole at (inside the unit circle, stable).
- DC: (). Nyquist (): . A low-pass with a gain of 4.
- Partial fractions in : , so
Check: run the recursion with an impulse: , , . And , the DC gain.
import numpy as np
from scipy.signal import lfilter, residuez, freqz, tf2zpk
b, a = [1.0, 1.0], [1.0, -0.5]
z, p, k = tf2zpk(b, a)
print("zeros:", z, " poles:", p, " gain:", k)
r, pr, direct = residuez(b, a) # H = sum r/(1 - p z^-1) + direct terms
print(f"partial fractions: {float(r[0].real):.1f} / (1 - {float(pr[0].real):.1f} z^-1) + direct {direct}")
n = np.arange(6)
h_formula = 3 * 0.5**n + np.where(n == 0, -2.0, 0.0)
h_filter = lfilter(b, a, (n == 0).astype(float))
print("h[n] formula:", h_formula)
print("h[n] lfilter:", h_filter)
def H_at(zv): # evaluate H(z) = B(z^-1)/A(z^-1) at any z
zi = 1 / zv
return np.polyval(b[::-1], zi) / np.polyval(a[::-1], zi)
print(f"H(1) = {H_at(1.0).real:.3f}, H(-1) = {abs(H_at(-1.0)):.3f}, |H(j)| = {abs(H_at(1j)):.4f} (f = fs/4)")
_, Hf = freqz(b, a, worN=[0.0, 100.0, 250.0], fs=1000.0)
print("freqz |H| at 0, 100, 250 Hz:", np.round(np.abs(Hf), 4))zeros: [-1.] poles: [0.5] gain: 1.0
partial fractions: 3.0 / (1 - 0.5 z^-1) + direct [-2.]
h[n] formula: [1. 1.5 0.75 0.375 0.1875 0.09375]
h[n] lfilter: [1. 1.5 0.75 0.375 0.1875 0.09375]
H(1) = 4.000, H(-1) = 0.000, |H(j)| = 1.2649 (f = fs/4)
freqz |H| at 0, 100, 250 Hz: [4. 2.8643 1.2649]
freqz evaluates exactly the same function on the unit circle; is .
Cascades and the step response
In series, transfer functions multiply, and multiplying polynomials in is convolving their coefficient arrays. The step response follows from , and its final value from the final value theorem for a stable :
import numpy as np
from scipy.signal import lfilter
b1, a1 = np.array([1.0, 1.0]), np.array([1.0, -0.5]) # the vendor filter
b2, a2 = np.array([0.2, 0.2]), np.array([1.0, -0.6]) # a second low-pass, DC gain 1
b, a = np.convolve(b1, b2), np.convolve(a1, a2) # H = H1 * H2
print("cascade b =", b, " a =", a)
rng = np.random.default_rng(0)
x = rng.standard_normal(200)
y_two = lfilter(b2, a2, lfilter(b1, a1, x))
y_one = lfilter(b, a, x)
print(f"max difference, two stages vs one: {np.max(np.abs(y_two - y_one)):.1e}")
step = lfilter(b, a, np.ones(40))
print(f"step response: y[39] = {step[-1]:.4f}, H(1) = sum(b)/sum(a) = {b.sum() / a.sum():.4f}")cascade b = [0.2 0.4 0.2] a = [ 1. -1.1 0.3]
max difference, two stages vs one: 2.4e-15
step response: y[39] = 4.0000, H(1) = sum(b)/sum(a) = 4.0000
Multiplying polynomials is exact in mathematics but not always in fixed-point hardware: high-order filters built as one big polynomial are numerically fragile, which is why biquads keep the factors separate.
Guided lab: check a library filter on a board
Hand calculation. For the vendor filter at : gain 4 at DC, at , at , at , 0 at .
Simulation. Add 400.0 to the freqz list: ().
What you would measure. Run the filter on a board's ADC samples and stream input and output codes. Because the DC gain is 4, scale the input down first or the output overflows: with a to input centred at , subtract the mid-scale code before filtering and keep the amplitude below a quarter of the range. Expect output/input amplitude ratios within about of the hand values (limited by ADC noise and the generator's amplitude flatness), and a residual at that depends on the phase of the samples relative to the sine.
Hazards and safe setup. USB-powered board, generator output checked on a scope (within to ) before connecting, series resistor.
Pitfalls
- Reading coefficients from written in positive powers of : is , so
b = [0, 1],a = [1, 0, -0.5], notb = [1, 0]. Divide numerator and denominator by the highest power of first. - Forgetting the sign convention: a feedback term is
a = [1, -0.5]. - Applying the final value theorem to an unstable filter.
- Ignoring the gain of a filter (here 4) and overflowing a fixed-point output.
Practice
- Find for . What are its DC and Nyquist gains?
- Find for and its DC gain.
- A filter has
b = [0.5, 0.5]. Cascaded with itself, what are the coefficients?
Answer
- ; and : a band-pass with zeros at DC and .
- ; DC gain .
- .
Test yourself
In the z-domain a one-sample delay corresponds to
- multiplying by
- multiplying by
- adding
- differentiating
The DC gain of a filter with coefficient arrays b and a is
b[0]/a[0]sum(b)/sum(a)max(b)- always 1
The frequency corresponds to which point of the z-plane?
Two filters in series have the combined numerator
b1 + b2np.convolve(b1, b2)b1 * b2element by elementnp.concatenate([b1, b2])