---
title: "Code lab: design and verify a second-order filter"
heading: From specification to component values, checked numerically
eyebrow: C4 · Lesson 25 · Code lab
xp: 60
---

::: hook
Designing a filter goes in two directions. First, from a specification (a corner frequency and a Q) to component values. Then back: compute the circuit's actual response from those values and check that it meets the specification. Writing both directions in code catches algebra slips and makes it easy to try standard component values and see how far they move the result.

::: play
- The plot is the response of an equal-resistor Sallen-Key low-pass (lesson 13) built from R, $C_1$ and $C_2$.
- Set R, $C_1$ and $C_2$ for a 1 kHz Butterworth. Then try the nearest standard values and see how much the response moves.
:::
:::

```sim plot
view: bode
x: f = 10..100k Hz log
slider: R = 1k..100k = 10k Ω log
slider: C1 = 1n..1u = 22n F log
slider: C2 = 1n..1u = 10n F log
H: 1/((j*2*pi*f)^2*R^2*C1*C2 + j*2*pi*f*2*R*C2 + 1)
readout: f0 = 1/(2*pi*R*sqrt(C1*C2)) Hz
readout: Q = 0.5*sqrt(C1/C2)
```

::: predict
## For an equal-resistor Sallen-Key low-pass with Q = 2, how much larger must $C_1$ be than $C_2$?

- [ ] 2 times
- [ ] 4 times
- [x] 16 times
- [ ] 8 times

::: reveal
16 times: $C_1 = 4Q^2C_2$. High Q needs a large capacitor ratio, which is one reason equal-resistor designs are used mainly for modest Qs; above a Q of a few, other topologies are easier to build accurately.
:::
:::

::: nameit
## The design procedure in code

**Specification to values** (equal resistors, unity gain):

```python
import numpy as np
f0, Q, C2 = 1000.0, 0.707, 10e-9
C1 = 4 * Q**2 * C2
R = 1 / (2 * np.pi * f0 * np.sqrt(C1 * C2))
```

**Values to response.** With $s = j2\pi f$:

$$H = \frac{1}{s^2R^2C_1C_2 + 2sRC_2 + 1}$$

```python
f = np.logspace(1, 5, 2000)
s = 1j * 2 * np.pi * f
H = 1 / (s**2 * R**2 * C1 * C2 + s * 2 * R * C2 + 1)
gain_db = 20 * np.log10(np.abs(H))
```

**Checking.** The peak of `gain_db` should be $20\log_{10}\frac{Q}{\sqrt{1 - 1/4Q^2}}$ for $Q > 0.707$ (0 dB, at DC, below that). The −3 dB point equals $f_0$ only for the Butterworth Q of 0.707.

**Standard values.** Rounding to the E12 series moves $f_0$ and Q by a few percent; recomputing the response from the rounded values shows by how much.
:::


::: example
## Run the specification-to-values step by hand for $f_0$ = 1 kHz, Q = 0.707 and $C_2$ = 10 nF.

::: solution
1. **Find $C_1$.**
   $$\begin{aligned} C_1 &= 4Q^2C_2 \\ &= 4 \times 0.4998 \times 10\ \text{nF} \\ &= 20.0\ \text{nF} \end{aligned}$$
2. **Find R.** Here $\sqrt{C_1C_2}$ = 14.1 nF:
   $$\begin{aligned} R &= \frac{1}{2\pi f_0\sqrt{C_1C_2}} \\ &= \frac{1}{2\pi \times 1000 \times 14.1 \times 10^{-9}} \\ &= 11.3\ \text{k}\Omega \end{aligned}$$

**Answer:** $C_1$ = 20.0 nF and R = 11.3 kΩ.
:::
:::

::: example
## What gain does that filter have at 2 kHz?

::: solution
1. **Denominator at $2f_0$.**
   $$\begin{aligned} D &= 1 - 4 + j\frac{2}{0.707} \\ &= -3 + 2.83j \end{aligned}$$
2. **Gain.**
   $$\begin{aligned} |H| &= \frac{1}{\sqrt{9 + 8.00}} \\ &= 0.243 \end{aligned}$$
   which is −12.3 dB.
3. **Check.** That's the second-order Butterworth value,
   $$10\log_{10}(1 + 2^4)$$

**Answer:** 0.243, or −12.3 dB.
:::
:::

::: check
kind: python
given: f0 = 500 | 1000 | 2000; Q = 0.707 | 1 | 2

## Design an equal-resistor, unity-gain Sallen-Key low-pass with $f_0$ = {f0} Hz and Q = {Q}, using $C_2$ = 10 nF. Set `C1` and `R`.

```python starter
import numpy as np

C2 = 10e-9
C1 = 0   # replace
R = 0    # replace
print(C1, R)
```

```python solution
import numpy as np

C2 = 10e-9
C1 = 4 * Q**2 * C2
R = 1 / (2 * np.pi * f0 * np.sqrt(C1 * C2))
print(C1, R)
```

```python tests
import math
assert C1 > 0 and R > 0, "Set C1 and R to positive values."
f0_got = 1 / (2 * math.pi * R * math.sqrt(C1 * C2))
Q_got = 0.5 * math.sqrt(C1 / C2)
assert abs(f0_got - f0) < 0.005 * f0, f"Your values give f0 = {f0_got:.1f} Hz; it should be {f0} Hz."
assert abs(Q_got - Q) < 0.005 * Q, f"Your values give Q = {Q_got:.3f}; it should be {Q}."
```

::: right
Right. $C_1 = 4Q^2C_2$ sets Q, then R sets $f_0$.
:::
:::

::: check
kind: python
given: Q = 1 | 2 | 4

## For a second-order low-pass with $f_0$ = 1 kHz and Q = {Q}, compute $H$ on 4000 log-spaced frequencies from 10 Hz to 100 kHz and set `peak_db` to the largest gain in dB.

```python starter
import numpy as np

f0 = 1000.0
f = np.logspace(1, 5, 4000)
x = f / f0
H = 1 / (1 - x**2 + 1j * x / Q)
peak_db = 0   # replace
print(peak_db)
```

```python solution
import numpy as np

f0 = 1000.0
f = np.logspace(1, 5, 4000)
x = f / f0
H = 1 / (1 - x**2 + 1j * x / Q)
peak_db = np.max(20 * np.log10(np.abs(H)))
print(peak_db)
```

```python tests
import math
want = 20 * math.log10(Q / math.sqrt(1 - 1 / (4 * Q * Q)))
assert abs(peak_db - want) < 0.02, f"The peak should be {want:.3f} dB; yours is {peak_db}."
```

::: right
Right. The peak is $Q/\sqrt{1 - 1/4Q^2}$, a little above Q, at slightly below $f_0$.
:::
:::

::: explain
## Why is it worth computing a filter's response from its final component values, rather than trusting the design formulas?

::: model
The design formulas give ideal values, but real parts come in standard values with tolerances, and the formulas themselves can be misapplied. Computing the response from the values actually chosen shows the corner frequency, Q, peaking and attenuation the circuit will really have. It catches algebra mistakes, shows how much rounding to standard values moved the response, and confirms the specification is still met before anything is built.
:::
:::

::: practice

::: check
given: Q = 0.707 | 1 | 1.5
answer: 4*Q^2
tolerance: 1%

## Equal-resistor Sallen-Key: $C_1/C_2$ for Q = {Q}?

::: misses
- [2*Q] $C_1/C_2 = 4Q^2$.
- [any] $4Q^2$.
:::
:::

::: check
given: Q = 1 | 1.5 | 2
answer: 20*log10(Q/sqrt(1 - 1/(4*Q^2)))
unit: dB
tolerance: 0.5%

## Peak gain of a second-order low-pass with Q = {Q}, in dB?

::: misses
- [20*log10(Q)] That's the gain at exactly $f_0$; the peak is slightly higher.
- [any] $20\log_{10}\frac{Q}{\sqrt{1 - 1/4Q^2}}$.
:::
:::

::: check
given: C1 = 15 | 22 | 33 | 47 | 68 | 100; C2 = 10
answer: 0.5*sqrt(C1/C2)
tolerance: 0.5%

## $C_1$ = {C1} nF and $C_2$ = 10 nF with equal resistors. Q?

::: misses
- [any] $\tfrac12\sqrt{C_1/C_2}$.
:::
:::
:::
