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?
3 kHz. Sampling at 10 kHz can only represent frequencies up to 5 kHz (the Nyquist frequency, half the sample rate). A 7 kHz tone produces exactly the same samples as a 3 kHz one, kHz: it aliases. That's why every ADC has a low-pass filter in front of it.
Name it
Sampling and aliasing
Sampling at rate captures frequencies from 0 up to . A tone at frequency shows up at its alias:
In code: fa = abs(f - fs * round(f / fs)).
The FFT of real samples
For samples taken at :
X = np.fft.rfft(x) # N//2 + 1 complex values
f = np.fft.rfftfreq(N, d=1/fs) # the frequency of each bin
- Bin stands for the frequency . The spacing (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 that falls exactly on bin gives . So the amplitude spectrum is
2 * np.abs(X) / N, except at DC (bin 0), where it'snp.abs(X[0]) / N. - The FFT computes the DFT in operations instead of (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 , sampled at 8 kHz for 800 samples. What does the FFT show?
Show the solution
- Take the FFT and scale it to amplitudes. Multiply each magnitude by .
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]
- The bin spacing. so both tones land exactly on bins.
- 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
- Find the nearest multiple of the sample rate. rounds to 1.
- The alias frequency.
- What it means. It shows up at 7.7 kHz, inside the band, indistinguishable from a real 7.7 kHz signal.
- 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 (counting from 0) holds it exactly?
Explain why
Why does every ADC need a low-pass filter in front of it?
Once a signal is sampled, a tone above half the sampling rate produces exactly the same samples as a tone below it, its alias, so the two can't be told apart or separated afterwards. Any noise or interference above the Nyquist frequency would fold down into the band you care about and look like real signal. The only cure is to remove those frequencies before sampling, with an analog low-pass filter.
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.