A10 · Lesson 15 · Sampling and the DFT, previewed
From a continuous signal to a list of numbers, and back to a spectrum
A computer can't store a continuous signal. It stores samples: the voltage measured thousands or millions of times a second. Between samples it knows nothing. Surprisingly, that's enough to rebuild the signal exactly, if you sample fast enough. Sample too slowly and a high frequency puts on a disguise: it shows up as a lower frequency that was never there. Once the signal is a list of numbers, the discrete Fourier transform finds its spectrum, and that's what runs inside every digital scope and spectrum analyzer.
Play
- A sine at frequency is sampled at 1 kHz. The staircase holds each sample until the next one, the way a simple converter does. The dashed curve is the lowest-frequency sine that passes through the same samples.
- Start well below 500 Hz and raise . What happens once passes 500 Hz? At 1 kHz?
- Find two different values of that give exactly the same samples.
Predict
A 900 Hz tone is sampled at 1 kHz, with no filter first. What frequency does the sampled data seem to contain?
100 Hz. At 1 kHz, a 900 Hz sine advances 0.9 of a cycle between samples, which looks exactly like going back 0.1 of a cycle: a 100 Hz sine running backward. Once sampled, there's no way to tell them apart. That's aliasing, the same effect that makes wagon wheels in films seem to turn slowly or backward. The only cure is to remove frequencies above half the sample rate before sampling.
Name it
Sampling
Samples. Measuring every seconds gives the list , at a sample rate .
The sampling theorem. A signal whose frequencies all lie below can be rebuilt exactly from its samples. That limit, , is the Nyquist frequency; equivalently, you must sample at more than twice the highest frequency present. CD audio samples at 44.1 kHz to cover hearing up to 20 kHz.
Aliasing. A frequency above isn't lost, it's disguised. Its samples match those of a lower frequency, the alias:
for the whole number that brings it into the range to . Every frequency band folds back into that range. So every analog-to-digital converter is preceded by an anti-aliasing filter, a low-pass that removes what's above before it can masquerade as something else.
The discrete Fourier transform
With samples, the DFT correlates the data with complex sinusoids, exactly like the Fourier series coefficients of lesson 2:
- Bin corresponds to the frequency , so the bins are apart: one over the record's length (lesson 13's resolution limit).
- A tone that fits a whole number of cycles in the record lands in a single bin. One that doesn't spreads into its neighbors: leakage, which window functions reduce.
- Computed directly, the DFT takes about operations. The fast Fourier transform (FFT) gets the same answer in about : for a million samples, 20 million instead of a trillion. It's one of the most important algorithms in engineering.
Course C9 (signals and systems) and the DSP survey build on all of this.
Worked example
Tones at 7, 13 and 23 kHz are sampled at 10 kHz. Where does each appear?
Show the solution
- The rule. Bring each tone into 0 to 5 kHz with .
- Fold each tone. In kHz:
- Compare. All three land at 3 kHz and can't be told apart in the data.
Answer all three appear at 3 kHz.
Worked example
You take a DFT of 1000 samples at 10 kHz. How far apart are the bins, where does a 1.25 kHz tone land, and how much work does the FFT save?
Show the solution
- The bin spacing.
- The tone's bin.
- A direct DFT. It takes about operations.
- The FFT. It takes about a hundred times fewer.
Answer the bins are 10 Hz apart, the tone lands in bin 125, and the FFT does about a hundred times less work.
Worked example
Telephone speech is limited to 3.4 kHz and sampled at 8 kHz. What is the minimum sampling rate, and why use more?
Show the solution
- Nyquist's minimum. It's twice the highest frequency:
- The guard band. Sampling at 8 kHz puts the first alias of a 3.4 kHz tone at 1.2 kHz above the band.
- What the filter has to do. The anti-aliasing filter only has to roll off between 3.4 and 4.6 kHz, which a practical filter can do, rather than from 3.4 kHz to 3.4 kHz, which no filter can.
Answer the minimum is 6.8 kHz; sampling at 8 kHz leaves a 1.2 kHz guard band that a practical anti-aliasing filter can work in.
Check
A signal has frequencies up to 4 kHz. What is the lowest sample rate that can capture it without aliasing?
Write units the way you normally would: 1.5 kHz, 1500 Hz and 0.0015 MHz are the same answer.
Check
A 6 kHz tone is sampled at 10 kHz with no anti-aliasing filter. At what frequency does it appear in the data?
Write units the way you normally would: 1.5 kHz, 1500 Hz and 0.0015 MHz are the same answer.
Check
A DFT of 1024 samples taken at 8000 Hz has bins how far apart?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
A sine wave at an unknown frequency has been sampled for you: x holds 1000 samples taken at 1000 Hz. Use np.fft.rfft to find its frequency, and set f_peak to it in hertz.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Where does an anti-aliasing filter go, and why?
Check
A signal is limited to 15 kHz and must leave a guard band of 1.2 kHz below the first alias.
(a)
What sampling rate does that take?
Write units the way you normally would: 1.5 kHz, 1500 Hz and 0.0015 MHz are the same answer.
Check
A DFT of samples taken at (kHz).
Step 1
What is the bin spacing, in Hz?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
How long a record is that, in ms?
Explain why
Why does a tone above half the sample rate show up at a lower frequency in sampled data?
Samples only record the signal at the sampling instants. A sinusoid that turns more than half a cycle between samples lands at the same points it would if it had turned a little less than a full cycle the other way, which is exactly what a lower frequency does. Since the samples are identical, nothing in the data can distinguish the two, and the high tone appears as its lower alias, .
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.