Filter Structures, Fixed Point and Limit Cycles
A filter's transfer function says what it computes; its structure says how: in what order the multiplications and additions happen and which intermediate values are stored. In exact arithmetic every structure gives the same output. On a fixed-point processor they do not: one structure overflows on a full-scale input, another hums with a small oscillation when the input stops, a third is fine. This lesson compares the direct forms, shows the cascade of biquads as the standard choice, and explains fixed-point formats, overflow, scaling and limit cycles.
Direct forms
For a biquad :
| Structure | Delays | What is stored | Strength | Weakness |
|---|---|---|---|---|
| direct form I (DF I) | 4 | past inputs and outputs | one accumulator sums everything: with a wide accumulator, no internal overflow | twice the memory |
| direct form II (DF II) | 2 | an internal node after the poles | minimum memory | can be much larger than input and output: overflow |
| DF II transposed (DF II T) | 2 | two partial sums | minimum memory, good in floating point, SciPy's lfilter |
partial sums need headroom in fixed point |
| cascade of SOS | one structure per section | robust to coefficient rounding | needs ordering and scaling | |
| lattice, state-space, wave digital | more | — | low sensitivity, low noise | more multiplications |
DF II T is the transposition of DF II (reverse every arrow, swap input and output): same transfer function, but the delays hold sums instead of the large internal node. Floating point (float32 on a Cortex-M4F or a DSP): use DF II T sections. Fixed point with a wide accumulator (a 16 × 16-bit multiplier into a 40-bit accumulator, as most fixed-point DSPs have, or the 64-bit multiply-accumulate instructions of a Cortex-M4/M7): DF I, because all products are summed once in the accumulator and only the output is rounded.
Worked example: same filter, different internal sizes
Given: a 2nd-order Butterworth low-pass, , , input a sine of amplitude (nearly full scale) at .
Find: whether DF I and DF II T reproduce lfilter, the peak output and the peak of DF II's internal node.
Solve: at the cutoff the output amplitude is . DF II first applies the poles, , then the zeros; at is large because the zeros (at ) that normally pull the gain down come afterwards.
import numpy as np
from scipy import signal
def df1(b, a, x): # direct form I: 4 delays, one accumulator
x1 = x2 = y1 = y2 = 0.0
y = np.empty(len(x))
for n, xn in enumerate(x):
yn = b[0] * xn + b[1] * x1 + b[2] * x2 - a[1] * y1 - a[2] * y2
x2, x1, y2, y1 = x1, xn, y1, yn
y[n] = yn
return y
def df2t(b, a, x): # direct form II transposed: 2 delays (the states s1, s2)
s1 = s2 = 0.0
y = np.empty(len(x))
for n, xn in enumerate(x):
yn = b[0] * xn + s1
s1 = b[1] * xn - a[1] * yn + s2
s2 = b[2] * xn - a[2] * yn
y[n] = yn
return y
b, a = signal.butter(2, 1000, fs=8000) # one biquad, a 2nd-order Butterworth low-pass
rng = np.random.default_rng(1)
x = rng.standard_normal(200)
ref = signal.lfilter(b, a, x)
print("b =", np.round(b, 6), " a =", np.round(a, 6))
print("DF I equals lfilter to 1e-12:", bool(np.abs(df1(b, a, x) - ref).max() < 1e-12))
print("DF II T equals lfilter to 1e-12:", bool(np.abs(df2t(b, a, x) - ref).max() < 1e-12))
# Internal signal sizes for a full-scale sine at the band edge: what a fixed-point design must hold
t = np.arange(400)
xs = 0.99 * np.sin(2 * np.pi * 1000 / 8000 * t)
w = signal.lfilter([1], a, xs) # DF II internal node: the poles act first
print(f"peak |y| = {np.abs(signal.lfilter(b, a, xs)).max():.3f}, peak DF II internal node = {np.abs(w).max():.3f}")b = [0.097631 0.195262 0.097631] a = [ 1. -0.942809 0.333333]
DF I equals lfilter to 1e-12: True
DF II T equals lfilter to 1e-12: True
peak |y| = 0.707, peak DF II internal node = 2.113
Check: both hand-written structures match lfilter to rounding error. The output peaks at , but the DF II internal node reaches : in Q15 arithmetic (range to ) it would overflow, although input and output are both in range. Scaling the input down by 3 fixes DF II at a cost of about of signal-to-noise ratio; DF I or DF II T avoid the problem.
Fixed-point numbers
A Q. number has integer bits (plus sign) and fractional bits; the common Q15 (Q0.15 in 16 bits) covers with a step of . Multiplying two Q15 numbers gives a Q30 product in 32 bits; adding many products needs guard bits (a 40-bit accumulator gives 8 extra integer bits, enough for 256 full-scale products). Coefficients with magnitude up to 2 (biquad ) are stored in Q14 or as two halves. The final result is shifted back to Q15 with rounding and saturation.
- Overflow: two's-complement addition wraps around, so a result just above full scale becomes a large negative number, a loud click in audio and a large error in a control loop. Saturation clips at the limit instead. Intermediate wrap-around in a sum is harmless if the final result fits (a property of two's complement), so a wide accumulator plus saturation of the result is the standard combination.
- Rounding noise: each rounding adds noise of variance ; in an IIR section that noise is shaped and amplified by the poles, so high-Q sections near the unit circle are noisy. This is one more reason to order sections well.
- Scaling: choose the gain of each section so that no node exceeds full scale for any input ( norm of the impulse response from the input to that node ) or for any sine ( norm of the frequency response ), as in Biquads and SOS.
Limit cycles
With rounding inside a feedback loop, a recursive filter with zero input need not decay to zero. A first-order section stops changing once rounds back to : the dead band LSB. Second-order sections can sustain an oscillation at roughly the pole frequency. Large-scale overflow oscillations (wrap-around feeding back) are worse; saturation arithmetic prevents them.
import numpy as np
def rnd(v): # round half up, as a fixed-point multiplier with rounding does
return int(np.floor(v + 0.5))
# 1) First-order section y[n] = Q(a*y[n-1]), zero input, state in integer LSBs
for a in (0.9, -0.9):
y, seq = 12, []
for n in range(14):
y = rnd(a * y)
seq.append(y)
print(f"a = {a:+.1f}, start 12 LSB, zero input:", seq)
print(f"dead band |y| <= 0.5/(1 - |a|) = {0.5 / (1 - 0.9):.0f} LSB")
# 2) Second-order section, poles r = 0.95 at 30 degrees, each product rounded (Q15-style)
r, th = 0.95, np.pi / 6
a1, a2 = -2 * r * np.cos(th), r * r
y1, y2, out = 40, 0, []
for n in range(60):
y = rnd(-a1 * y1) + rnd(-a2 * y2)
y2, y1 = y1, y
out.append(y)
print(f"a1 = {a1:.4f}, a2 = {a2:.4f}; last 12 outputs with zero input:", out[-12:])
# 3) Overflow: 16-bit two's complement wraps, saturation clips
def wrap16(v):
return (v + 32768) % 65536 - 32768
def sat16(v):
return max(-32768, min(32767, v))
s = 30000 + 5000 # an accumulator result just over full scale
print(f"35000 in 16 bits: wraps to {wrap16(s)}, saturates to {sat16(s)}")a = +0.9, start 12 LSB, zero input: [11, 10, 9, 8, 7, 6, 5, 5, 5, 5, 5, 5, 5, 5]
a = -0.9, start 12 LSB, zero input: [-11, 10, -9, 8, -7, 6, -5, 5, -4, 4, -4, 4, -4, 4]
dead band |y| <= 0.5/(1 - |a|) = 5 LSB
a1 = -1.6454, a2 = 0.9025; last 12 outputs with zero input: [5, 5, 3, 0, -3, -5, -5, -3, 0, 3, 5, 5]
35000 in 16 bits: wraps to -30536, saturates to 32767
With the state sticks at 5 LSB, the edge of the dead band; with it alternates in sign and never reaches zero. The second-order section (, ) settles into a 12-sample oscillation of 5 LSB amplitude, a tone at that would be audible as a faint whistle in quiet passages. Remedies: magnitude truncation (round toward zero) in the feedback path, more state bits (a 32-bit state with 16-bit I/O), dither, or a structure free of limit cycles. The last line shows why saturation matters: wraps to in 16 bits, a full-scale jump, while saturation gives .
Guided lab: hearing a limit cycle
Hand calculation. For the dead band is 5 LSB; in a 16-bit converter with a range () that is a DC offset of about , and the second-order cycle is a tone at ( at ).
Simulation. Change the rounding function to round toward zero (int(v)) in the program and confirm that both limit cycles die out.
What you would measure. Run a fixed-point biquad (16-bit states, rounding) on a USB-powered board feeding its DAC; play a sine, stop it, and look at the DAC output on a scope at high sensitivity with averaging off. Expect a residual tone of a few LSB (about to for a 16-bit, DAC) instead of silence, gone with 32-bit states. The measurement is near the noise floor: average the scope's FFT to see the line at .
Hazards and safe setup. USB-powered board and scope only; if you listen, use headphones at low volume, because overflow oscillations in a wrapping implementation are full-scale.
Pitfalls
- Using DF II in fixed point without checking the internal node.
- Letting sums wrap around instead of saturating the result.
- Rounding to 16 bits between every multiply in a high-Q section.
- Testing fixed-point code only with loud signals: limit cycles appear when the input goes quiet.
- Porting a float design to Q15 without re-checking coefficient ranges ( can approach 2).
Practice
- What are the range and step of Q1.14 in 16 bits?
- Find the dead band of .
- A 40-bit accumulator sums Q30 products. How many full-scale products can it add without overflow?
Answer
- , step .
- LSB.
- A Q30 product of two full-scale numbers is at most (32 bits including sign); 8 more bits allow such products.
Test yourself
Which biquad structure stores an internal node that can exceed the output by a large factor?
- DF I
- DF II
- DF II transposed
- none
A zero-input limit cycle is caused by
- coefficient quantisation alone
- rounding inside a feedback loop
- an unstable pole
- aliasing
In 16-bit two's complement, 35000 wraps to
- 32767
- -30536
- 2232
- -32768
The dead band of is
- 0.5 LSB
- 1 LSB
- 5 LSB
- 10 LSB