X · HX[k]·H[k]x[0]1x[1]2x[2]3x[3]0x[4]0c[0]–c[1]–c[2]–c[3]–c[4]–1from numpy.fft import fft, ifft2x, h = [1, 2, 3], [1, 0.5, 0.25]3N = len(x) + len(h) - 14y = ifft(fft(x, N) * fft(h, N)).real5print(y.round(6))h[0]1h[0]1h[1]0.5h[1]0.5h[2]0.25h[2]0.25h[3]0h[3]0h[4]0h[4]00
-1012345601234nvalueLINEAR y[n]-1012345601234nvalueCIRCULAR c[n]
Coloured parts: what the current step is about; everything else rests in whiteBlocks: the input list x[k], the weights h on a circle, the output list c[n], the DFT block and a Python code cardPlots: the linear result y[n] (its red tail wraps back) and the circular result c[n]The flipped weights rotate one place per output; the one leaving the right end returns on the leftWith x = [1, 2, 3] and h = [1, 0.5, 0.25], 3-point circular convolution gives [3, 3.25, 4.25]; zero-padded to 5 points it gives the linear result [1, 2.5, 4.25, 2, 0.75]. For long lists the FFT route is much faster than the direct sum: SciPy's documentation says fftconvolve is generally much faster than convolve for arrays longer than about 500, but can be slower when only a few output values are needed.

Two ways to convolve

  1. Take the echo h = [1, 0.5, 0.25] and the input x = [1, 2, 3]. Their ordinary, linear convolution is y = x ∗ h = [1, 2.5, 4.25, 2, 0.75]. That is 3 + 3 − 1 = 5 samples. Remember it.

Convolution on a circle

Wrap-around and the fix

In code

The working

# step 1/11 · linear

y[n] = Σ_k x[k]·h[n − k]

x = [1, 2, 3]          L = 3
h = [1, 0.5, 0.25]      M = 3
y = [1, 2.5, 4.25, 2, 0.75]
length L + M − 1 = 5

flip and slide in detail:
see "Discrete convolution, step by step"

The echo h = [1, 0.5, 0.25] and the input x = [1, 2, 3] are chosen example values. Every number and stem in the figure is computed from the sums in this lesson's model; the tests check them against a direct DFT and against numpy's printed output. The DFT property behind the shortcut: the N-point DFT of a circular convolution is the product of the N-point DFTs (Oppenheim and Schafer, ch. 8). Inverting a product of N-point DFTs gives the linear convolution only when N ≥ L + M − 1; for a smaller N it gives, in general, the wrapped (circular) result.