Tutorial · 7
Signals and the FFT
A network is handed numbers, and how those numbers are written down decides most of how hard its job is. This tutorial takes one signal, looks at the frequencies inside it, throws some of them away, and then trains the same small model twice — once on the waveform and once on the spectrum — to see the difference the writing makes.
1 · What is inside a signal
Two sine waves added together. fft.rfft takes the 64 samples to 33
complex numbers, one per frequency, and the size of each says how much of that
frequency is present. Real input has a symmetric spectrum, so rfft
keeps only the half that is not a mirror — that is the whole difference from
fft.fft.
Two bars, at 4 and 11 cycles, in a ratio of 2:1 — which is what was put in. Nothing was learned here; this is arithmetic, and it runs on the GPU because the tensors already live there.
2 · Filtering is deleting bins
Bury the signal in noise, take it to frequencies, set the bins above 12 cycles to
zero, and come back with fft.irfft. A low-pass filter is that and
nothing else. The error against the clean signal says whether it worked.
The noise that survived is the part of it that happened to sit below 12 cycles. A filter cannot separate signal from noise; it separates frequencies, and that is only the same thing when they do not overlap.
3 · The same model, two ways of writing the input
Three classes of signal — 5, 6 and 7 cycles — each with a random amplitude, a random phase and noise on top. Train one small network on the raw 64 samples, and the identical network on the 33 spectrum sizes, at four different training-set sizes. Nothing about the model changes between the two columns.
The gap is at the small end and it closes. The waveform model has to learn that a shifted sine is the same sine; the spectrum model gets that for free, because the size of a complex number drops the phase — so the transform buys examples, not accuracy, and buys nothing once there are enough of them. Which is the honest version of what feature engineering does, and the reason it did not disappear when deep learning arrived: it is worth the most exactly where data is worth the most.
4 · Where the spectrum lies
The bins are whole numbers of cycles. A signal at 4.5 cycles fits none of them, so its energy smears across the neighbours — spectral leakage, and nothing is wrong with the transform when it happens. A window makes the smear narrower and never removes it.
fft.stft cuts the signal into
overlapping pieces and transforms each one, which is how a spectrogram is made —
feed that to the convolutional network from
tutorial 4 and you have audio classification,
with the picture being time across and frequency up. fft.fft2 does the
same thing to an image, where a low-pass filter is a blur you can read the recipe
for.