from scipy.signal import convolve
x = np.array([1, 2, 3])
k = [1, 0]
result = convolve(x, k, mode='same')
print(result)
medium
A. Lists cannot be used; inputs must be numpy arrays.
B. The mode 'same' is invalid for convolve.
C. Kernel length must be odd for convolution.
D. Missing import for numpy.
Solution
Step 1: Check for undefined variables
The code uses np.array but numpy (as np) is not imported, causing NameError.
Step 2: Validate other parts
Mode 'same' is valid, kernel length can be even, and lists are acceptable (as used for k).
Final Answer:
Missing import for numpy. -> Option D
Quick Check:
Import numpy as np [OK]
Hint: Import numpy as np before using np.array [OK]
Common Mistakes:
Forgetting to import numpy before using np.array
Thinking 'same' mode is invalid
Assuming kernel length must be odd
5. You have two signals: signal = np.array([1, 2, 3, 4]) and kernel = np.array([1, 0, -1]). You want to detect edges by convolving and keep the output the same size as the signal. Which code snippet correctly does this?
hard
A. result = convolve(signal, kernel, mode='valid')
B. result = convolve(signal, kernel, mode='full')
C. result = convolve(signal, kernel, mode='same')
D. result = convolve(signal, kernel, mode='trim')
Solution
Step 1: Understand mode effects on output size
'full' mode gives largest output, 'valid' smaller, 'same' keeps output size equal to first input.
Step 2: Choose mode to keep output size same as signal
Using mode='same' returns output with same length as signal, ideal for edge detection.
Final Answer:
result = convolve(signal, kernel, mode='same') -> Option C
Quick Check:
Use mode='same' for output size match [OK]
Hint: Use mode='same' to keep output size equal input [OK]
Common Mistakes:
Using 'full' mode and getting larger output
Using invalid mode like 'trim'
Using 'valid' mode and losing edges
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.