Convolution and correlation

Flip, slide, multiply, sum — the one operation behind every filter, and why it becomes a simple product in the frequency domain.

What you'll learn
  • Compute a convolution by hand and predict its length
  • State the convolution theorem and use it to convolve with the FFT
  • Use correlation as a matched filter to find a known shape in noise

A linear, time-invariant system — a filter, a room’s echo, a camera’s blur — is completely described by its impulse response \(h[n]\): what comes out when a single 1 goes in. Every input is a sum of shifted, scaled impulses, so the output is the same sum of shifted, scaled impulse responses. That sum is the convolution

\[y[n] = (x \ast h)[n] = \sum_k h[k]\, x[n - k].\]

Flip, slide, multiply, sum

time_domain_convolution.m works it through with a box and a ramp:

signal = zeros(1,20);
signal(8:15) = 1;
kernel = [1 .8 .6 .4 .2];
nConv = nSign + nKern - 1;     % 20 + 5 - 1 = 24
The kernel, flipped, slides along the box signal. At each position the overlapping samples are multiplied and summed into one output sample. The output ramps up to 3.0 while the kernel enters the box, holds, and ramps down as it leaves.

The output is longer than either input: \(N + M - 1\) samples for ‘full’ convolution. conv(signal, kernel, 'same') and SciPy’s mode='same' trim it back to the signal’s length, centred — which is what 1_Convolution_v1.py uses to low-pass the 1 kHz + 15 kHz test signal with the 29-tap impulse response in mysignals.py. (1_Convolution_v2.py, the hand-written version, is still an empty pass.)

The convolution theorem

Sliding costs \(N \times M\) multiplications. The Fourier transform offers a shortcut: convolution in time is multiplication in frequency.

Top row: signal x[n] convolved with kernel h[n] equals output y[n]. Bottom row: spectrum X[k] times response H[k] equals spectrum Y[k]. FFT arrows lead down from x and h; an inverse FFT arrow leads up from Y to y. Top row: signal x[n] convolved with kernel h[n] equals output y[n]. Bottom row: spectrum X[k] times response H[k] equals spectrum Y[k]. FFT arrows lead down from x and h; an inverse FFT arrow leads up from Y to y.
Two FFTs, one element-wise product and one inverse FFT replace the sliding sum. For long kernels this is far cheaper, and it explains why a filter's frequency response is simply the FFT of its impulse response.

freq_domain_convolution.m does exactly this, with one detail that matters: both signals are zero-padded to length \(N + M - 1\) before the FFT (fft(signal, nConv)), and the result is then trimmed by half the kernel at each end to match 'same'. Without the padding, the product computes a circular convolution, and the tail of the output wraps round onto its start.

Correlation: convolution without the flip

Correlation slides one signal along another without flipping it, so it peaks where the two look alike. Match Filter/1_match_filter.py hides the bits 0 1 1 0 1 0 0 1 — 128 samples each — under noise as strong as the signal itself, then correlates with a 128-sample pulse:

original_signal = np.repeat([0.,1.,1.,0.,1.,0.,0.,1.], 128)
noisy_signal = original_signal + np.random.randn(len(original_signal))
correlated_output = signal.correlate(noisy_signal, np.ones(128), mode='same')
Three panels: the clean bit pattern with sample points at each bit centre; the same pattern buried in noise; and the correlation output, which rises to about 135 at the one-bits and stays near 0 at the zero-bits.
At each bit centre the correlation sums 128 samples: a one-bit gives about 128, the noise adds only about ±11 (√128). In a re-run with NumPy's default_rng(3): 135, 134, 141, 140 for the ones and −2, 21, −5, −16 for the zeros.

This is the matched filter: correlating with the shape you expect is the best linear detector of that shape in white noise. Radar, GPS and Wi-Fi receivers all rely on it.

Try it yourself. Convolve the box and ramp above with np.convolve and check you get 24 samples peaking at 3.0. Then do it with np.fft.ifft(np.fft.fft(x, 24) * np.fft.fft(h, 24)).real — and again with length 20 instead of 24, and find which samples the wrap-around corrupted.

View source on GitHub →