Discrete convolution, step by step
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A linear, time-invariant digital system is described by one list of numbers: its impulse response h[n]. Feed an echo h = [1, 0.5, 0.25] the input x = [1, 2, 3] and work out the output two ways. First, every input sample makes its own scaled, shifted copy of h, and the copies add up. Then, one output at a time: flip h, slide it under x, multiply the lined-up pairs and add. Both give y = [1, 2.5, 4.25, 2, 0.75], the convolution sum y[n] = Σ x[k]·h[n − k]. Finally numpy does it in one line. It builds on "The impulse response of a digital system", "Discrete-time signals x[n]" and "System properties"; the continuous-time version is "Convolution and the impulse response".
The system: an echo h[n]
This digital system is an echo. Feed it one sample of 1, the impulse δ[n], and out comes 1, then 0.5, then 0.25. That list is its impulse response: h = [1, 0.5, 0.25].
δ[n] = 1 at n = 0, else 0 impulse in -> impulse response out h[0] = 1 h[1] = 0.5 h[2] = 0.25 h[n] = 0 for every other n more in "The impulse response of a digital system" (it uses a different example, h = [0.5, 0.3, 0.2])
اقرأ الدرس خطوة بخطوة
The system: an echo h[n]
This digital system is an echo. Feed it one sample of 1, the impulse δ[n], and out comes 1, then 0.5, then 0.25. That list is its impulse response: h = [1, 0.5, 0.25].
δ[n] = 1 at n = 0, else 0 impulse in -> impulse response out h[0] = 1 h[1] = 0.5 h[2] = 0.25 h[n] = 0 for every other n more in "The impulse response of a digital system" (it uses a different example, h = [0.5, 0.3, 0.2])The input x[n]
Now feed it a longer input: x = [1, 2, 3] at n = 0, 1, 2. Everywhere else x is 0. What comes out? Convolution answers that, one sample at a time.
x[0] = 1 x[1] = 2 x[2] = 3 x[n] = 0 for every other n a signal is a list of numbers: see "Discrete-time signals x[n]"Each input sample makes a copy of h
The system is linear and time-invariant. So x[0] = 1 makes 1·h[n], x[1] = 2 makes 2·h[n − 1], one step later, and x[2] = 3 makes 3·h[n − 2]: three scaled, shifted echoes.
input sample k -> x[k]·h[n − k] k = 0: 1·h[n] = 1, 0.5, 0.25 at n = 0, 1, 2 k = 1: 2·h[n − 1] = 2, 1, 0.5 at n = 1, 2, 3 k = 2: 3·h[n − 2] = 3, 1.5, 0.75 at n = 2, 3, 4 shift: time-invariance scale: linearity see "System properties"Add the copies: y = x ∗ h
Linear also means the copies add. Stack them at every n and you get y = [1, 2.5, 4.25, 2, 0.75]. That sum is the convolution y[n] = Σ x[k]·h[n − k], written y = x ∗ h.
y[n] = Σ_k x[k]·h[n − k] (superposition) n = 0: 1 = 1 n = 1: 0.5 + 2 = 2.5 n = 2: 0.25 + 1 + 3 = 4.25 n = 3: 0.5 + 1.5 = 2 n = 4: 0.75 = 0.75 check: Σ y = Σ x · Σ h 10.5 = 6 × 1.75Flip h and slide it under x
Now compute one output at a time. Flip h to get h[−k]: the weights now read 0.25, 0.5, 1, with 1, the weight for the newest input, on the right. Slide it under x. Where it sits is the output time n.
h[k] weights 1, 0.5, 0.25 at k = 0, 1, 2 h[−k] the same, mirrored: k = 0, −1, −2 h[n − k] shifted right by n the weight h[0] = 1 sits under x[n]: the newest input counts the mostn = 0: multiply the pairs, add
At n = 0 only x[0] = 1 meets a weight, h[0] = 1. The other weights sit over zeros. Multiply each lined-up pair and add: y[0] = 1·1 = 1.
y[0] = Σ_k x[k]·h[0 − k] = x[0]·h[0] = 1·1 = 1 x[−1]·h[1] = 0·0.5 = 0 x[−2]·h[2] = 0·0.25 = 0Slide one step: n = 1
Slide the window one step right, to n = 1. Now two pairs line up: y[1] = 1·0.5 + 2·1 = 2.5. It is the same sum as before, read one output at a time.
y[1] = x[0]·h[1] + x[1]·h[0] = 1·0.5 + 2·1 = 2.5 the same number the stacked copies gave at n = 1n = 2: full overlap
At n = 2 the window covers all of x: y[2] = 1·0.25 + 2·0.5 + 3·1 = 4.25. It is the largest output, because every input sample sits under a weight.
y[2] = x[0]·h[2] + x[1]·h[1] + x[2]·h[0] = 1·0.25 + 2·0.5 + 3·1 = 4.25 three pairs: the most overlapThe window slides off the end
Keep sliding. y[3] = 2·0.25 + 3·0.5 = 2, then y[4] = 3·0.25 = 0.75. After that no pairs meet, so y is 0. The output is 3 + 3 − 1 = 5 samples long.
y[3] = x[1]·h[2] + x[2]·h[1] = 2 y[4] = x[2]·h[2] = 0.75 y[5] = 0 (no pairs meet) length = N + M − 1 = 3 + 3 − 1 = 5 y = [1, 2.5, 4.25, 2, 0.75]Convolution in Python
In Python 3 with numpy, np.convolve(x, h) does every multiply and add for you. It prints the same five numbers: 1, 2.5, 4.25, 2 and 0.75. Its default "full" mode returns N + M − 1 values.
import numpy as np x = np.array([1, 2, 3]) h = np.array([1, 0.5, 0.25]) y = np.convolve(x, h) print(y) [1. 2.5 4.25 2. 0.75] the same sum as two plain loops: y = [0] * (len(x) + len(h) - 1) for n in range(len(y)): for k in range(len(x)): if 0 <= n - k < len(h): y[n] += x[k] * h[n - k] (Python 3, numpy)
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01With h = [1, 0.5, 0.25] and x = [1, 2, 3] (both starting at n = 0), what is y[1]?
At n = 1 two pairs line up: x[0]·h[1] + x[1]·h[0] = 1·0.5 + 2·1 = 2.5.
02In y[n] = Σ x[k]·h[n − k], what does h[n − k] look like as a function of k?
Replacing k by −k flips h; adding n slides it so the weight h[0] sits under x[n]. That is the flip and slide picture.
03x has 4 samples and h has 6 samples. How many samples does the full convolution x ∗ h have?
The window first meets x when one pair overlaps and last leaves it when one pair overlaps: N + M − 1 = 4 + 6 − 1 = 9. That is also the length np.convolve returns in its default full mode.
04Why may we build the output by adding one scaled, shifted copy of h for every input sample?
Time-invariance: a sample at k gives h shifted to k. Linearity: a sample of size x[k] gives h scaled by x[k], and the responses add. Causality, stability and finite length are not needed.
05Which Python line computes the convolution of the numpy arrays x and h?
np.convolve(x, h) computes Σ x[k]·h[n − k] for every n. x * h multiplies element by element, and np.sum(x * h) or np.dot(x, h) give a single number: only one lined-up position, without the flip.
المسرد
- Impulse response h[n]
- The output of a discrete-time system when the input is the unit impulse δ[n] (1 at n = 0, 0 elsewhere). For an LTI system it describes the system completely.
- LTI system
- A system that is linear (scaled and added inputs give scaled and added outputs) and time-invariant (delaying the input only delays the output).
- Superposition
- For a linear system, the response to a sum of inputs is the sum of the responses to each input.
- Convolution sum
- y[n] = Σ x[k]·h[n − k], written y = x ∗ h. It gives the output of a discrete-time LTI system for any input.
- Flip and slide
- The hand method: flip h to h[−k], shift it to h[n − k], multiply the lined-up pairs with x[k] and add. Repeat for every n.
- Full convolution
- All N + M − 1 output values of two finite lists of lengths N and M: numpy's default mode.
الملاحظات والمصادر
- 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 convolution sum in this lesson's model; the tests check it against a plain double loop and against numpy's printed output.
- Convolution does not care about order: x ∗ h = h ∗ x. You may flip and slide x under h instead of h under x, and get the same y.
- np.convolve defaults to mode "full" (length N + M − 1 = 5 here). mode "same" keeps the middle max(N, M) values and mode "valid" only the values where the two lists overlap completely (here just y[2] = 4.25).
- A. V. Oppenheim, A. S. Willsky, S. H. Nawab, Signals and Systems, 2nd ed., Pearson, 1997 (§2.1: Discrete-Time LTI Systems: The Convolution Sum)
- A. V. Oppenheim, R. W. Schafer, Discrete-Time Signal Processing, 3rd ed., Pearson, 2010 (ch. 2: linear time-invariant systems and the convolution sum)
- B. P. Lathi, R. A. Green, Linear Systems and Signals, 3rd ed., Oxford University Press, 2018 (Internet Archive record)
- MIT OpenCourseWare RES.6-007 Signals and Systems (A. V. Oppenheim), Lecture 4: Convolution (the convolution sum for discrete-time LTI systems)
- MIT OpenCourseWare 6.341 Discrete-Time Signal Processing (A. V. Oppenheim, Fall 2005)
- MIT OpenCourseWare 6.003 Signals and Systems (Fall 2011), Lecture 8: Convolution
- NumPy documentation: numpy.convolve (definition, full mode, length N + M − 1)
- Convolution (Wikipedia): discrete convolution of sequences
اختبار يتاح بعد الخطوة الأخيرة.