Circular and linear convolution
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
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".
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"
اقرأ الدرس خطوة بخطوة
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"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"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.5N 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 endn = 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)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.25n = 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 = 3The 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 timeZero-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"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. ]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)
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01x = [1, 2, 3] and h = [1, 0.5, 0.25]. What is the 3-point circular convolution value c[0] = Σ x[k]·h[(0 − k) mod 3]?
The weights under k = 0, 1, 2 are h[0], h[2], h[1] = 1, 0.25, 0.5, so c[0] = 1·1 + 2·0.25 + 3·0.5 = 3. Linear convolution gives y[0] = 1; the extra 2 is the wrapped tail.
02You compute ifft(fft(x) * fft(h)) for two lists of the same length N, without padding. What do you get?
The DFT property: the N-point DFT of a circular convolution is the product of the N-point DFTs. So the inverse DFT of the product is the circular convolution.
03The linear convolution is y = [1, 2.5, 4.25, 2, 0.75]. What is the 4-point circular convolution of the same lists?
Fold y every 4 samples: y[4] = 0.75 wraps onto y[0], giving 1 + 0.75 = 1.75. The rest stay: [1.75, 2.5, 4.25, 2]. numpy prints [1.75 2.5 4.25 2. ].
04x has 100 samples and h has 31. What is the smallest DFT size N for which circular convolution equals linear convolution?
N must be at least L + M − 1 = 100 + 31 − 1 = 130. Any larger N also works (in practice a fast size such as 256 is common).
05In numpy, what does fft(x, N) do when N is larger than len(x)?
From the numpy documentation: if n is larger than the input length, the input is padded with zeros. That is exactly the zero-padding that makes the circular result equal the linear one.
المسرد
- Linear convolution
- y[n] = Σ x[k]·h[n − k] for lists that are zero outside their range. Lengths L and M give L + M − 1 output samples.
- Circular convolution
- c[n] = Σ_{k=0}^{N−1} x[k]·h[(n − k) mod N]: the same sum, but indices wrap around a circle of N points. The result has N samples and repeats every N.
- mod N
- The remainder after division by N, kept between 0 and N − 1. With N = 3, −1 mod 3 = 2.
- Circular shift
- Moving a list around a circle: what falls off one end comes back on the other.
- Time-domain aliasing
- The wrap-around error: circular convolution equals the linear result folded every N samples, c[n] = Σ_r y[n + r·N].
- Zero-padding
- Appending zeros to a list before the DFT. Padding both lists to N ≥ L + M − 1 makes circular convolution equal linear convolution.
- Fast convolution
- Computing a linear convolution as inverse FFT of the product of zero-padded FFTs; much faster than the direct sum for long lists.
الملاحظات والمصادر
- 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.
- 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.
- A. V. Oppenheim, R. W. Schafer, Discrete-Time Signal Processing, 3rd ed., Pearson, 2010 (ch. 8: properties of the DFT, circular convolution, linear convolution using the DFT)
- A. V. Oppenheim, A. S. Willsky, S. H. Nawab, Signals and Systems, 2nd ed., Pearson, 1997 (ch. 2: the convolution sum)
- MIT OpenCourseWare 6.341 Discrete-Time Signal Processing (A. V. Oppenheim, Fall 2005), Lecture 16: Linear Filtering with the DFT
- MIT OpenCourseWare 6.341 Discrete-Time Signal Processing, lecture notes (lecture 15: the DFT; lecture 19: FFT algorithms)
- J. O. Smith III, Mathematics of the Discrete Fourier Transform, CCRMA Stanford: Convolution Theorem
- J. O. Smith III, Spectral Audio Signal Processing, CCRMA Stanford: Acyclic Convolution (N ≥ M + L − 1)
- NumPy documentation: numpy.fft.fft (the n argument crops or zero-pads)
- SciPy documentation: scipy.signal.fftconvolve
اختبار يتاح بعد الخطوة الأخيرة.