---
title: "Analog modulation lab: spectra, sidebands and demodulators"
heading: Measuring FM's Bessel sidebands and Carson's rule, AM's power efficiency, an FM discriminator and a phasing SSB modulator's image rejection from simulated signals
eyebrow: S8 · Lesson 11 · Analog modulation lab
xp: 60
---

::: hook
Lessons 2 to 7 predicted what analog modulation does to a spectrum: AM puts a carrier and two sidebands around $f_c$, FM spreads a tone into Bessel-weighted lines, a phasing modulator with imperfect parts leaks an image. In this lab you'll generate those signals sample by sample, measure their spectra with a discrete Fourier transform, and check every prediction to many digits. When the number of carrier and message cycles in the record is a whole number, the DFT bins land exactly on the lines, and the measurements match the formulas almost to rounding error.
:::

::: play
- The curves are the magnitudes of the first three Bessel functions, which set the amplitudes of FM's carrier and its first two pairs of sidebands against the modulation index β.
- The carrier vanishes at β = 2.405, the first zero of J₀: all the power has moved into the sidebands.
- Power is conserved: $J_0^2 + 2\sum J_k^2 = 1$ for every β, so FM's envelope stays constant however the lines rearrange.
:::

```sim plot
view: graph
x: b = 0..8
y: A = 0..1
plot: abs(J0(b)) | label: carrier |J₀(β)|
plot: abs(J1(b)) | label: first sidebands |J₁(β)|
plot: abs(J2(b)) | label: second sidebands |J₂(β)|
```

::: predict
## An FM transmitter with a 1 kHz test tone raises its deviation until a spectrum analyzer shows the carrier line disappearing. What is the deviation?

- [ ] 1 kHz
- [x] 2.405 kHz
- [ ] 5 kHz
- [ ] The carrier never disappears

::: reveal
The carrier's amplitude is J₀(β), whose first zero is β = 2.405; with $\beta = \Delta f/f_m$ and a 1 kHz tone, the deviation is 2.405 kHz. This carrier-null method is how FM deviation meters are calibrated, since it needs only a frequency and a null.
:::
:::

::: nameit
## What the lab computes

For a record of N samples, the DFT bin k is $X_k = \sum_n x_n e^{-j2\pi kn/N}$, and a real sinusoid that completes exactly k cycles in the record has one-sided amplitude $2|X_k|/N$. Parseval's theorem gives the power in any set of bins as a fraction of the total:
$$\frac{\sum_{k \in S}|X_k|^2}{\sum_k |X_k|^2}$$

- FM: $\cos(2\pi f_cn/N + \beta\sin 2\pi f_mn/N)$ has lines $|J_k(\beta)|$ at $f_c \pm kf_m$.
- AM: $(1 + \mu\cos 2\pi f_mn/N)\cos 2\pi f_cn/N$ puts a fraction $\mu^2/(2 + \mu^2)$ of its power in the sidebands.
- A discriminator on the complex baseband signal $z_n = e^{j\phi_n}$ reads the phase step $\arg(z_nz_{n-1}^*)$, the instantaneous frequency in radians per sample.
- A phasing SSB modulator with gain ratio a and phase error φ in its quadrature path leaves the unwanted sideband $\frac{1 + 2a\cos\phi + a^2}{1 - 2a\cos\phi + a^2}$ below the wanted one.
:::

::: check
kind: python

## Write `bin_amp(x, k)`: the one-sided amplitude $2|X_k|/N$ of DFT bin k of the real list x, and `fm_lines(beta, fc, fm, N)`: generate N samples of an FM tone with fc and fm in cycles per record, and return the amplitudes of the bins $f_c + kf_m$ for k = 0 to 4.

```python starter
def bin_amp(x, k):
    return 0.0

def fm_lines(beta, fc, fm, N):
    return [0.0] * 5
```

```python solution
import cmath, math

def bin_amp(x, k):
    N = len(x)
    return 2 * abs(sum(v * cmath.exp(-2j * math.pi * k * n / N) for n, v in enumerate(x))) / N

def fm_lines(beta, fc, fm, N):
    x = [math.cos(2 * math.pi * fc * n / N + beta * math.sin(2 * math.pi * fm * n / N)) for n in range(N)]
    return [bin_amp(x, fc + k * fm) for k in range(5)]
```

```python tests
import math
def J(n, x):
    s, t = 0.0, (x / 2) ** n / math.factorial(n)
    for k in range(60):
        s += t; t *= -(x / 2) ** 2 / ((k + 1) * (k + 1 + n))
    return s
for beta in (0.5, 2.0, 2.405):
    lines = fm_lines(beta, 200, 10, 1024)
    assert all(abs(lines[k] - abs(J(k, beta))) < 1e-9 for k in range(5)), f"Lines at β = {beta} must be |J_k(β)|."
assert fm_lines(2.405, 200, 10, 1024)[0] < 1e-3, "The carrier nearly vanishes at β = 2.405."
```

::: right
Right. With whole numbers of cycles in the record, each line falls exactly on a bin and the DFT reproduces Bessel's amplitudes to rounding error.
:::
:::

::: check
kind: python

## Write `power_fraction(x, bins)`: the fraction of the real signal's total power that lies in the given one-sided bins (count each bin's negative-frequency twin too), by Parseval over the full DFT.

```python starter
def power_fraction(x, bins):
    return 1.0
```

```python solution
import cmath, math

def power_fraction(x, bins):
    N = len(x)
    X = [sum(v * cmath.exp(-2j * math.pi * k * n / N) for n, v in enumerate(x)) for k in range(N)]
    total = sum(abs(v) ** 2 for v in X)
    want = sum(abs(X[k]) ** 2 + abs(X[(N - k) % N]) ** 2 for k in bins)
    return want / total
```

```python tests
import math
N, fc, fm = 256, 64, 4
for mu in (0.5, 1.0):
    am = [(1 + mu * math.cos(2 * math.pi * fm * n / N)) * math.cos(2 * math.pi * fc * n / N) for n in range(N)]
    assert abs(power_fraction(am, [fc - fm, fc + fm]) - mu ** 2 / (2 + mu ** 2)) < 1e-9, "AM sidebands carry μ²/(2 + μ²)."
fmx = [math.cos(2 * math.pi * fc * n / N + 2 * math.sin(2 * math.pi * fm * n / N)) for n in range(N)]
carson = power_fraction(fmx, [fc + k * fm for k in range(-3, 4)])
assert 0.98 < carson < 0.999, "Carson's band, β + 1 = 3 sidebands each side, holds about 99% of the power at β = 2."
```

::: right
Right. AM with full modulation still spends two thirds of its power on the carrier, while FM's power stays put and only moves between lines.
:::
:::

::: check
kind: python

## Write `discriminator(z)`: from a list of complex baseband samples, return the phase steps $\arg(z_nz_{n-1}^*)$ for n = 1 to N − 1, in radians per sample.

```python starter
def discriminator(z):
    return [0.0] * (len(z) - 1)
```

```python solution
import cmath

def discriminator(z):
    return [cmath.phase(z[n] * z[n - 1].conjugate()) for n in range(1, len(z))]
```

```python tests
import cmath, math
N, fm, beta = 1000, 5, 3.0
phi = [beta * math.sin(2 * math.pi * fm * n / N) for n in range(N)]
d = discriminator([0.7 * cmath.exp(1j * p) for p in phi])
assert len(d) == N - 1 and max(abs(d[n - 1] - (phi[n] - phi[n - 1])) for n in range(1, N)) < 1e-12, "Each step is the phase change."
assert abs(max(d) - 2 * beta * math.sin(math.pi * fm / N)) < 1e-4, "The largest step is close to 2β sin(πf_m/N), about the peak deviation 2πβf_m/N."
```

::: right
Right. The discriminator ignores the amplitude (0.7 here) entirely, which is why FM shrugs off amplitude noise and fading until the noise gets strong enough to flip the phase.
:::
:::

::: check
kind: python

## Write `ssb_image_db(a, phi_deg, fc, fm, N)`: build N samples of a phasing upper-sideband modulator with a tone message, $\cos(2\pi f_mn/N)\cos(2\pi f_cn/N) - a\sin(2\pi f_mn/N)\sin(2\pi f_cn/N + \phi)$, and return the wanted-to-image power ratio in dB, measured from the DFT bins $f_c + f_m$ and $f_c - f_m$.

```python starter
def ssb_image_db(a, phi_deg, fc, fm, N):
    return 0.0
```

```python solution
import cmath, math

def ssb_image_db(a, phi_deg, fc, fm, N):
    p = math.radians(phi_deg)
    x = [math.cos(2 * math.pi * fm * n / N) * math.cos(2 * math.pi * fc * n / N)
         - a * math.sin(2 * math.pi * fm * n / N) * math.sin(2 * math.pi * fc * n / N + p) for n in range(N)]
    X = lambda k: abs(sum(v * cmath.exp(-2j * math.pi * k * n / N) for n, v in enumerate(x)))
    return 20 * math.log10(X(fc + fm) / X(fc - fm))
```

```python tests
import math
def irr(a, p):
    c = 2 * a * math.cos(math.radians(p))
    return 10 * math.log10((1 + c + a * a) / (1 - c + a * a))
for a, p in ((10 ** (0.2 / 20), 2), (1.1, 5), (0.95, 0.5)):
    assert abs(ssb_image_db(a, p, 50, 6, 256) - irr(a, p)) < 1e-6, f"Image rejection for a = {a:.3f}, φ = {p}°."
assert ssb_image_db(10 ** (0.2 / 20), 2, 50, 6, 256) > 33.5, "0.2 dB and 2° give about 33.6 dB."
```

::: right
Right. Good image rejection needs both errors small at once: 0.2 dB and 2° already cap it near 34 dB, which is why receivers calibrate their I/Q paths.
:::
:::

::: explain
## Why can an FM signal's carrier line vanish completely while the transmitted power stays the same?

::: model
An FM signal has a constant envelope: it's a sinusoid whose phase wanders with the message, so its power is fixed by its amplitude alone, whatever the modulation. Its spectrum, however, depends on how the phase wanders. Writing the signal as a sum of lines gives a carrier of amplitude J₀(β) and sidebands of amplitudes J_k(β), and the squares of all of them always add to one. As the modulation index grows, power moves out of the carrier into the sidebands, and at β = 2.405, where J₀ is zero, none is left at the carrier at all. The power hasn't gone anywhere: it all sits in the sidebands.
:::
:::
