Challenge - 5 Problems
Convolution Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of simple 1D convolution
What is the output of this code using
np.convolve() with default mode?NumPy
import numpy as np x = np.array([1, 2, 3]) h = np.array([0, 1, 0.5]) result = np.convolve(x, h) print(result)
Attempts:
2 left
💡 Hint
Remember that default mode is 'full' which gives the complete convolution result.
✗ Incorrect
The convolution sums overlapping products of x and h. The output length is len(x)+len(h)-1 = 5. Calculating stepwise: 0*1=0, 1*1=1, (2*1 + 1*0.5)=2.5, (3*1 + 2*0.5)=4, 3*0.5=1.5.
❓ data_output
intermediate1:30remaining
Length of convolution output with 'valid' mode
Given two arrays
a = np.array([1, 2, 3, 4]) and b = np.array([1, 0, -1]), what is the length of the output when convolving with mode='valid'?NumPy
import numpy as np a = np.array([1, 2, 3, 4]) b = np.array([1, 0, -1]) result = np.convolve(a, b, mode='valid') print(len(result))
Attempts:
2 left
💡 Hint
Length in 'valid' mode is max(len(a), len(b)) - min(len(a), len(b)) + 1.
✗ Incorrect
For 'valid' mode, output length is len(a) - len(b) + 1 = 4 - 3 + 1 = 2, so answer is 2.
🔧 Debug
advanced2:00remaining
Identify the error in convolution code
What error does this code raise when run?
NumPy
import numpy as np x = np.array([1, 2, 3]) h = np.array([[0, 1, 0.5]]) result = np.convolve(x, h) print(result)
Attempts:
2 left
💡 Hint
Check the shape of the arrays passed to np.convolve.
✗ Incorrect
np.convolve expects 1D arrays. Here, h is 2D (shape (1,3)) which causes a TypeError indicating 1D arrays expected.
🧠 Conceptual
advanced1:30remaining
Effect of 'same' mode in np.convolve()
Which statement best describes the output length when using
np.convolve(a, b, mode='same')?Attempts:
2 left
💡 Hint
The 'same' mode centers the full convolution output to match the first input's length.
✗ Incorrect
In 'same' mode, np.convolve returns an output array with the same length as the first input array, centered with respect to the full convolution.
🚀 Application
expert2:30remaining
Using convolution to compute moving average
You want to compute a simple moving average of window size 3 on array
data = np.array([1, 2, 3, 4, 5]). Which np.convolve() call produces the correct moving average values aligned with the original data length?Attempts:
2 left
💡 Hint
Moving average should have the same length as data and average over 3 points centered.
✗ Incorrect
Using mode='same' with kernel np.ones(3)/3 gives a moving average aligned with the original data length. 'valid' mode shortens output, 'full' extends it, and option C divides after convolution with unnormalized kernel causing wrong values.