Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 38 ยท Applications

AM and FM Modulation and Demodulation

Modulate and demodulate AM and FM signals in software. Use the analytic signal for envelope and instantaneous frequency and mix to baseband with I/Q processing.

Digital signal processing runs inside software-defined radios, modems and audio effects. All of them rely on a few ideas: put information on a carrier (modulation), shift frequencies by mixing, and recover the information (demodulation). In this lesson you build amplitude modulation (AM) and frequency modulation (FM) from scratch and decode them with the analytic signal.

Amplitude modulation

AM varies the carrier's amplitude with the message m(t): s(t) = (1 + k m(t)) cos(2 pi fc t). The modulation index k (with |m| <= 1) must stay below 1 so the envelope never goes negative; otherwise the envelope no longer equals the message. The spectrum has the carrier plus two sidebands at fc +/- fm.

Python AM signal and its sidebands

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The analytic signal and the Hilbert transform

For a real signal s(t), the analytic signal is z(t) = s(t) + j * H{s}(t), where H is the Hilbert transform (a 90-degree phase shift). It contains only positive frequencies. Its magnitude |z| is the envelope; the unwrapped angle arg z is the instantaneous phase, and the derivative of the phase divided by 2 pi is the instantaneous frequency. scipy.signal.hilbert returns z directly.

From a real signal to the analytic signal

Real s(t) Positive and negative frequencies
Hilbert Adds a 90 degree copy
Magnitude Envelope for AM
Phase slope Frequency for FM

One complex signal gives both envelope and frequency.

1 / 2
Python AM envelope detection

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In the noiseless case the message is recovered almost exactly. With noise, a low-pass filter on the envelope removes the high-frequency part of the noise.

Frequency modulation

FM varies the carrier's frequency: f_inst(t) = fc + kf m(t), with the deviation kf (in Hz per unit of message). The phase is the integral of the frequency, so the signal is s(t) = cos(2 pi fc t + 2 pi kf * integral of m). FM has constant amplitude, which makes it robust against amplitude noise; its bandwidth follows Carson's rule: B ~ 2 (deviation + fm).

Python FM modulation and demodulation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The instantaneous frequency swings between 2600 and 3400 Hz, exactly fc +/- kf, and the recovered message matches m closely. The slice trick above keeps the two arrays aligned: diff shortens the array by one sample.

Mixing, I/Q and baseband

Receivers work at baseband (around 0 Hz) because it is cheap to sample and filter there. To shift a band at fc down to 0 Hz, multiply by a complex exponential exp(-j 2 pi fc t) (a mixer), then low-pass filter. The result is a complex baseband signal with I (in-phase, real) and Q (quadrature, imaginary) parts. Both AM and FM are recovered from the I/Q samples: amplitude is sqrt(I^2 + Q^2); frequency is the rate of change of atan2(Q, I).

Python Mixing an FM signal to baseband

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

After mixing, the carrier is removed and the baseband frequency swings between -400 and +400 Hz, the message scaled by kf. The envelope is nearly constant, as expected for FM.

Summary of the demodulators

  • AM: envelope = |analytic signal| or |I + jQ|; remove the DC offset and scale.
  • FM: instantaneous frequency = derivative of the unwrapped phase.
  • Phase-locked loops and quadrature detectors do the same in hardware; the idea is identical.
  • Digital modulation (BPSK, QAM) uses the same I/Q baseband representation with discrete symbols.

The spectrogram view from the STFT lesson is a handy way to see an FM signal's frequency sweep.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the envelope of an AM signal in terms of the analytic signal z?

    The magnitude of the analytic signal tracks the amplitude variations, which is the AM envelope.
  2. 2. How do you get the instantaneous frequency of a signal?

    Frequency is the rate of change of phase, so the derivative of the unwrapped angle, scaled by 1/(2 pi), gives Hz.
  3. 3. Why must the AM modulation index stay below 1 for envelope detection?

    When 1 + k m(t) becomes negative, the envelope (a magnitude) no longer equals the message.
  4. 4. Carson's rule gives the bandwidth of an FM signal as roughly

    Most of the FM power lies within twice the sum of the peak deviation and the highest message frequency.

Summary

  • AM puts the message on the carrier's amplitude; FM puts it on the carrier's frequency.
  • The analytic signal from the Hilbert transform gives envelope (magnitude) and instantaneous frequency (phase slope).
  • Mixing with a complex oscillator and low-pass filtering produces I/Q baseband samples used by modern receivers.
  • Noise is reduced with low-pass filtering after demodulation.

Next: Pitch Detection