A450 of 56
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

A4 · Lesson 50 · Sampling and the FFT

What frequencies are in this signal?

A recording of a motor's vibration looks like noise in time. Its spectrum shows a sharp peak at the shaft speed and another at twice that, which says exactly which part is out of balance. The fast Fourier transform turns samples in time into amplitudes at frequencies, and NumPy does it in one call. Reading the result correctly takes three ideas: which frequencies sampling can represent, which frequency each output bin stands for, and how to turn a bin's value into an amplitude.

Predict

A 7 kHz tone is sampled at 10 kHz. What frequency do the samples look like?

Name it

Sampling and aliasing

Sampling at rate fs captures frequencies from 0 up to fs/2. A tone at frequency f shows up at its alias:

falias=|f−kfs|with k the whole number that brings it into [0,fs/2]

In code: fa = abs(f - fs * round(f / fs)).

The FFT of real samples

For N samples taken at fs:

X = np.fft.rfft(x)                   # N//2 + 1 complex values
f = np.fft.rfftfreq(N, d=1/fs)       # the frequency of each bin
  • Bin k stands for the frequency kfs/N. The spacing fs/N=1/T (one over the record length) is the frequency resolution: to tell apart tones 1 Hz apart, record for at least about a second.
  • A sine of amplitude A that falls exactly on bin k gives |Xk|=AN/2. So the amplitude spectrum is 2 * np.abs(X) / N, except at DC (bin 0), where it's np.abs(X[0]) / N.
  • The FFT computes the DFT in O(Nlog⁡N) operations instead of O(N2) (lesson 24).

Leakage and windows

If a tone doesn't complete a whole number of cycles in the record, its energy smears into neighboring bins: leakage. Multiplying the samples by a smooth window such as np.hanning(N) before the FFT confines the smear to a few bins, at the cost of slightly wider peaks. A10 explains why.

Finding peaks

The strongest tone is at f[np.argmax(np.abs(X[1:])) + 1] (skipping DC). For several tones, look for bins larger than both neighbors and above a threshold.

Worked example

A signal is sin⁡(2π⋅1000t)+0.3sin⁡(2π⋅3000t), sampled at 8 kHz for 800 samples. What does the FFT show?

Show the solution
  1. Take the FFT and scale it to amplitudes. Multiply each magnitude by 2/N.
import numpy as np
fs, N = 8000, 800
t = np.arange(N) / fs
x = np.sin(2 * np.pi * 1000 * t) + 0.3 * np.sin(2 * np.pi * 3000 * t)
A = 2 * np.abs(np.fft.rfft(x)) / N
f = np.fft.rfftfreq(N, 1 / fs)
print(f[A > 0.1], A[A > 0.1])        # [1000. 3000.] [1.  0.3]
  1. The bin spacing. Δf=8000800Hz=10Hz so both tones land exactly on bins.
  2. Read the peaks. The amplitudes come back as 1 and 0.3, at 1000 and 3000 Hz.

Answer two clean peaks, an amplitude of 1 at 1000 Hz and 0.3 at 3000 Hz.

Worked example

A 12.3 kHz interference tone reaches an ADC sampling at 20 kHz without a filter. Where does it appear?

Show the solution
  1. Find the nearest multiple of the sample rate. 12.3/20=0.615 rounds to 1.
  2. The alias frequency. fa=|12.3−20|kHz=7.7kHz
  3. What it means. It shows up at 7.7 kHz, inside the band, indistinguishable from a real 7.7 kHz signal.
  4. The fix. After sampling, nothing can remove it; only an anti-alias filter before the ADC can.

Answer the tone appears at 7.7 kHz.

Check

Write alias(f, fs) returning the apparent frequency of a tone f sampled at fs, in the range 0 to fs/2. Set fa = alias(f, fs) for f = 7000 Hz and fs = 10000 Hz.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

The starter samples two tones at 8 kHz for 1600 samples. Set A to the amplitude spectrum (2 * |X| / N), f to the bin frequencies, and peaks to the sorted frequencies of the bins with amplitude above 0.1.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Two tones are df = 2 Hz apart. With sample rate fs = 1000 Hz, set N to the smallest number of samples whose bin spacing is at most df, and T to the record length in seconds.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

The starter makes x: a DC level plus a sine of amplitude A1 landing exactly on bin k1. From np.fft.rfft(x), set dc_est and amp_est.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

The starter makes N = 1000 samples of a sine of amplitude 1 whose frequency sits at bin 50.5, between bins. Set amp_rect to the largest value of 2 * abs(np.fft.rfft(x)) / N, and amp_hann to the same for x * np.hanning(N), divided also by the Hann window's coherent gain (its mean). Then set loss_rect and loss_hann to how far each falls below 1, in dB.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

A 2.5 kHz tone is sampled at 8000 samples per second for 1000 samples. Which FFT bin index k (counting from 0) holds it exactly?

Explain why

Why does every ADC need a low-pass filter in front of it?

Practice

Keep practicing

8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.