|
| 1 | +# Biquad / Second-Order-Section Cascade |
| 2 | + |
| 3 | +## Overview & Motivation |
| 4 | + |
| 5 | +High-order IIR filters realized as a single monolithic direct-form transfer function suffer |
| 6 | +from severe numerical sensitivity: small perturbations in any coefficient can shift poles |
| 7 | +dramatically, and round-off noise accumulates in proportion to the filter order. These |
| 8 | +effects are catastrophic on fixed-point hardware and non-trivial even in single-precision |
| 9 | +floating-point. |
| 10 | + |
| 11 | +The solution adopted universally in professional DSP is to factor the transfer function into |
| 12 | +quadratic terms — each with exactly two poles and two zeros — and chain them in series. |
| 13 | +This cascade of second-order sections ("biquads") keeps each section's coefficients |
| 14 | +well-scaled, its pole-sensitivity small, and its round-off noise bounded independently of |
| 15 | +the total filter order. |
| 16 | + |
| 17 | +## Mathematical Theory |
| 18 | + |
| 19 | +### Transfer Function Factorization |
| 20 | + |
| 21 | +Any real-coefficient rational transfer function of order $N$ factors as: |
| 22 | + |
| 23 | +$$H(z) = G \prod_{k=1}^{\lceil N/2 \rceil} \frac{b_{0,k} + b_{1,k}z^{-1} + b_{2,k}z^{-2}}{1 + a_{1,k}z^{-1} + a_{2,k}z^{-2}}$$ |
| 24 | + |
| 25 | +Each factor is a second-order section (SOS). The overall frequency response is the product |
| 26 | +of the per-section responses; the total group delay is the sum of per-section group delays. |
| 27 | + |
| 28 | +### RBJ Cookbook Design Equations |
| 29 | + |
| 30 | +For standard shelving, peaking, and pass/reject types, closed-form normalized coefficients |
| 31 | +derive from the bilinear transform applied to an analog prototype. Define: |
| 32 | + |
| 33 | +$$\omega_0 = \frac{2\pi f_c}{f_s}, \qquad c_\omega = \cos(\omega_0), \qquad \alpha = \frac{\sin(\omega_0)}{2Q}$$ |
| 34 | + |
| 35 | +**Low-pass:** |
| 36 | + |
| 37 | +$$b_0 = \frac{1 - c_\omega}{2(1+\alpha)}, \quad b_1 = \frac{1-c_\omega}{1+\alpha}, \quad b_2 = b_0$$ |
| 38 | + |
| 39 | +$$a_1 = \frac{-2c_\omega}{1+\alpha}, \quad a_2 = \frac{1-\alpha}{1+\alpha}$$ |
| 40 | + |
| 41 | +**High-pass:** replace $(1 - c_\omega)$ with $(1 + c_\omega)$ and negate $b_1$. |
| 42 | + |
| 43 | +**Notch (band-reject):** |
| 44 | + |
| 45 | +$$b_0 = b_2 = \frac{1}{1+\alpha}, \quad b_1 = a_1 = \frac{-2c_\omega}{1+\alpha}, \quad a_2 = \frac{1-\alpha}{1+\alpha}$$ |
| 46 | + |
| 47 | +All coefficients are pre-normalized by $a_0 = 1 + \alpha$, so the feedback denominator leading |
| 48 | +coefficient is always 1 and the hot path requires no division. |
| 49 | + |
| 50 | +### Transposed Direct Form II (TDF-II) |
| 51 | + |
| 52 | +TDF-II is the canonical embedded realization. It minimizes the number of state registers to |
| 53 | +two per section while achieving low round-off noise. The recurrence for section $k$ is: |
| 54 | + |
| 55 | +$$y[n] = b_0 x[n] + z_1[n-1]$$ |
| 56 | + |
| 57 | +$$z_1[n] = b_1 x[n] - a_1 y[n] + z_2[n-1]$$ |
| 58 | + |
| 59 | +$$z_2[n] = b_2 x[n] - a_2 y[n]$$ |
| 60 | + |
| 61 | +This is exactly five multiplies and four adds per sample — the theoretical minimum for an |
| 62 | +arbitrary biquad — and requires no intermediate storage beyond $z_1$ and $z_2$. |
| 63 | + |
| 64 | +## Complexity Analysis |
| 65 | + |
| 66 | +| Case | Time | Space | Notes | |
| 67 | +|------------|---------------|---------------|------------------------------------------------| |
| 68 | +| Per sample | $O(S)$ | $O(S)$ | $S$ = number of sections; 5 mults + 4 adds each | |
| 69 | +| Design | $O(1)$ | $O(1)$ | Closed-form RBJ formulas; no iteration | |
| 70 | +| Reset | $O(S)$ | — | Zero two state words per section | |
| 71 | + |
| 72 | +## Step-by-Step Walkthrough |
| 73 | + |
| 74 | +**Example:** design a 4th-order low-pass as two cascaded 2nd-order sections. |
| 75 | + |
| 76 | +Given $f_c = 100$ Hz, $f_s = 1000$ Hz, $Q = 0.707$ (Butterworth factor for each section): |
| 77 | + |
| 78 | +1. Compute $\omega_0 = 2\pi \cdot 0.1 \approx 0.6283$ |
| 79 | +2. $c_\omega = \cos(0.6283) \approx 0.8090$, $\alpha = \sin(0.6283)/(2 \times 0.707) \approx 0.4142$ |
| 80 | +3. $a_0 = 1.4142$; normalize: $b_0 \approx 0.06745$, $b_1 \approx 0.13490$, $b_2 \approx 0.06745$, |
| 81 | + $a_1 \approx -1.1429$, $a_2 \approx 0.4128$ |
| 82 | +4. Use the same section twice to realize a 4th-order filter. |
| 83 | +5. At each sample, pass input through section 1, then section 2: $y = H_2(H_1(x))$. |
| 84 | + |
| 85 | +**Impulse response trace** for bypass section $\{b_0=1, b_1=b_2=a_1=a_2=0\}$: |
| 86 | +Input $[1, 0, 0, \ldots]$ → output $[1, 0, 0, \ldots]$ — identity passthrough. |
| 87 | + |
| 88 | +## Pitfalls & Edge Cases |
| 89 | + |
| 90 | +- **Section ordering**: sections should be ordered by increasing peak gain and poles paired with |
| 91 | + their nearest zeros. Misordering can cause intermediate signals to overflow before later |
| 92 | + sections attenuate them, especially in fixed-point implementations. |
| 93 | +- **Poles near the unit circle**: high-$Q$ or near-Nyquist designs push poles close to |
| 94 | + $|z| = 1$. Finite-precision rounding can move a pole just outside, causing instability. |
| 95 | + Use $Q \leq 30$ in single precision; double precision or lattice realizations for higher $Q$. |
| 96 | +- **Denormal floats**: small state values approaching the denormal range stall the FPU pipeline |
| 97 | + on many embedded cores. Enabling flush-to-zero (FTZ) or the fast-math pragma prevents this |
| 98 | + at the cost of negligible numerical error. |
| 99 | +- **DC gain normalization**: the RBJ low-pass has unity DC gain by construction. Gain-staging |
| 100 | + between sections is not required; each section's output is well-scaled relative to its input. |
| 101 | + |
| 102 | +## Variants & Generalizations |
| 103 | + |
| 104 | +- **Direct Form I**: maintains four state variables per section (input and output history) but |
| 105 | + provides extra dynamic range at the cost of higher memory. Preferred when signal levels are |
| 106 | + difficult to bound. |
| 107 | +- **Lattice / wave-digital**: alternative topologies that remain stable under very aggressive |
| 108 | + coefficient quantization; used in high-speed fixed-point applications. |
| 109 | +- **Parallel SOS**: sections are summed rather than chained; useful for multi-band equalizers. |
| 110 | +- **Second-order allpass**: $b_0 = a_2$, $b_1 = a_1$, $b_2 = 1$ — phase rotation without |
| 111 | + magnitude change, used in crossover networks and polyphase systems. |
| 112 | + |
| 113 | +## Applications |
| 114 | + |
| 115 | +- Multi-pole anti-aliasing and reconstruction filters in ADC/DAC chains. |
| 116 | +- Audio equalization (parametric EQ, shelving filters) in embedded audio processors. |
| 117 | +- Vibration isolation and sensor conditioning in industrial control systems. |
| 118 | +- ECG/EEG baseline wander removal with high-pass SOS cascades. |
| 119 | +- Motor drive current-sensing loop compensation when a precise roll-off characteristic is needed. |
| 120 | + |
| 121 | +## Connections to Other Algorithms |
| 122 | + |
| 123 | +- **IIR (direct form)**: the single-section IIR is the primitive building block. A cascade of |
| 124 | + biquads is simply a structured composition of single sections with superior numerical properties. |
| 125 | +- **NotchCombFilter**: the notch filter is a single biquad specialized to place zeros exactly |
| 126 | + on the unit circle; a cascade realizes multi-pole notch designs. |
| 127 | +- **IirFilterDesign**: a companion design routine generates SOS coefficient arrays from an analog |
| 128 | + prototype via the bilinear transform, feeding directly into the cascade. |
| 129 | +- **RecursiveBuffer**: the IIR simulator uses `math::RecursiveBuffer` for time-domain state; |
| 130 | + the biquad avoids the overhead by keeping only two explicit state scalars per section. |
| 131 | + |
| 132 | +## References & Further Reading |
| 133 | + |
| 134 | +- R. Bristow-Johnson, "Cookbook formulae for audio EQ biquad filter coefficients", |
| 135 | + <https://www.w3.org/TR/audio-eq-cookbook/> |
| 136 | +- A. V. Oppenheim & R. W. Schafer, *Discrete-Time Signal Processing*, 3rd ed., Ch. 6 |
| 137 | + (cascade and parallel structures). |
| 138 | +- R. G. Lyons, *Understanding Digital Signal Processing*, 3rd ed., Prentice Hall, 2011, Ch. 6. |
| 139 | +- S. J. Orfanidis, *Introduction to Signal Processing*, Prentice Hall, 1996, Ch. 12 |
| 140 | + (second-order sections and ladder filters). |
0 commit comments