Challenge - 5 Problems
Filter Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of lfilter with simple coefficients
What is the output array after applying
lfilter with given coefficients on the input signal?SciPy
from scipy.signal import lfilter import numpy as np b = [0.5, 0.5] a = [1] x = np.array([1, 2, 3, 4, 5]) y = lfilter(b, a, x) print(y)
Attempts:
2 left
💡 Hint
Remember that
lfilter applies the filter coefficients as a convolution-like operation starting from the first element.✗ Incorrect
The filter coefficients b = [0.5, 0.5] average the current and previous input values. The output at each step is 0.5 * x[n] + 0.5 * x[n-1], with x[-1] assumed zero at start.
❓ data_output
intermediateNumber of output samples from sosfilt
Given a second-order sections filter and an input signal of length 8, how many samples does
sosfilt return?SciPy
from scipy.signal import sosfilt import numpy as np sos = np.array([[1, 0, -1, 1, -1, 0]]) x = np.arange(8) y = sosfilt(sos, x) print(len(y))
Attempts:
2 left
💡 Hint
The output length of
sosfilt matches the input length.✗ Incorrect
sosfilt applies the filter to the input signal and returns an output array of the same length as the input.
🔧 Debug
advancedIdentify the error in lfilter usage
What error will this code raise when run?
SciPy
from scipy.signal import lfilter b = [1, -1] a = [1, -0.9] x = [1, 2, 3] y = lfilter(b, a) print(y)
Attempts:
2 left
💡 Hint
Check the function call arguments carefully.
✗ Incorrect
The lfilter function requires three arguments: b, a, and the input signal x. Omitting x causes a TypeError.
🧠 Conceptual
advancedEffect of filter coefficients on output shape
Which statement about the output length of
lfilter and sosfilt is correct?Attempts:
2 left
💡 Hint
Think about how these filters process signals sample by sample.
✗ Incorrect
Both lfilter and sosfilt produce output arrays with the same length as the input signal.
🚀 Application
expertFilter a noisy signal and identify the filtered output mean
Given a noisy signal
x and a low-pass filter defined by second-order sections sos, what is the mean of the filtered output y?SciPy
import numpy as np from scipy.signal import sosfilt np.random.seed(0) x = np.sin(np.linspace(0, 2*np.pi, 100)) + 0.5 * np.random.randn(100) sos = np.array([[0.2929, 0.5858, 0.2929, 1.0000, -0.0000, 0.1716]]) y = sosfilt(sos, x) print(round(np.mean(y), 3))
Attempts:
2 left
💡 Hint
The filter smooths the signal, so the mean should be close to zero.
✗ Incorrect
The filtered signal mean is close to zero because the low-pass filter removes noise but preserves the sine wave's zero-centered nature.
