Signal Processing
Fourier analysis, filtering, and convolution — the computational toolkit for time-series data
Any signal — an audio waveform, a stock price, an EEG trace, a radar return — can be decomposed into the building blocks it is made of. Signal processing is the toolkit for doing that decomposition, removing what you don't want, and extracting what you do.
The technique that ties it all together is the Fourier transform: a way to look at a signal not as a function of time but as a function of frequency.
The big idea: time vs frequency
A pure sine wave at 5 Hz looks like a peak at exactly 5 in frequency space. A square wave looks like a comb of odd harmonics. Noise looks like a flat spectrum. Music looks like a moving cloud of peaks. Once you can see a signal in frequency space, filtering and analysis become almost trivial.
The Discrete Fourier Transform (and FFT)
For a sampled signal , the DFT is
A naive evaluation costs multiplications. The Fast Fourier Transform computes the same result in — a speed-up that turned spectral analysis from a research project into a routine operation.
The noisy time-domain plot looks like garbage; the spectrum reveals two clean peaks exactly at 50 and 120 Hz, with a roughly flat noise floor. This is the power of the FFT in one image.
Three things that bite beginners
- Aliasing. A signal sampled at can only represent frequencies below (the Nyquist limit). Anything faster appears as a ghost at a lower frequency.
- Spectral leakage. Truncating a signal to a finite window smears each spectral line into a sinc. Multiply by a tapered window (Hann, Hamming, Blackman) to control the smear.
- Units.
np.fft.rfftfreq(N, d)returns frequencies in Hz whendis the sample spacing in seconds. Always pass the sample spacing.
The rectangular window's spectrum is a wide sinc that leaks energy into nearby bins. The Hann window concentrates the energy into a much narrower peak. Always window non-periodic signals.
Designing a digital filter
When you want to remove unwanted frequencies, you design a
filter. The most useful workhorse is the Butterworth IIR
filter, applied with sosfiltfilt for zero-phase response.
Why sosfiltfilt instead of lfilter?
- It runs the filter forward then backward, canceling the phase delay (zero-phase response).
- The "sos" (second-order sections) form is numerically stable for higher orders; the equivalent transfer-function (b, a) form often blows up past order 6 or so.
Time-frequency analysis: the spectrogram
Real signals change over time. A single FFT averages the whole duration; a spectrogram slides a windowed FFT along the signal to show how its frequency content evolves.
The bright diagonal stripe traces the instantaneous frequency rising linearly from 100 to 800 Hz — exactly the chirp law we wrote down. The spectrogram is how everything from speech recognition to gravitational-wave analysis visualizes non-stationary signals.
Convolution
Convolution is the most fundamental operation in linear systems theory. It is also the basis of every finite-impulse-response (FIR) filter and every convolutional layer in a neural network.
For large signals or large kernels, use scipy.signal.fftconvolve,
which exploits the convolution theorem ( in the frequency domain) to run in instead
of .
A multi-file denoising pipeline
This pattern — generate → measure → filter → quantify — is the template for almost every signal-processing experiment.
Check your understanding
You sample a signal at Hz and want to detect a tone at Hz. What happens?
The FFT shows a peak at 700 Hz
The 700 Hz tone is aliased to Hz because it lies above the Nyquist limit Hz
The FFT shows two peaks: one at 700 Hz and one at 300 Hz
The FFT returns NaN
Why is scipy.signal.sosfiltfilt typically preferred over scipy.signal.lfilter for offline filtering?
It runs on the GPU
It performs a forward-then-backward pass for zero phase distortion and uses the numerically stable second-order-sections form for higher-order filters
It always returns integer results
It uses less memory