---
title: Noise and distortion lab
heading: Integrated noise, noise figure, harmonics and intercepts, in Python
eyebrow: S2 · Lesson 37 · Noise and distortion lab
xp: 60
---

::: hook
Noise sets the smallest signal a circuit can handle and distortion sets the largest, and between them lies its dynamic range. The formulas for both are first-order approximations: flicker noise integrated over a band, the noise figure from a source, harmonics from a power series, and the bipolar pair's $\frac{1}{48}(v/V_T)^2$ third harmonic. This lab computes each exactly in Python, including a discrete Fourier transform of a real tanh pair's output, and measures how far the approximations hold.
:::

::: play
- The curves are a bipolar differential pair's third-harmonic distortion against input amplitude: the series approximation $\frac{1}{48}(v/V_T)^2$ and the exact result from the tanh characteristic.
- They agree closely at a few millivolts and part as the amplitude grows: the next term of the series has the opposite sign, so the exact HD3 falls slightly below the approximation.
- The lab computes the exact curve with a Fourier transform.
:::

```sim plot
view: graph
x: v = 1..50 mV log
y: h = 0.00001..0.1 log
plot: (v/25.85)^2/48 | label: series approximation
```

::: predict
## A bipolar pair is driven with 10 mV peak. What does the series approximation give for HD3, and is the exact value higher or lower?

- [x] 0.31%, and the exact value is about 1% lower
- [ ] 0.31%, and the exact value is about 10% higher
- [ ] 3.1%, exactly
- [ ] 0.031%

::: reveal
$(10/25.85)^2/48 = 0.312\%$. The lab's Fourier transform of $\tanh(v\sin\omega t/2V_T)$ gives 0.309%, 0.9% lower: the series is excellent at small amplitudes and drifts as they grow (3.6% low at 20 mV).
:::
:::

::: nameit
## The tools

- **Integrated noise:** with white density $e_w$ and flicker corner $f_c$, the rms noise from $f_1$ to $f_2$ is $\sqrt{e_w^2[(f_2 - f_1) + f_c\ln(f_2/f_1)]}$.
- **Harmonics from a power series** $y = a_1x + a_2x^2 + a_3x^3$ for $x = A\cos\omega t$: $HD_2 \approx a_2A/(2a_1)$ and $HD_3 \approx a_3A^2/(4a_1)$.
- **Third-order intercept** (input amplitude): $A_{IP3} = \sqrt{\tfrac43|a_1/a_3|}$.
- **Exact harmonics:** sample one period of the output at N points and take the DFT magnitudes at bins 1 and 3.
:::

::: check
kind: python

## Write `integrated_noise(ew, fc, f1, f2)`: rms noise voltage over the band.

```python starter
import math

def integrated_noise(ew, fc, f1, f2):
    return ew * math.sqrt(f2 - f1)
```

```python solution
import math

def integrated_noise(ew, fc, f1, f2):
    return math.sqrt(ew * ew * ((f2 - f1) + fc * math.log(f2 / f1)))
```

```python tests
assert abs(integrated_noise(10e-9, 1e3, 1, 1e4) * 1e6 - 1.386) < 0.001, "10 nV/√Hz, 1 kHz corner, 1 Hz to 10 kHz: 1.39 µV."
assert abs(integrated_noise(10e-9, 0, 1, 1e4) * 1e6 - 1.0) < 1e-4, "White only: 10 nV × √9999, about 1 µV."
```

::: right
Right. Each decade of band contributes $f_c\ln 10$ of flicker noise power, the same for every decade.
:::
:::

::: check
kind: python

## Write `harmonics(a1, a2, a3, A)`: return (HD2, HD3) from the power-series approximations, and `iip3(a1, a3)`: the input amplitude at the third-order intercept.

```python starter
import math

def harmonics(a1, a2, a3, A):
    return (0.0, 0.0)

def iip3(a1, a3):
    return 1.0
```

```python solution
import math

def harmonics(a1, a2, a3, A):
    return (a2 * A / (2 * a1), a3 * A * A / (4 * a1))

def iip3(a1, a3):
    return math.sqrt(4 / 3 * abs(a1 / a3))
```

```python tests
h2, h3 = harmonics(1, 0.1, -0.1, 0.2)
assert abs(h2 - 0.01) < 1e-12 and abs(h3 + 0.001) < 1e-12, "A = 0.2 V: HD2 = 1%, HD3 = −0.1%."
assert abs(iip3(1, -0.1) - 3.651) < 1e-3, "a3 = −0.1: intercept at 3.65 V."
```

::: right
Right. Doubling the input doubles HD2 and quadruples HD3; that's how the intercept extrapolates from measurements.
:::
:::

::: check
kind: python

## Write `pair_hd3(v_mv, N=4096)`: the exact HD3 of a bipolar pair, the ratio of the third to the first DFT magnitude of $\tanh(v\sin(2\pi k/N)/2V_T)$ over one period (k = 0..N−1, $V_T$ = 25.85 mV).

```python starter
import math

def pair_hd3(v_mv, N=4096):
    return (v_mv / 25.85) ** 2 / 48
```

```python solution
import math

def pair_hd3(v_mv, N=4096):
    out = [math.tanh(v_mv * math.sin(2 * math.pi * k / N) / (2 * 25.85)) for k in range(N)]
    def mag(m):
        re = sum(o * math.cos(2 * math.pi * m * k / N) for k, o in enumerate(out))
        im = sum(o * math.sin(2 * math.pi * m * k / N) for k, o in enumerate(out))
        return math.hypot(re, im)
    return mag(3) / mag(1)
```

```python tests
e = pair_hd3(10)
assert abs(e - 0.003089) < 2e-6, "10 mV: exact HD3 0.309%."
approx = (10 / 25.85) ** 2 / 48
assert abs(e / approx - 0.9907) < 0.001, "About 0.9% below the series."
assert pair_hd3(20) / ((20 / 25.85) ** 2 / 48) < 0.97, "At 20 mV the series overestimates by over 3%."
```

::: right
Right. The DFT of one exact period has no leakage, so the harmonics come out to rounding accuracy.
:::
:::

::: check
kind: python

## Write `dynamic_range(noise_rms, a1, a3, hd3_max)`: the largest amplitude whose HD3 (from the series) stays under `hd3_max`, divided by the rms noise, returned in dB; amplitudes are peak, so convert to rms by $/\sqrt2$.

```python starter
import math

def dynamic_range(noise_rms, a1, a3, hd3_max):
    return 0.0
```

```python solution
import math

def dynamic_range(noise_rms, a1, a3, hd3_max):
    A = math.sqrt(4 * a1 * hd3_max / abs(a3))
    return 20 * math.log10(A / math.sqrt(2) / noise_rms)
```

```python tests
dr = dynamic_range(1e-6, 1, -0.1, 1e-3)
assert abs(dr - 20 * math.log10(0.2 / math.sqrt(2) / 1e-6)) < 1e-9, "HD3 of 0.1% allows 0.2 V peak: 103 dB over 1 µV rms."
assert dynamic_range(1e-6, 1, -0.1, 1e-2) - dr > 9.9, "Allowing ten times the distortion adds 10 dB."
```

::: right
Right. Noise fixes the bottom and distortion the top; dynamic range is the distance between them.
:::
:::

::: explain
## Why does the series approximation for a bipolar pair's third harmonic overestimate the exact value at larger amplitudes?

::: model
The approximation keeps only the first nonlinear term of the tanh's Taylor series, the cubic, which makes the third harmonic grow as the square of the amplitude. The next term, the fifth-order one, has the opposite sign, and it also contributes to the third harmonic, in proportion to the amplitude to the fourth power. At small amplitudes that contribution is negligible, but as the drive grows it subtracts a growing amount, so the exact third harmonic falls below the quadratic prediction. At a few millivolts the two agree to under a percent; at 20 mV the gap is several percent.
:::
:::
