TExcelTAD
Home › Signals, Systems & Digital Filters for Engineers › Filter Structures, Fixed Point and Limit Cycles

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 y[n]=b0x[n]+b1x[n−1]+b2x[n−2]−a1y[n−1]−a2y[n−2]y[n] = b_0 x[n] + b_1 x[n-1] + b_2 x[n-2] - a_1 y[n-1] - a_2 y[n-2]:

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 w[n]w[n] after the poles minimum memory w[n]w[n] can be much larger than input and output: overflow
DF II transposed (DF II T) 2 two partial sums s1,s2s_1, s_2 minimum memory, good in floating point, SciPy's lfilter partial sums need headroom in fixed point
cascade of SOS 2L2L one structure per section robust to coefficient rounding needs ordering and scaling
lattice, state-space, wave digital more — low sensitivity, low noise more multiplications
x[n] b0 + b1 + b2 + z^-1 s2 z^-1 s1 y[n] -a1 -a2 y = b0 x + s1; s1 = b1 x - a1 y + s2; s2 = b2 x - a2 y

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, fc=1 kHzf_c = 1\ \text{kHz}, fs=8 kHzf_s = 8\ \text{kHz}, input a sine of amplitude 0.990.99 (nearly full scale) at 1 kHz1\ \text{kHz}.

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 0.99/2=0.700.99/\sqrt{2} = 0.70. DF II first applies the poles, w=x/A(z)w = x/A(z), then the zeros; ∣1/A∣\lvert 1/A \rvert at 1 kHz1\ \text{kHz} is large because the zeros (at z=−1z = -1) that normally pull the gain down come afterwards.

python · try it yourself
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 0.7070.707, but the DF II internal node reaches 2.112.11: in Q15 arithmetic (range −1-1 to 11) 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 10 dB10\ \text{dB} of signal-to-noise ratio; DF I or DF II T avoid the problem.

Fixed-point numbers

A Qmm.nn number has mm integer bits (plus sign) and nn fractional bits; the common Q15 (Q0.15 in 16 bits) covers −1≤x<1-1 \le x < 1 with a step of 2−15=3.05×10−52^{-15} = 3.05 \times 10^{-5}. 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 a1a_1) 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 q2/12q^2/12; 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 (L1L_1 norm of the impulse response from the input to that node ≤1\le 1) or for any sine (L∞L_\infty norm of the frequency response ≤1\le 1), 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 y[n]=Q(a y[n−1])y[n] = Q(a\,y[n-1]) stops changing once ∣ay∣\lvert a y \rvert rounds back to ∣y∣\lvert y \rvert: the dead band ∣y∣≤0.5/(1−∣a∣)\lvert y \rvert \le 0.5/(1 - \lvert a \rvert) 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.

python · try it yourself
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 a=0.9a = 0.9 the state sticks at 5 LSB, the edge of the dead band; with a=−0.9a = -0.9 it alternates in sign and never reaches zero. The second-order section (r=0.95r = 0.95, 30∘30^\circ) settles into a 12-sample oscillation of 5 LSB amplitude, a tone at fs/12f_s/12 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: 35 00035\,000 wraps to −30 536-30\,536 in 16 bits, a full-scale jump, while saturation gives 32 76732\,767.

Guided lab: hearing a limit cycle

Hand calculation. For a=0.9a = 0.9 the dead band is 5 LSB; in a 16-bit converter with a ±1 V\pm 1\ \text{V} range (q=30.5 μVq = 30.5\ \mu\text{V}) that is a DC offset of about 0.15 mV0.15\ \text{mV}, and the second-order cycle is a 0.15 mV0.15\ \text{mV} tone at fs/12f_s/12 (4 kHz4\ \text{kHz} at 48 kSa/s48\ \text{kSa/s}).

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 0.10.1 to 0.3 mV0.3\ \text{mV} for a 16-bit, ±1 V\pm 1\ \text{V} 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 fs/12f_s/12.

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 (∣a1∣\lvert a_1 \rvert can approach 2).

Practice

  1. What are the range and step of Q1.14 in 16 bits?
  2. Find the dead band of y[n]=Q(0.98 y[n−1])y[n] = Q(0.98\,y[n-1]).
  3. A 40-bit accumulator sums Q30 products. How many full-scale products can it add without overflow?
Answer
  1. −2≤x<2-2 \le x < 2, step 2−14=6.1×10−52^{-14} = 6.1 \times 10^{-5}.
  2. 0.5/(1−0.98)=250.5/(1 - 0.98) = 25 LSB.
  3. A Q30 product of two full-scale numbers is at most 11 (32 bits including sign); 8 more bits allow 28=2562^8 = 256 such products.

Test yourself

Which biquad structure stores an internal node that can exceed the output by a large factor?

  1. DF I
  2. DF II
  3. DF II transposed
  4. none

A zero-input limit cycle is caused by

  1. coefficient quantisation alone
  2. rounding inside a feedback loop
  3. an unstable pole
  4. aliasing

In 16-bit two's complement, 35000 wraps to

  1. 32767
  2. -30536
  3. 2232
  4. -32768

The dead band of y[n]=Q(0.9 y[n−1])y[n] = Q(0.9\,y[n-1]) is

  1. 0.5 LSB
  2. 1 LSB
  3. 5 LSB
  4. 10 LSB