Lesson 5 of 15
Convolution
Convolution
Convolution is the central operation of linear time-invariant (LTI) system theory. The output of any LTI system is the convolution of the input signal with the system's impulse response.
Linear Convolution
Given signals of length and of length , their convolution has length :
where when is out of bounds.
How to Compute It
For each output index from to :
- Slide over , multiply corresponding elements, sum the products.
Example
convolved with :
Result:
Applications
- Filtering — convolving with a low-pass kernel smooths the signal
- Echo — adding a delayed copy of the signal
- Cross-correlation — closely related to convolution
Your Task
Implement convolve(x, h) that returns the full linear convolution of lists and (no imports needed).
def convolve(x, h):
N = len(x) + len(h) - 1
result = [0.0] * N
for i, xi in enumerate(x):
for j, hj in enumerate(h):
result[i + j] += xi * hj
return result
print(convolve([1, 2, 3], [1, 1])) # [1.0, 3.0, 5.0, 3.0]Python runtime loading...
Loading...
Click "Run" to execute your code.