---
title: "Software radio: FM demodulation"
heading: Frequency modulation as a phasor whose speed follows the audio, the modulation index and Carson's bandwidth, the discriminator that turns phase steps between samples into frequency, why FM ignores amplitude, and the SNR it trades bandwidth for
eyebrow: D13 · Lesson 6 · Software radio
xp: 40
---

::: hook
At baseband, an FM station is a phasor of constant length whose angle swings back and forth with the audio: the louder the sound, the faster it turns. To get the audio back, a software radio only has to measure how far the phasor turns from one sample to the next. That's one multiplication and one angle per sample, the **discriminator**, and because it looks only at angles, it ignores fading and amplitude noise that would wreck an AM receiver.
:::

::: play
- The complex baseband phasor of an FM signal, $e^{j\beta\sin(2\pi f_mt)}$, modulated by a tone of frequency $f_m$ with peak deviation Δf. Play it in slow motion.
- The phasor swings through ±β radians, where β = Δf/$f_m$. Its angular speed, not its angle, carries the audio: it turns fastest through the middle of each swing.
- Raise the deviation: the swing widens and the signal's bandwidth grows, as Carson's rule in the readout shows.
:::

```sim plot
view: complex
range: 1.4
time: tau
slider: D = 500..5000 step 100 = 2500 Hz
slider: m = 300..3000 step 100 = 1000 Hz
arrow: exp(j*(D/m)*sin(2*pi*0.3*tau)) | label: FM phasor
curve: exp(j*u) | over: u = -D/m..D/m | dashed
readout: modulation index β = D/m | digits: 3
readout: Carson bandwidth, kHz = 2*(D + m)/1000 | digits: 3
```

::: predict
## A discriminator compares each sample with the one before. If the signal arrives 100 times weaker, what happens to its output?

- [ ] It's 100 times smaller
- [ ] It's 10 times smaller
- [x] It's unchanged
- [ ] It's 100 times larger

::: reveal
The discriminator takes the angle of $z[n]\overline{z[n-1]}$, and scaling z by any constant scales that product by a positive number without changing its angle. So the audio comes out the same at any signal strength, as long as the signal stays well above the noise. That's FM's resistance to fading, and why it needs no automatic gain control.
:::
:::

::: nameit
## Frequency modulation

The audio a(t) moves the instantaneous frequency by Δf × a(t), so the phase is the running integral:

$$z(t) = e^{j2\pi\Delta f\int a\,dt}, \qquad z[n] = e^{j2\pi\Delta f\sum_k a[k]/f_s}$$

For a tone, the phase swings by the **modulation index** $\beta = \Delta f/f_m$, and **Carson's rule** puts 98% of the power within $B_T = 2(\Delta f + f_m)$. Narrowband FM voice uses Δf = 2.5 to 5 kHz; broadcast FM uses 75 kHz.

## The discriminator

$$f_\text{inst}[n] = \frac{f_s}{2\pi}\arg\left(z[n]\,\overline{z[n-1]}\right)$$

is the frequency in hertz, and dividing by Δf gives back a(t). A tuning error shows up as a steady offset in the output, which a receiver can use to correct its tuning. The phase step between samples must stay within ±π, so $f_s$ must exceed twice the highest instantaneous frequency.

## What FM buys

Above its **threshold**, FM's output SNR for a tone is $1.5\beta^2$ times what the same received power gives as plain baseband audio, the baseband reference $\gamma = S/(N_0W)$ of S8: bandwidth traded for SNR. The threshold sits near γ = 20(β + 1); below it, noise spikes ("clicks") take over quickly. Broadcast FM also boosts the treble before transmission (**pre-emphasis**, a 75 µs time constant in North America) and cuts it after demodulation, since the discriminator's output noise rises with audio frequency.
:::

::: example
## A 1 kHz tone at full scale deviates a narrowband station by 2.5 kHz. What's β, the bandwidth, and what does the lab's discriminator output?

::: solution
1. **Modulation index.**
   $$\begin{aligned} \beta &= \frac{2.5\ \text{kHz}}{1\ \text{kHz}} \\ &= 2.5 \end{aligned}$$
2. **Bandwidth by Carson's rule.**
   $$\begin{aligned} B &= 2(2500 + 1000)\ \text{Hz} \\ &= 7\ \text{kHz} \end{aligned}$$
3. **Discriminator output.** A 1 kHz sine of peak 2500 Hz, whose RMS value is
   $$\frac{2500}{\sqrt{2}}\ \text{Hz} = 1768\ \text{Hz}$$
4. **Recover the tone.** Divided by Δf it's the original tone.

**Answer:** β = 2.5 and a 7 kHz bandwidth, and the discriminator puts out a 1 kHz sine of 2500 Hz peak (1768 Hz RMS).
:::
:::

::: example
## What output SNR advantage does β = 5 give over plain baseband audio, above threshold, and where's the pre-emphasis corner for 75 µs?

::: solution
1. **SNR advantage.**
   $$\begin{aligned} 1.5\beta^2 &= 1.5 \times 25 \\ &= 37.5 \end{aligned}$$
   or 15.7 dB over baseband.
2. **Bandwidth it costs.**
   $$2(5 + 1) = 12$$
   times the audio bandwidth.
3. **Threshold.** The advantage holds only above
   $$\begin{aligned} \gamma &= 20 \times 6 \\ &= 120 \end{aligned}$$
   or 20.8 dB.
4. **Pre-emphasis corner.**
   $$\begin{aligned} f &= \frac{1}{2\pi \times 75\ \mu\text{s}} \\ &= 2122\ \text{Hz} \end{aligned}$$

**Answer:** 37.5 times (15.7 dB) better than baseband above a threshold of γ = 120 (20.8 dB), and a pre-emphasis corner at 2122 Hz.
:::
:::

::: check
kind: python

## Write an FM modulator and a discriminator.

```python starter
import numpy as np

def fm_modulate(audio, fs, dev):
    # Frequency modulation at baseband: the phase is 2*pi*dev*(running sum of audio)/fs, so an
    # audio value of 1 moves the frequency by dev Hz. Return exp(1j*phase).
    return np.ones(len(audio), dtype=complex)

def fm_demod(z, fs):
    # The instantaneous frequency in Hz between neighboring samples: the angle of
    # z[n]*conj(z[n-1]), times fs/(2*pi). The result is one sample shorter than z.
    return np.zeros(len(z) - 1)
```

```python solution
import numpy as np

def fm_modulate(audio, fs, dev):
    phase = 2*np.pi*dev*np.cumsum(audio)/fs
    return np.exp(1j*phase)

def fm_demod(z, fs):
    return np.angle(z[1:]*np.conj(z[:-1]))*fs/(2*np.pi)
```

```python tests
_fs = 9600
_t = np.arange(9600)/_fs
_a = np.sin(2*np.pi*1000*_t)
_z = fm_modulate(_a, _fs, 2500)
assert np.allclose(np.abs(_z), 1), "FM keeps a constant amplitude."
_f = fm_demod(_z, _fs)
assert len(_f) == 9599, "One output per pair of neighbors."
assert abs(np.sqrt(np.mean(_f**2)) - 2500/np.sqrt(2)) < 5, f"A full-scale 1 kHz tone at 2.5 kHz deviation demodulates to a 2500 Hz-peak sine, 1768 Hz RMS; got {np.sqrt(np.mean(_f**2)):.0f}."
assert abs(np.argmax(np.abs(np.fft.rfft(_f[:4800])))*_fs/4800 - 1000) < 1, "The recovered audio is the 1 kHz tone."
assert np.allclose(fm_demod(0.01*_z, _fs), _f), "The amplitude doesn't matter to a discriminator: a weak signal demodulates the same."
_off = _z*np.exp(2j*np.pi*300*np.arange(9600)/_fs)
assert abs(np.mean(fm_demod(_off, _fs)) - 300) < 1, "A tuning error of 300 Hz shows up as a steady 300 Hz in the output."
```

::: right
Right. Your discriminator recovers the tone at its deviation, ignores the signal's amplitude and turns a tuning error into a steady offset.
:::
:::

::: check
given: D = 2500 | 5000 | 75000; m = 1000 | 3000 | 15000
answer: 2*(D + m)/1000
unit: kHz
tolerance: 0.5%

## An FM signal deviates {D} Hz with audio up to {m} Hz. What bandwidth does Carson's rule give?

::: right
Right. $2(\Delta f + f_m)$ = {answer} kHz.
:::

::: misses
- [2*D/1000] Add the audio bandwidth.
- [(D + m)/1000] Carson's rule counts both sides of the carrier.
- [any] Not yet. Twice the deviation plus the audio bandwidth.
:::
:::

::: check
given: b = 1 | 2 | 5 | 10
answer: 10*log(1.5*b^2)
unit: dB
tolerance: 0.5%

## Above threshold, how much SNR does FM with modulation index {b} gain over plain baseband audio of the same power?

::: right
Right. $1.5\beta^2$ = {answer} dB.
:::

::: misses
- [10*log(3*b^2*(b + 1))] That compares with the carrier-to-noise ratio in Carson's bandwidth; against the baseband reference it's $1.5\beta^2$.
- [any] Not yet. For a tone, FM's figure of merit is $1.5\beta^2$.
:::
:::

::: check
given: T = 25 | 50 | 75
answer: 1e6/(2*pi*T)
unit: Hz
tolerance: 0.5%

## FM pre-emphasis uses a {T} µs time constant. Where's its corner?

::: right
Right. $1/(2\pi\tau)$ = {answer} Hz.
:::

::: misses
- [1e6/T] Divide by 2π.
- [any] Not yet. The RC corner.
:::
:::

::: check
kind: parts
given: df = 5 | 30 | 75; fm = 1 | 3 | 15

## An FM signal has a peak deviation of {df} kHz and is modulated by a {fm} kHz tone.

::: part
answer: df/fm
tolerance: 0.5%

### What is its modulation index?

::: misses
- [fm/df] β is deviation over modulating frequency.
- [any] Not yet. $\beta = \Delta f/f_m$.
:::
:::

::: part
answer: 2*(df + fm)
unit: kHz
tolerance: 0.5%

### What bandwidth does Carson's rule give?

::: misses
- [2*df] Add the modulating frequency: $2(\Delta f + f_m)$.
- [df + fm] Carson's rule is twice that: both sides of the carrier.
- [any] Not yet. $B = 2(\Delta f + f_m)$.
:::
:::

::: right
Right. Broadcast FM's 75 kHz deviation and 15 kHz audio give 180 kHz, which is why stations sit 200 kHz apart.
:::
:::

::: check
given: b = 1 | 2 | 5
answer: 10*log(1.5*b^2)
tolerance: 0.5%

## For tone modulation with index {b}, an FM receiver above threshold improves the SNR over baseband by $1.5\beta^2$. How many dB is that?

::: right
Right. $10\log_{10}(1.5\beta^2)$ = {answer} dB: FM trades bandwidth for noise immunity.
:::

::: misses
- [20*log(1.5*b^2)] It's a power ratio: 10 log.
- [10*log(b^2)] Include the factor 1.5.
- [any] Not yet. $10\log_{10}(1.5\beta^2)$.
:::
:::

::: explain
## Why does FM reject amplitude noise and fading that would distort AM?

::: model
In FM, all the information is in how fast the phase turns, while the amplitude is constant at the transmitter. The discriminator measures only the angle between successive samples, so anything that scales the signal's amplitude, whether fading, a weaker station or amplitude noise, changes the length of the phasor but not the angle it turns through, and the audio comes out the same. AM, by contrast, carries its audio in the amplitude itself, so every amplitude change appears directly in the output. FM's immunity holds as long as the signal stays well above the noise; once noise is comparable to the signal, it can swing the phase on its own and the output collapses into clicks.
:::
:::

::: practice

::: check
given: D = 1000 | 100000; m = 100 | 5000
answer: 2*(D + m)/1000
unit: kHz
tolerance: 0.5%

## Carson: {D} Hz deviation, {m} Hz audio?

::: misses
- [any] $2(\Delta f + f_m)$.
:::
:::

::: check
given: b = 3 | 7 | 20
answer: 10*log(1.5*b^2)
unit: dB
tolerance: 0.5%

## FM advantage over baseband at β = {b}?

::: misses
- [any] $1.5\beta^2$.
:::
:::
:::
