Skip to content

Circular vs linear convolution

See why multiplying DFTs gives a convolution whose tail wraps onto its start, and how zero-padding stops it.

Before thisDiscrete convolution (5.2)

Before this5.2
Chapter 13 · Lesson 4 of 6

First, the picture

Convolve 1, 2, 3, 4 with 1, 1, 1 directly, and through a DFT of NN points. Watch the tail wrap onto the start as NN shrinks.

The tail wraps round

x = 1, 2, 3, 4 and h = 1, 1, 1. The linear output y has 4 + 3 − 1 = 6 samples.

N = 8: room for all 6 samples, plus 2 zeros. Circular and linear agree: 1, 3, 6, 9, 7, 4.

DFT length N
8
samples wrapped
none
0.00 / 14.00 s
Describe this picture

Two panels for x=1,2,3,4x=1,2,3,4 and h=1,1,1h=1,1,1, whose linear output yy has 4+3−1=64+3-1=6 samples. The linear panel draws yy as stems with dot heads. The circular panel draws the output of the DFT product in NN points as stems with square heads. A dashed vertical line labelled “N” marks where the room ends, between n=N−1n=N-1 and n=Nn=N. When NN shrinks, each sample of yy at n≥Nn\ge N travels along an arc back to n−Nn-N and adds on. Afterwards a faint dot stays where it came from, joined to its new place by a dotted arc, and the part of the stem it added is a hatched block, named “wrapped” in the key. The readouts are the DFT length NN and the number of samples wrapped.

The clip plays once and holds on its end frame. It steps through N=8N=8, 6, 5 and 4, with a caption at each value and none while NN changes; the readouts go from 8 and “none”, 6 and “none”, 5 and “1”, to 4 and “2”. Once the clip has finished, dragging the dashed line, or the arrow keys, sets NN from 4 to 10.

Two convolutions, one product

In Discrete convolution (5.2) you slid a flipped copy of hh along xx and added up the overlaps. From here on I call that linear convolution, written y=x∗hy=x*h. If xx has NxN_x samples and hh has NhN_h, the output yy has Nx+Nh−1N_x+N_h-1 samples.

The DFT offers a shortcut. Pad xx and hh with zeros to the same length NN, take the DFT of each, multiply the two spectra bin by bin, and invert. What comes back is called ycirc[n]y_\text{circ}[n]. It is not always y[n]y[n].

The reason is in Sampling the spectrum (13.1). By the convolution rule of Properties of the DTFT (12.3), the DTFT of yy is Y(ejΩ)=X(ejΩ)H(ejΩ)Y(e^{j\Omega})=X(e^{j\Omega})H(e^{j\Omega}). The products X[k]H[k]X[k]H[k] are NN samples of that spectrum. And NN samples of a spectrum describe the signal repeated every NN samples, with overlapping copies added. So ycircy_\text{circ} is yy repeated every NN samples, one period kept.

If NN is at least the length of yy, the copies do not touch and you see yy followed by zeros. If NN is shorter, the copies overlap and the tail of yy lands on top of its start. This operation has a name from Properties of the DFT (13.3): ycirc=x⊛Nhy_\text{circ}=x\circledast_N h, the NN-point circular convolution.

The tail wraps round

Think of a circular parking garage with NN bays. Cars arrive in order, and the first NN park in bays 0 to N−1N-1. A car that arrives after the last bay is taken goes round to the start and parks in a bay that is already occupied. In the convolution, parking in an occupied bay means adding to it.

The picture at the top of the page convolves x=1,2,3,4x=1,2,3,4 with h=1,1,1h=1,1,1, so yy is 1, 3, 6, 9, 7, 4. Its clip stops at four DFT lengths:

  • N=8N=8: room for all 6 samples, plus 2 zeros. Circular and linear agree: 1, 3, 6, 9, 7, 4.
  • N=6=4+3−1N=6=4+3-1: just enough room. Still equal.
  • N=5N=5: the last sample, 4, has no room and wraps onto n=0n=0: 1+4=51+4=5. Output 5, 3, 6, 9, 7.
  • N=4N=4: the last two wrap: 1+7=81+7=8 and 3+4=73+4=7. Output 8, 7, 6, 9. The DFT product gave a circular convolution; pad to N≥Nx+Nh−1N\ge N_x+N_h-1 and nothing wraps.

When the clip ends, drag the dashed line, or use the arrow keys, to set the DFT length NN from 4 to 10. Notice that the smallest value, 4, is where both sequences just fit. Move up one step at a time and watch the wrapped samples travel back out past the dashed line, until “samples wrapped” reads “none”.

One more thing to notice. Each circular output adds up to 30, the same total as the linear one. Wrapping moves samples; it never loses any.

The wrap rule, in symbols, is

ycirc[n]=∑ry[n−rN],0≤n≤N−1.y_\text{circ}[n]=\sum_{r}y[n-rN],\qquad 0\le n\le N-1.

Here rr runs over all whole numbers; at most a few terms are not zero. At N=5N=5, n=0n=0 collects y[0]y[0] and y[5]y[5], which is 1+4=51+4=5. If N≥Nx+Nh−1N\ge N_x+N_h-1, only r=0r=0 contributes inside 0≤n≤N−10\le n\le N-1, and ycirc=yy_\text{circ}=y followed by zeros.

The recipe

The quickest way to see the shortcut working is to follow one pass at N=8N=8, the first stop of the clip. Each strip below shows the sizes of a sequence of eight numbers. The first three are bins k=0k=0 to 77 and the last is samples n=0n=0 to 77.

|X[k]|1007.2512.8322.723242.7252.8367.257k|H[k]|302.411120.413140.415162.417k|X[k]H[k]|30017.5112.8321.123241.1252.83617.517ky[n]1031629374450607n
Fig. Pad x and h to N = 8, take their DFTs, multiply bin by bin (sizes shown; the angles add), and invert: 1, 3, 6, 9, 7, 4, 0, 0, the linear output.

The sizes multiply bin by bin, and the angles add; the figure shows only the sizes. Bin 0 is the easiest check. X[0]=10X[0]=10 is the sum of xx, H[0]=3H[0]=3 is the sum of hh, and X[0]H[0]=30X[0]H[0]=30 is the sum of yy.

Written out for two of the bins, X[1]≈−0.414−7.243jX[1]\approx-0.414-7.243\mathrm{j} and H[1]≈1.707−1.707jH[1]\approx1.707-1.707\mathrm{j}. That is the whole algorithm. In words:

  1. Pad xx and hh with zeros to length NN.
  2. Take the DFT of each.
  3. Multiply the two spectra, bin by bin.
  4. Take the inverse DFT of the product.

Choosing N

Padding to N≥Nx+Nh−1N\ge N_x+N_h-1 leaves room for all of yy, so nothing wraps and circular equals linear. Here that is 4+3−1=64+3-1=6, which is why N=6N=6 already agrees. Below that, the wrapped samples add onto the start; this is time aliasing, the mirror of the frequency aliasing in Sampling and aliasing (10.1).

Below max⁡(Nx,Nh)\max(N_x,N_h) the DFT would not even hold the whole of xx or hh, so that question never arises. For a longer example, take 1000 samples through a filter of 101 taps. You need N≥1000+101−1=1100N\ge1000+101-1=1100, and the next power of two is 2048, the usual choice for the FFT.

Why bother? Direct convolution needs about NxNhN_xN_h multiplications, which is 1000×101=101,0001000\times101=101{,}000 here. The recipe needs three DFTs of length NN, and the FFT of The FFT (14.1) makes each of those fast. This is fast convolution. For signals too long to transform in one piece, Fast convolution (14.3) does it in blocks.

The maths behind it · circulant matrices

Linear convolution is the tall Toeplitz matrix of 5.2 §6. Circular convolution folds its bottom rows onto its top and gives a square circulant matrix, where each row is a rotation of the one above. The DFT as a matrix (13.5) diagonalises it.

The maths behind it · sums of random variables

The distribution of a sum of two independent whole-number quantities is the convolution of their distributions. Compute it with a DFT that is too short and large totals wrap onto small ones, a classic bug when dice or count distributions are combined by FFT.

Worked example

Take x=1,2,3,4x=1,2,3,4 and h=1,1,1h=1,1,1, as in the instrument. The linear output is y=1,3,6,9,7,4y=1,3,6,9,7,4, and it sums to 30.

Now compute the circular output with N=5N=5 by wrapping. Only y[5]=4y[5]=4 lies at or beyond NN, so it adds onto n=0n=0:

ycirc=[ 1+4, 3, 6, 9, 7 ]=[ 5, 3, 6, 9, 7 ].y_\text{circ}=[\,1+4,\ 3,\ 6,\ 9,\ 7\,]=[\,5,\ 3,\ 6,\ 9,\ 7\,].

The inverse DFT of X[k]H[k]X[k]H[k] with both padded to 5 gives the same numbers. The total is 5+3+6+9+7=305+3+6+9+7=30, as it must be.

Where you’ll meet this

Filters applied through an FFT, such as an audio reverb or an image blur, use this recipe and must choose NN with the rule above. A circular result also appears on purpose: a periodic signal passed through a filter gives a circular convolution, with no padding at all.

Reference card

QuantityFormulaNotes
Linear convolutiony=x∗hy=x*h, length Nx+Nh−1N_x+N_h-15.2
DFT productycirc=IDFT{X[k]H[k]}=x⊛Nhy_\text{circ}=\text{IDFT}\{X[k]H[k]\}=x\circledast_N hboth padded to NN
Wrap ruleycirc[n]=∑ry[n−rN]y_\text{circ}[n]=\sum_{r}y[n-rN], 0≤n≤N−10\le n\le N-113.1 applied to yy
No wrapN≥Nx+Nh−1N\ge N_x+N_h-1then ycirc=yy_\text{circ}=y (and zeros)
Total kept∑nycirc[n]=∑ny[n]\sum_n y_\text{circ}[n]=\sum_n y[n]wrapping moves samples
Fast convolutionpad, DFT, multiply, inverseFFT: 14.1, 14.3

End of lesson 13.4

Where to go next.

Phasorium
LibraryEvery lesson, in order

Parts

About Phasorium
Look