ENع
←Circular convolution
Lesson 30 · From the DTFT to the FFT

Circular and linear convolution

15 min11 steps5 questionsIntermediate
About this lesson

Computers convolve long signals fast by taking DFTs, multiplying them and transforming back. But a product of N-point DFTs gives circular convolution, not the linear convolution you want. Take the echo h = [1, 0.5, 0.25] and the input x = [1, 2, 3]: linear convolution gives [1, 2.5, 4.25, 2, 0.75], while the 3-point DFT route gives [3, 3.25, 4.25]. Rotate the flipped weights around a circle to see why, watch the tail of y wrap onto its start (time-domain aliasing), then zero-pad both lists to N ≥ L + M − 1 = 5 and the two answers agree. It builds on "Discrete convolution, step by step", "The DFT: a spectrum you can compute" and "Zero-padding and frequency resolution".

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 30 · Interactive
01 / 11

Linear convolution: 5 samples

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.

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"
Loading figure…
or ← →
Read the lesson step by step
  1. Linear convolution: 5 samples

    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.

    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"
  2. The DFT shortcut: multiply

    The DFT turns convolution into multiplication. Take the 3-point DFTs X[k] and H[k], multiply them point by point, and take the inverse DFT. With the FFT this is fast for long lists. Do we get y back?

    3-point DFTs (k = 0, 1, 2):
    X = 6,  −1.5 + 0.866j,  −1.5 − 0.866j
    H = 1.75,  0.625 − 0.217j,  0.625 + 0.217j
    
    C[k] = X[k]·H[k]
    C = 10.5,  −0.75 + 0.866j,  −0.75 − 0.866j
    
    then c[n] = inverse DFT of C
    DFT: see "The DFT: a spectrum you can compute"
  3. Out come 3 numbers, not 5

    The inverse DFT gives back N = 3 numbers: c = [3, 3.25, 4.25]. Only the last one matches y. Multiplying N-point DFTs does not compute linear convolution. It computes circular convolution.

    inverse DFT of X·H, N = 3:
    c = [3, 3.25, 4.25]
    
    linear y = [1, 2.5, 4.25, 2, 0.75]
    
    c[2] = y[2] = 4.25   matches
    c[0] = 3    but y[0] = 1
    c[1] = 3.25 but y[1] = 2.5
  4. N points on a circle

    An N-point DFT treats each list as one turn of a circle: index arithmetic wraps around, so with N = 3 index −1 is index 2. Flipped on the circle, h[(0 − k) mod 3] puts 1, 0.25, 0.5 under k = 0, 1, 2.

    circular convolution, N points:
    c[n] = Σ_{k=0}^{N−1} x[k]·h[(n − k) mod N]
    
    n = 0, N = 3:
    k = 0:  h[0 mod 3]  = h[0] = 1
    k = 1:  h[−1 mod 3] = h[2] = 0.25
    k = 2:  h[−2 mod 3] = h[1] = 0.5
    
    the flip wraps instead of
    falling off the end
  5. n = 0: multiply the pairs, add

    At n = 0 every input sample meets a weight: c[0] = 1·1 + 2·0.25 + 3·0.5 = 3. Linear convolution gave y[0] = 1. The extra 2 comes from the weights that wrapped around.

    c[0] = 1·1 + 2·0.25 + 3·0.5 = 3
    
    linear:  y[0] = 1·1 = 1
    extra:   2·0.25 + 3·0.5 = 2
             (the wrapped weights)
  6. Rotate one step: n = 1

    Rotate the weights one place right. The one that falls off the right end comes back on the left. c[1] = 1·0.5 + 2·1 + 3·0.25 = 3.25. On a circle, sliding becomes rotating: a circular shift.

    weights at n = 1:
    k = 0:  h[1] = 0.5
    k = 1:  h[0] = 1
    k = 2:  h[2] = 0.25   (wrapped in from the end)
    
    c[1] = 1·0.5 + 2·1 + 3·0.25 = 3.25
  7. n = 2, and then it repeats

    c[2] = 1·0.25 + 2·0.5 + 3·1 = 4.25. One more rotation brings the weights back to n = 0, so c repeats every 3 samples. c = [3, 3.25, 4.25]: exactly what the DFT shortcut gave.

    c[2] = 1·0.25 + 2·0.5 + 3·1 = 4.25
    
    c = [3, 3.25, 4.25]
    the same as ifft(fft(x) * fft(h))
    
    n = 3 would give the n = 0 weights again:
    c is periodic with period N = 3
  8. The tail wraps onto the start

    Compare with linear y. Its last two samples, 2 and 0.75, have no room in 3 slots, so they wrap around and add onto the first two: 1 + 2 = 3, 2.5 + 0.75 = 3.25. This is time-domain aliasing.

    c_N[n] = Σ_r y[n + r·N]   (fold y every N)
    
    N = 3:
    c[0] = y[0] + y[3] = 1 + 2      = 3
    c[1] = y[1] + y[4] = 2.5 + 0.75 = 3.25
    c[2] = y[2]        = 4.25
    
    sampling the spectrum (N DFT points)
    aliases the signal in time
  9. Zero-pad to N = 5

    The fix: give the result room. Append zeros so both lists have N ≥ 3 + 3 − 1 = 5 samples: x = [1, 2, 3, 0, 0] and h = [1, 0.5, 0.25, 0, 0]. Now the wrapped weights land on zeros.

    rule: N ≥ L + M − 1 = 5
    
    x = [1, 2, 3, 0, 0]
    h = [1, 0.5, 0.25, 0, 0]
    
    n = 0, N = 5: weights under k = 0 .. 4
      1, 0, 0, 0.25, 0.5
      0.25 and 0.5 sit over x[3] = x[4] = 0
    
    zero-padding: see "Zero-padding and
    frequency resolution"
  10. Rotate again: circular = linear

    Rotate through n = 0 to 4. Every product that wraps now meets a zero, so nothing folds back. c = [1, 2.5, 4.25, 2, 0.75]: the same five numbers as linear convolution.

    c[0] = 1·1 = 1
    c[1] = 1·0.5 + 2·1 = 2.5
    c[2] = 1·0.25 + 2·0.5 + 3·1 = 4.25
    c[3] = 2·0.25 + 3·0.5 = 2
    c[4] = 3·0.25 = 0.75
    
    c = [1, 2.5, 4.25, 2, 0.75] = y
    with N = 4 one sample still wraps:
      [1.75 2.5  4.25 2.  ]
  11. Fast convolution in Python

    In Python 3 with numpy, fft(x, N) pads x with zeros to length N. Multiply the spectra, invert, keep the real part: [1, 2.5, 4.25, 2, 0.75]. scipy.signal.fftconvolve does all this for you.

    from numpy.fft import fft, ifft
    x, h = [1, 2, 3], [1, 0.5, 0.25]
    N = len(x) + len(h) - 1
    y = ifft(fft(x, N) * fft(h, N)).real
    print(y.round(6))
    
    [1.   2.5  4.25 2.   0.75]
    
    without the padding (N = 3):
    ifft(fft(x) * fft(h)).real.round(6)
    [3.   3.25 4.25]
    
    .round(6) hides tiny float errors
    (about 1e−16) left by the FFT
    (Python 3, numpy)

Quiz Unlocks after the last step.