C929 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 N=2m, 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 log2⁡N 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?

Name it

The techniques

  • The DFT by definition: X[k]=∑nx[n]e−j2πkn/N, N² operations. Checking it against numpy.fft.fft is 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 k+δ with
δ=12(α−γ)α−2β+γ

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
  1. Interpolate the peak. δ=12(−6.1−(−4.0))−6.1+2.4−4.0=12(−2.1)/(−7.7)=0.136
  2. Convert to hertz. The peak is at bin 128.136, which is f=128.136×8000/1024=1001.1Hz
  3. 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
  1. New rate. The new rate is 12 kHz and its Nyquist frequency 6 kHz.
  2. Fold the tone. 9 kHz is 3 kHz above it, so it folds to 12−9=3kHz right in the middle of the band, where no later filter can tell it from a real 3 kHz tone.
  3. 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 N=29 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?

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.