C9 · Lesson 29 · Spectral analysis lab
DFTs, peak finding, fast convolution and resampling in Python
Lessons 21 to 27 covered sampling, rate changes, the DFT, windows and the FFT. In this lab you'll put them to work the way an instrument or an audio program does: check a DFT against the FFT, find a tone's frequency to a fraction of a bin, filter a long signal by overlap-add, cut a sample rate without aliasing, and follow a chirp through a spectrogram.
- The curve is the cost of overlap-add filtering, in real multiplications per output sample, against the FFT size , for a filter of M taps.
- Each block costs two FFTs and N products and yields N − M + 1 new samples. Too small an FFT wastes most of each block on overlap; too large a one pays for nothing.
- The readout is direct convolution's cost, M per sample. For short filters it wins; somewhere around 30 to 60 taps the FFT takes over.
Predict
A 101-tap filter runs by overlap-add with 1024-point FFTs. Counting real multiplications, about how many does each output sample cost?
About 49: two 1024-point FFTs at real multiplications each, plus 1024 complex products, is 45,056 per block, spread over 924 new samples. Direct convolution costs 101 per sample, so the FFT is twice as fast here, and much faster for longer filters.
Name it
The techniques
- The DFT by definition: , N² operations. Checking it against
numpy.fft.fftis the standard test that an FFT routine is right. - Peak interpolation: a tone between bins puts its peak between two DFT samples. Fitting a parabola through the log magnitudes α, β, γ at bins k − 1, k, k + 1 puts the peak at with
With a Hann window this is accurate to a few hundredths of a bin.
- Overlap-add: split x into blocks of L = N − M + 1, convolve each with h by FFT (padding both to N), and add the overlapping tails.
- Decimation: low-pass to the new Nyquist frequency, then keep every Mth sample. Without the filter, everything above the new Nyquist frequency folds down.
- Spectrogram: windowed DFTs of overlapping frames; the strongest bin in each frame tracks a changing tone.
Worked example
A Hann-windowed 1024-point DFT at 8 kHz has log magnitudes −6.1, −1.2 and −4.0 dB at bins 127, 128 and 129. Where is the tone?
Show the solution
- Interpolate the peak.
- Convert to hertz. The peak is at bin 128.136, which is
- Compare. Without interpolation the answer would be bin 128, 1000 Hz: off by about an eighth of a bin.
Answer the tone is at bin 128.136, which is 1001.1 Hz.
Worked example
A 48 kHz signal is decimated by 4 without filtering. Where does a 9 kHz tone end up?
Show the solution
- New rate. The new rate is 12 kHz and its Nyquist frequency 6 kHz.
- Fold the tone. 9 kHz is 3 kHz above it, so it folds to right in the middle of the band, where no later filter can tell it from a real 3 kHz tone.
- The fix. A low-pass with its cutoff at 6 kHz (π/4 at 48 kHz) before the downsampling removes it.
Answer it ends up at 3 kHz, where it can't be told from a real 3 kHz tone.
Check
Overlap-add with for a 101-tap filter. Real multiplications per output?
Check
Write dft(x): the DFT of a list by its definition, as a numpy array of complex values.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write peak_frequency(x, fs): apply a Hann window, take np.fft.rfft, find the largest bin k (not 0 or the last), interpolate a parabola through the log magnitudes at k − 1, k, k + 1, and return the frequency in hertz.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write overlap_add(x, h, nfft): the full linear convolution of x and h (length len(x) + len(h) − 1), computed with FFTs of size nfft on blocks of nfft − len(h) + 1 samples.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write decimate(x, M, ntaps): filter x with an ntaps-tap Hamming-windowed sinc low-pass with cutoff π/M (ntaps odd, unity gain at DC), then keep every Mth sample, starting with the first.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write track(x, fs, n, hop): a spectrogram's peak frequencies. For frames of n samples starting every hop samples (while a whole frame fits), apply a Hann window, take rfft, and return the list of frequencies (bin × fs/n) of each frame's largest bin.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why must a signal be low-pass filtered before its sample rate is reduced, and not after?
Keeping every Mth sample is itself a sampling operation at the new, lower rate, so any content above the new Nyquist frequency folds down into the band below it. Once that has happened, the folded energy sits at the same frequencies as genuine signal, and no filter can separate the two. Filtering first, while the signal is still at the high rate, removes everything above the new Nyquist frequency while it's still distinguishable, so that the downsampling has nothing left to fold. The same reasoning is why an analog anti-aliasing filter comes before an ADC.
Practice
Keep practicing
One kind of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.