Biquads, Second-Order Sections and Coefficient Quantisation
An 8th-order elliptic low-pass with a passband at is a routine audio or sensor filter. Write it as one transfer function , round the nine denominator coefficients to 24 fractional bits (finer than a 32-bit float), and the filter is unstable: its output grows without bound. Write the same filter as four second-order sections and it survives rounding to 16 bits. This lesson shows why, defines the biquad and its stability triangle, and gives the rules for ordering and scaling sections. Every IIR filter in practice, in SciPy, in an audio plug-in or on a microcontroller, is run this way. Background: IIR Filters and Poles and Zeros in z.
The biquad
A biquad is a second-order IIR filter:
One complex-conjugate pole pair gives and ; a zero pair works the same way in the numerator. An order- filter becomes biquads in cascade. SciPy stores them as an array sos of shape with rows , and sosfilt, sosfreqz, sosfilt_zi work on it directly. Every design function returns it with output='sos'.
Why one polynomial fails
The poles of a sharp low-pass at a low frequency crowd together near . The roots of a polynomial are extremely sensitive to its coefficients when roots are clustered: a change moves a pole by roughly
and the product of small distances in the denominator makes the factor huge. In a biquad each pole has only its own conjugate as a neighbour, so the sensitivity stays small. The direct-form coefficients also span a wide range (here up to 64), costing integer bits in fixed point.
Worked example: quantising an 8th-order elliptic low-pass
Given: an elliptic low-pass, order 8, ripple, attenuation, passband edge , .
Find: the largest pole radius after rounding the coefficients to 24, 20 and 16 fractional bits, for one 8th-order polynomial and for four biquads, and the gain error at .
Solve: round to and recompute the roots:
import numpy as np
from scipy import signal
# A narrow 8th-order elliptic low-pass: fs = 48 kHz, passband to 1 kHz
fs = 48000
sos = signal.ellip(8, 0.5, 70, 1000, output="sos", fs=fs)
b, a = signal.sos2tf(sos)
print("direct form: a =", np.array2string(a, precision=4, max_line_width=100))
_, H0 = signal.sosfreqz(sos, worN=[500], fs=fs)
print(f"largest pole radius, exact: {np.abs(np.roots(a)).max():.6f}; gain at 500 Hz, exact: {20 * np.log10(abs(H0[0])):.3f} dB")
def quantise(x, frac_bits): # round to a fixed-point grid with frac_bits fractional bits
return np.round(x * 2**frac_bits) / 2**frac_bits
for bits in (24, 20, 16):
aq = quantise(a, bits)
r_tf = np.abs(np.roots(aq)).max()
sq = quantise(sos, bits)
r_sos = max(np.abs(np.roots(s[3:])).max() for s in sq)
_, H = signal.sosfreqz(sq, worN=[0, 500], fs=fs)
print(f"{bits} fractional bits: one polynomial -> max |pole| {r_tf:.4f} ({'UNSTABLE' if r_tf >= 1 else 'stable'}); "
f"4 biquads -> max |pole| {r_sos:.6f}, gain at 500 Hz {20 * np.log10(abs(H[1])):.3f} dB")direct form: a = [ 1. -7.8123 26.7413 -52.3829 64.226 -50.4709 24.8242 -6.987 0.8616]
largest pole radius, exact: 0.996989; gain at 500 Hz, exact: -0.489 dB
24 fractional bits: one polynomial -> max |pole| 1.0358 (UNSTABLE); 4 biquads -> max |pole| 0.996989, gain at 500 Hz -0.490 dB
20 fractional bits: one polynomial -> max |pole| 1.1353 (UNSTABLE); 4 biquads -> max |pole| 0.996989, gain at 500 Hz -0.517 dB
16 fractional bits: one polynomial -> max |pole| 1.2370 (UNSTABLE); 4 biquads -> max |pole| 0.996989, gain at 500 Hz 0.923 dB
Check: the exact filter's largest pole radius is , only inside the unit circle. Rounding the polynomial's coefficients by at most pushes a pole out to : unstable. The four biquads keep their poles to six decimals even at 16 bits. At 16 bits the gain moves instead ( at against exact): the first section's numerator coefficients are about , so 16-bit rounding changes them by a few percent. That is a gain-distribution problem, solved by scaling (below), not a stability problem.
Reading the sections
import numpy as np
from scipy import signal
fs = 48000
sos = signal.ellip(8, 0.5, 70, 1000, output="sos", fs=fs)
print(" section b0 b1 b2 a1 a2 pole r f0 (Hz) Q in triangle")
for i, (b0, b1, b2, _, a1, a2) in enumerate(sos):
p = np.roots([1, a1, a2])[0]
r, w = abs(p), abs(np.angle(p))
# analog-equivalent f0 and Q from the z-plane pole (s = fs ln z)
s = fs * np.log(p)
f0, Q = abs(s) / (2 * np.pi), abs(s) / (-2 * s.real)
ok = abs(a2) < 1 and abs(a1) < 1 + a2
print(f" {i} {b0:10.3e} {b1:10.3e} {b2:10.3e} {a1:9.5f} {a2:9.6f} {r:.5f} {f0:7.1f} {Q:5.2f} {ok}")
# Run a unit step through the exact sections and through the 24-bit direct-form polynomial
b, a = signal.sos2tf(sos)
aq = np.round(a * 2**24) / 2**24
x = np.ones(3000)
y_sos = signal.sosfilt(sos, x)
y_tf = signal.lfilter(b, aq, x)
print(f"step response after 3000 samples: SOS {y_sos[-1]:.4f}, quantised polynomial {y_tf[-1]:.3e}") section b0 b1 b2 a1 a2 pole r f0 (Hz) Q in triangle
0 3.281e-04 -5.396e-04 3.281e-04 -1.92655 0.929064 0.96388 390.5 0.69 True
1 1.000e+00 -1.944e+00 1.000e+00 -1.94510 0.953737 0.97659 718.5 1.99 True
2 1.000e+00 -1.969e+00 1.000e+00 -1.96385 0.978253 0.98907 922.3 5.49 True
3 1.000e+00 -1.975e+00 1.000e+00 -1.97680 0.993986 0.99699 1003.8 21.78 True
step response after 3000 samples: SOS 0.9441, quantised polynomial -2.517e+43
Each section is one pole pair and one zero pair. Numerators have zeros exactly on the unit circle (the stopband nulls of the elliptic filter); the overall gain sits in the first row. The poles get closer to the unit circle and their Q rises toward the band edge: the last section has and a pole radius of . Every section is inside the stability triangle , . The 24-bit polynomial, run on a step for 3000 samples, reaches .
The figure shows why biquads near DC are hard even on their own: the sections sit in the corner , , where a small coefficient step is a large change of pole radius or angle. Low-frequency, high-Q sections at high sample rates need more coefficient bits (or a different structure, see Filter Structures). One remedy is to run such a filter at a lower rate after decimation.
Pairing, ordering and scaling
SciPy's zpk2sos (used by every output='sos' design) follows the classic rules:
- Pairing: each pole pair is matched with the zero pair closest to it, so each section's numerator partly cancels its own resonance and no section has a huge gain peak.
- Ordering: sections are ordered by pole radius; SciPy's output puts the poles closest to the unit circle last (its documentation describes the pairing, not the order, but the table above shows it), so the high-Q, high-gain section is fed by a signal that the earlier sections have already filtered.
- Scaling: in fixed point, each section's internal peak must not overflow. The overall gain is distributed so that the gain from the input to each section's output stays below 1 (an , or worst-case sine, norm) or the sum of the absolute values of its impulse response stays below 1 (, never overflows). Scaling moves gain between sections; it does not change the overall response.
Guided lab: float32 against fixed point
Hand calculation. The exact filter has at and a DC gain of (an even-order elliptic starts at the bottom of its ripple), so a step settles to .
Simulation. In the first program, round the coefficients to np.float32 (sos.astype(np.float32)) and check the pole radii; then try 12 fractional bits: every section's poles stay inside the circle, but the first section's numerator (about , close to one step of ) rounds to , a double zero at , and the DC gain vanishes. The poles only reach the unit circle below about 8 fractional bits, so with this filter the gain distribution fails long before stability does.
What you would measure. Implement the four sections on a USB-powered microcontroller board with float32 and with 16-bit fixed-point coefficients, feed a sine of amplitude and compare the output amplitudes on a scope. Expect float32 within of and the unscaled 16-bit version about higher, as the simulation predicts. The direct-form 8th-order version, if you try it, oscillates or saturates: switch it off at once.
Hazards and safe setup. Signals below the converter's input range from a function generator; USB-powered board; an unstable filter saturates the DAC output at a rail but cannot damage anything at these levels. Keep headphones off while testing unstable code.
Pitfalls
- Converting a stable
sosto(b, a)withsos2tf"for convenience" and running high orders that way. - Quantising coefficients without re-checking the pole radii and the frequency response.
- Assuming float32 is always enough: high-Q sections near DC at high sample rates can lose many bits.
- Ignoring the gain distribution: a section with a tiny numerator loses relative accuracy.
- Changing the section order without re-scaling.
Practice
- A biquad has poles at . Find and .
- Is , stable? And , ?
- How many biquads does a 7th-order Butterworth need, and what is the odd one?
Answer
- , .
- : is false, so the first is unstable (a real pole just outside ). is also false: a pole exactly at , marginally stable at best; in practice unstable.
- Four: three biquads plus one first-order section (stored as a biquad with ).
Test yourself
Why are high-order IIR filters implemented as cascades of biquads?
- biquads are faster
- pole positions are far less sensitive to coefficient rounding
- biquads have linear phase
- SciPy cannot handle polynomials
A biquad's denominator is stable if
- and
A pole pair gives
In the example, rounding the 8th-order denominator to 24 fractional bits
- changed nothing
- moved a pole outside the unit circle
- only changed the gain
- removed the ripple