---
title: Filter lab
heading: Ladder responses from ABCD matrices, the bandpass transformation and Richards' stub filters, in Python
eyebrow: S6 · Lesson 25 · Filter lab
xp: 60
---

::: hook
Lessons 21 to 24 synthesized filters from prototype tables and transformations, each step justified by algebra. This lab checks the whole chain by simulation: it builds the ladder from its element values, multiplies the elements' ABCD matrices and reads the insertion loss, then does the same for the bandpass filter's actual inductors and capacitors and for the Richards stub filter, whose response must equal the prototype's at Richards' frequency variable and repeat every four cutoff frequencies.
:::

::: play
- The curves are a third-order Butterworth stub filter's insertion loss against frequency, $10\log_{10}(1 + \Omega^6)$ with $\Omega = \tan(\frac{\pi}{4}\frac{f}{f_c})$, and its lumped prototype's, $10\log_{10}(1 + (f/f_c)^6)$.
- Below cutoff the two nearly agree; the stub filter's loss climbs faster and becomes infinite at $2f_c$, where the stubs are a quarter wave.
- Beyond $2f_c$ it falls again and passes near $4f_c$: the spurious passband of every distributed filter.
:::

```sim plot
view: graph
x: r = 0..4
y: L = 0..60 dB
plot: 10*log(1 + tan(pi/4*r)^6) | label: Richards stub filter
plot: 10*log(1 + r^6) | dashed | label: lumped prototype
```

::: predict
## A 0.5 dB, N = 3 Chebyshev bandpass filter at 1 GHz with 10% bandwidth in 50 Ω is built from the transformed inductors and capacitors. What loss does it give at 1.1 GHz?

- [ ] 0.5 dB
- [ ] 3 dB
- [x] 17.8 dB, exactly the prototype's loss at the mapped frequency
- [ ] 60 dB

::: reveal
The mapping $\omega' = \frac{1}{\Delta}\left(\frac{f}{f_0} - \frac{f_0}{f}\right)$ sends 1.1 GHz to 1.909, where the prototype loses 17.8 dB. Each transformed resonator presents exactly the prototype element's reactance at the mapped frequency, so the lab's ladder of real LC values gives 17.826 dB, matching the prototype to every digit.
:::
:::

::: nameit
## Simulating a ladder

With 1 Ω terminations and a ladder that starts with a shunt capacitor $g_1$, alternating with series inductors:
- a shunt element of admittance Y is [[1, 0], [Y, 1]]; a series element of impedance Z is [[1, Z], [0, 1]];
- the product of all of them, A B C D, gives $S_{21} = \frac{2}{A + B/R_0 + CR_0 + D}$ for equal terminations $R_0$, and the insertion loss is $-20\log_{10}|S_{21}|$.

## Bandpass elements

At center $\omega_0$ and fractional bandwidth Δ in $R_0$: a shunt prototype capacitor g becomes a parallel LC with $C = \frac{g}{R_0\omega_0\Delta}$, $L = \frac{\Delta R_0}{\omega_0g}$; a series inductor g becomes a series LC with $L = \frac{gR_0}{\omega_0\Delta}$, $C = \frac{\Delta}{\omega_0gR_0}$.

## Stub filters

Richards' filter replaces each shunt capacitor g with an open stub of admittance g (in units of $Y_0$) and each series inductor with a short-circuited stub of impedance g, both λ/8 long at $f_c$. A shunt open stub adds $Y = jg\tan\theta$; a series short stub adds $Z = jg\tan\theta$, with $\theta = \frac{\pi}{4}\frac{f}{f_c}$.
:::

::: check
kind: python

## Write `butter_g(N)` and `ladder_db(g, w)`: the insertion loss at normalized frequency w of a ladder with 1 Ω terminations and elements g (shunt C first, alternating), from ABCD matrices.

```python starter
def butter_g(N):
    return [1.0] * N

def ladder_db(g, w):
    return 0.0
```

```python solution
import math

def mul(M, N):
    (a, b), (c, d) = M
    (e, f), (g, h) = N
    return ((a * e + b * g, a * f + b * h), (c * e + d * g, c * f + d * h))

def butter_g(N):
    return [2 * math.sin((2 * k - 1) * math.pi / (2 * N)) for k in range(1, N + 1)]

def ladder_db(g, w):
    M = ((1, 0), (0, 1))
    for i, x in enumerate(g):
        E = ((1, 0), (1j * w * x, 1)) if i % 2 == 0 else ((1, 1j * w * x), (0, 1))
        M = mul(M, E)
    (A, B), (C, D) = M
    return -20 * math.log10(abs(2 / (A + B + C + D)))
```

```python tests
import math
assert abs(ladder_db(butter_g(5), 1) - 3.0103) < 1e-4, "Butterworth: 3.01 dB at cutoff."
assert abs(ladder_db(butter_g(5), 2) - 10 * math.log10(1 + 2 ** 10)) < 1e-6, "And exactly 10 log(1 + w^10) at w = 2."
cheb = [1.5963, 1.0967, 1.5963]
assert abs(ladder_db(cheb, 1) - 0.5) < 0.001 and abs(ladder_db(cheb, 2) - 19.22) < 0.01, "0.5 dB Chebyshev: ripple edge and 19.2 dB at w = 2."
```

::: right
Right. The ladder reproduces the response formula exactly: the tabulated g-values are the solution of that formula.
:::
:::

::: check
kind: python

## Write `bandpass_db(g, f, f0, fbw, R0)`: the insertion loss of the bandpass filter built from prototype g (shunt first) by the transformation, at frequency f, from its actual L and C values.

```python starter
def bandpass_db(g, f, f0, fbw, R0):
    return 0.0
```

```python solution
import math

def mul(M, N):
    (a, b), (c, d) = M
    (e, f), (g, h) = N
    return ((a * e + b * g, a * f + b * h), (c * e + d * g, c * f + d * h))

def bandpass_db(g, f, f0, fbw, R0):
    w0, w = 2 * math.pi * f0, 2 * math.pi * f
    M = ((1, 0), (0, 1))
    for i, x in enumerate(g):
        if i % 2 == 0:
            C = x / (R0 * w0 * fbw)
            L = fbw * R0 / (w0 * x)
            E = ((1, 0), (1j * w * C + 1 / (1j * w * L), 1))
        else:
            L = x * R0 / (w0 * fbw)
            C = fbw / (w0 * x * R0)
            E = ((1, 1j * w * L + 1 / (1j * w * C)), (0, 1))
        M = mul(M, E)
    (A, B), (C, D) = M
    return -20 * math.log10(abs(2 / (A + B / R0 + C * R0 + D)))
```

```python tests
g = [1.5963, 1.0967, 1.5963]
assert abs(bandpass_db(g, 1e9, 1e9, 0.1, 50)) < 1e-9, "No loss at the center."
assert abs(bandpass_db(g, 1.1e9, 1e9, 0.1, 50) - 17.826) < 0.001, "17.8 dB at 1.1 GHz, the prototype's loss at 1.909."
lo = 1e9 * (-0.05 + (0.05 ** 2 + 1) ** 0.5)
assert abs(bandpass_db(g, lo, 1e9, 0.1, 50) - 0.5) < 0.002, "The lower band edge, where the mapping gives −1: 0.5 dB."
```

::: right
Right. The lower band edge isn't 0.95 GHz but 0.9512 GHz: the band edges are geometrically symmetric about $f_0$, so $f_1f_2 = f_0^2$.
:::
:::

::: check
kind: python

## Write `richards_db(g, r)`: the loss of a Richards stub filter (shunt open stubs of admittance g, series short stubs of impedance g, all λ/8 at cutoff) at r times the cutoff frequency.

```python starter
def richards_db(g, r):
    return 0.0
```

```python solution
import math

def mul(M, N):
    (a, b), (c, d) = M
    (e, f), (g, h) = N
    return ((a * e + b * g, a * f + b * h), (c * e + d * g, c * f + d * h))

def richards_db(g, r):
    t = math.tan(math.pi / 4 * r)
    M = ((1, 0), (0, 1))
    for i, x in enumerate(g):
        E = ((1, 0), (1j * t * x, 1)) if i % 2 == 0 else ((1, 1j * t * x), (0, 1))
        M = mul(M, E)
    (A, B), (C, D) = M
    return -20 * math.log10(abs(2 / (A + B + C + D)))
```

```python tests
import math
g = [1.0, 2.0, 1.0]
assert abs(richards_db(g, 1) - 3.0103) < 1e-4, "3 dB at the cutoff, as the prototype."
assert abs(richards_db(g, 0.5) - 10 * math.log10(1 + math.tan(math.pi / 8) ** 6)) < 1e-9, "At 0.5 fc it equals the prototype at Ω = tan 22.5°."
assert abs(richards_db(g, 0.7) - richards_db(g, 3.3)) < 1e-9, "Periodic: the response at f repeats at 4fc − f."
assert richards_db(g, 1.99) > 60, "Near 2fc the stubs approach a quarter wave and the loss soars."
```

::: right
Right. The mapping $\Omega = \tan\theta$ is exact, so the stub filter is the prototype seen through a periodic frequency warp.
:::
:::

::: explain
## Why is a bandpass filter's passband geometrically, not arithmetically, centered on its center frequency?

::: model
The lowpass-to-bandpass transformation replaces each reactance with a resonator whose reactance depends on $f/f_0 - f_0/f$. That quantity takes equal and opposite values at two frequencies whose product is $f_0^2$, so the filter's response is the same at those two frequencies: symmetric about $f_0$ on a logarithmic frequency axis. For a 10% filter at 1 GHz the 0.5 dB edges come out at 0.951 and 1.051 GHz, not 0.95 and 1.05; for narrow filters the difference is small, but for wide ones the upper side of the band is noticeably wider than the lower.
:::
:::
