0
0
SciPydata~20 mins

Applying filters (lfilter, sosfilt) in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Filter Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output 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)
A[0.5 1.5 2.5 3.5 4.5]
B[0.5 1. 1.5 2. 2.5]
C[1. 1.5 2.5 3.5 4.5]
D[0.5 1. 2. 3. 4. ]
Attempts:
2 left
💡 Hint
Remember that lfilter applies the filter coefficients as a convolution-like operation starting from the first element.
data_output
intermediate
1:30remaining
Number 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))
A7
B8
C9
D6
Attempts:
2 left
💡 Hint
The output length of sosfilt matches the input length.
🔧 Debug
advanced
1:30remaining
Identify 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)
ATypeError: lfilter() missing 1 required positional argument: 'x'
BValueError: Input arrays must have the same length
CIndexError: list index out of range
DNo error, prints filtered output
Attempts:
2 left
💡 Hint
Check the function call arguments carefully.
🧠 Conceptual
advanced
1:30remaining
Effect of filter coefficients on output shape
Which statement about the output length of lfilter and sosfilt is correct?
A<code>lfilter</code> output length is always shorter than input length; <code>sosfilt</code> output length equals input length
B<code>lfilter</code> output length is always longer than input length; <code>sosfilt</code> output length equals input length
C<code>lfilter</code> output length equals input length; <code>sosfilt</code> output length is always longer
DBoth <code>lfilter</code> and <code>sosfilt</code> output lengths equal input length
Attempts:
2 left
💡 Hint
Think about how these filters process signals sample by sample.
🚀 Application
expert
2:30remaining
Filter 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))
A-0.005
B-0.050
C0.002
D0.050
Attempts:
2 left
💡 Hint
The filter smooths the signal, so the mean should be close to zero.