Radio from scratch · Chapter 9

FM: listening to the speed

Before you startThis chapter uses:

Everything so far has been preparation. After chapters 5 to 7, we have one station, centered at 0, with its neighbors removed, at a few hundred thousand samples per second. In chapter 5's lab you saw what's left of its arrow: it doesn't go round any more, it just wobbles back and forth. Chapter 1 told us what that wobble is: the music, written into the arrow's speed (↩ ch. 1).

So the last real step is to measure the speed, sample by sample. Speed is "how far it turned since last time". So we need one thing: the angle between two consecutive snapshots.

How far did it turn?

We could compute each snapshot's angle and subtract. But angles wrap around (from 179° to −179° is a turn of 2°, not −358°), which makes subtraction fiddly. Chapter 2 has a cleaner trick. Multiplying arrows adds their angles (↩). So to subtract an angle, multiply by the arrow's mirror image: the same arrow flipped upside down, which has the opposite angle. Mathematicians call the mirror image the complex conjugate.

Predict before you drag: "before" points at −170° and "now" at 170°. Which angle will the pink arrow (now × mirrored before) point at, in degrees?

170° − (−170°) = 340°, which is the same direction as −20°. The arrow turned 20° clockwise, the short way round, and the multiplication gets that right with no wrapping code at all.

Drag the two arrows:

The pink arrow, "now × mirrored before", always points at exactly the angle between the two. No wrapping problems: if "before" is at 179° and "now" at −179°, the pink arrow points at 2°, as it should.

And notice: we only need the pink arrow's angle, not its length. That turns out to be one of FM's great strengths, as you'll see in a moment.

The demodulator, step by step

Before you tick "Signal fades": if the signal drops to half its strength, what happens to the FM demodulator's output?

Only the angle between samples counts, and fading changes lengths, not angles. Try it in the scene.

Now apply it to a whole FM signal. For every sample, multiply it by the mirrored previous sample and take the angle. Each answer is one bar below. The dashed green line is the music that was put into the signal:

With no noise, the pink bars trace the music exactly. That's the whole FM demodulator: one multiplication and one angle per sample. (The yellow line is what's left after the audio filter that follows, a short running average: it's what you'd actually hear.) Now experiment:

FM's sound is how far the arrow turns between samples: multiply each sample by the mirrored previous one and take the angle. Lengths never enter, so fading doesn't either.

Why does the mirrored multiplication give exactly the turn?
  1. Write the new sample as length r₁ at angle a, the previous one as length r₀ at angle b.
  2. Mirroring the previous one flips its angle: length r₀ at angle −b.
  3. Multiplying multiplies lengths and adds angles (chapter 2): length r₁·r₀ at angle a − b.
  4. a − b is the turn between the samples, already as a direction, so no wrap-around arithmetic is needed.
  5. The length r₁·r₀ never enters the angle, so a fading signal gives the same output.
Where it breaks

The demodulator can only see turns of less than half a turn per sample (chapter 3's honest zone). At the 300 kS/s the receiver keeps for broadcast FM (chapter 7), half a turn per sample is a speed of 150 kHz from the center. A broadcast station stays within about ±100 kHz (it's 200 kHz wide), so it fits. Decimate further and the fast moments of the music would wrap round and come out as loud wrong values: the sample rate has to stay above the station's width, which is the same rule as chapter 7's.

What's inside a real broadcast

Why can't we just take the demodulator's 300 kS/s output and decimate it straight down to 48 kS/s for your speakers? Because a real FM station packs several hidden signals into the arrow's speed above 15 kHz, where the station's sound ends — and if we don't filter them off before decimating to audio speed (↩ ch. 7), they fold right down on top of the music as whistles and hiss. Here's the spectrum (↩ ch. 4) of our demodulator's output for 98.7 FM, recorded by this site's dongle, from 0 to 64 kHz.

Our receiver plays the mono part only. Stereo and RDS would be good projects for after the course.

The pilot is at 19 kHz and the stereo part sits around exactly twice the pilot. At what frequency, in kHz?

2 × 19 = 38 kHz. Locking onto the pilot and doubling it gives the receiver a perfectly matched reference for shifting the stereo band back down.

De-emphasis: why FM turns its treble down

Even after we low-pass filter at 15 kHz to keep only the mono audio, playing that signal straight to your speakers sounds tinny, overly bright, and full of high-pitched hiss. Why?

Predict: noise jiggles the arrow's angle at random. Why would that come out louder at high pitches than low ones?

Speed of change is bigger for quick wiggles than slow ones, so the demodulated noise rises with frequency. De-emphasis turns the treble down by exactly what the station boosted, taking the hiss down with it.

FM has a quirk. Noise in the radio signal pushes the arrow around randomly, and the demodulator turns those pushes into sound. But a push changes the arrow's angle by a random amount, and the demodulator reports the angle's rate of change, which is larger for fast jitter. So FM noise comes out louder at high pitches: a hiss that rises with frequency (red below).

The fix is a deal made in the 1940s. Broadcasters deliberately boost the treble before transmitting (pre-emphasis), and every receiver turns the treble down by exactly the same amount (de-emphasis, green). The music comes out balanced, and the high-pitched hiss gets turned down along with the treble. In the Americas the amount is set by a "75 µs" time constant (µs: a microsecond, a millionth of a second). It's how quickly the filter reacts, and it sets where the treble cut begins: 1 ÷ (2π × 75 µs) ≈ 2.1 kHz. Below that, almost nothing changes (−0.9 dB at 1 kHz); above it the cut grows (−14 dB at 10 kHz). Europe uses 50 µs.

De-emphasis is just a gentle low-pass filter: each output moves a small fraction of the way towards the new input (a running average with a long memory, ↩ ch. 6). In an old analog radio it was literally a resistor and a capacitor with R × C = 75 µs, for example 75 kΩ and 1 nF: you can build it in Electronics ch. 2.

Lab: listen to FM you decoded yourself

This is the whole receiver, built from the blocks of chapters 5–9 (mixer, filter + decimator, FM demodulator, de-emphasis, audio filter), playing live radio. The picture is the demodulator's output: watch for the 19 kHz pilot. Turn de-emphasis off and listen to the hiss and the harsh treble.

Writing it down

audio[n]=angle(x[n]·x[n−1]‾) (a+jb)(c−jd)=(ac+bd)+j(bc−ad)

In code

Show the real code (optional: everything it does is explained above)

The FM demodulator in this site's receiver, with the previous sample remembered between chunks (↩ ch. 8):

export class FmDemod {
  private pi = 0;   // the previous sample (I and Q)
  private pq = 0;
  process(iq: Float32Array): Float32Array {
    const out = new Float32Array(iq.length / 2);
    let pi = this.pi, pq = this.pq;
    for (let k = 0; k < iq.length; k += 2) {
      const i = iq[k], q = iq[k + 1];
      out[k / 2] = Math.atan2(q * pi - i * pq, i * pi + q * pq);   // angle of now × mirrored before
      pi = i; pq = q;
    }
    this.pi = pi; this.pq = pq;
    return out;
  }
}

De-emphasis (Deemphasis): each output moves a fraction a of the way towards the input; a comes from the 75 µs time constant and the sample rate:

Names in this code: y is the output so far; each new output moves the fraction a of the way toward the input x[i]. Math.exp is ex.

this.a = 1 - Math.exp(-1 / (fs * tau));                     // tau = 75e-6
for (let i = 0; i < x.length; i++) out[i] = this.y += this.a * (x[i] - this.y);

Where this chapter's numbers come from. The numbers this chapter computes come from a short checking script behind this page, which also checks each claim and stops if one is wrong. Its output:

recovered tone: 997 Hz, peak speed 5000 Hz (the deviation)
steady vs fading (down to 10% strength): outputs differ by at most 1.2e-7
noise 0.05 × the signal: 0 clicks per second
noise 0.20 × the signal: 0 clicks per second
noise 0.40 × the signal: 678 clicks per second
noise 0.70 × the signal: 5757 clicks per second
de-emphasis at   100 Hz: -0.0 dB
de-emphasis at  1000 Hz: -0.9 dB
de-emphasis at  3000 Hz: -4.8 dB
de-emphasis at 10000 Hz: -13.7 dB
de-emphasis at 15000 Hz: -17.0 dB
98.7 FM in the embedded samples (3.9 ms): 19 kHz pilot 24 dB above its neighbors → stereo
all checks passed

Look at the clicks: none at all with noise up to 0.2, then hundreds per second at 0.4. That's the threshold effect, in numbers.

Check yourself

Why multiply by the mirrored previous sample instead of subtracting the two angles?

Multiplying by the mirror image subtracts the angles automatically, and the result never has a wrap-around problem: a turn from 179° to −179° comes out as 2°.

Why does FM sound steady even when the signal strength changes?

The demodulator uses only angles, never lengths. A weaker signal has a shorter arrow, but it turns in exactly the same way.

Why does a weak FM station crackle instead of just getting quieter?

Noise sometimes pushes the arrow near the center, where a tiny push changes its angle by a lot. Each such moment gives a huge wrong reading, heard as a click. Below a certain signal strength these happen many times per second.

What does the 19 kHz pilot tell a receiver?

That the station is in stereo, and, by its exact timing, how to decode the stereo signal around 38 kHz (twice its frequency).

Last updated