0
0
SciPydata~10 mins

Applying filters (lfilter, sosfilt) in SciPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to apply a filter using lfilter.

SciPy
from scipy.signal import lfilter
b = [0.2, 0.2, 0.2, 0.2, 0.2]
a = [1]
signal = [1, 2, 3, 4, 5]
filtered = lfilter([1], a, signal)
print(filtered)
Drag options to blanks, or click blank then click option'
Ab
Ba
Csignal
D[1, 0, 0, 0, 0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using the denominator coefficients a as the first argument instead of numerator b.
Passing the signal as the first argument.
2fill in blank
medium

Complete the code to apply a second-order sections filter using sosfilt.

SciPy
from scipy.signal import sosfilt
import numpy as np
sos = np.array([[1, 2, 1, 1, -0.5, 0.25]])
signal = [1, 2, 3, 4, 5]
filtered = sosfilt([1], signal)
print(filtered)
Drag options to blanks, or click blank then click option'
Asignal
Bsos
C[0.2, 0.2, 0.2, 0.2, 0.2]
D[1, 0, 0, 0, 0]
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the signal as the first argument instead of the filter coefficients.
Using lfilter instead of sosfilt for second-order sections.
3fill in blank
hard

Fix the error in the code to correctly apply lfilter with numerator and denominator coefficients.

SciPy
from scipy.signal import lfilter
b = [0.1, 0.1, 0.1]
a = [1, -0.8]
signal = [1, 2, 3, 4, 5]
filtered = lfilter(b, [1], signal)
print(filtered)
Drag options to blanks, or click blank then click option'
Aa
Bb
Csignal
D[1]
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping numerator and denominator coefficients.
Passing the signal as the second argument.
4fill in blank
hard

Fill both blanks to create a dictionary of filtered signals for words longer than 3 characters.

SciPy
from scipy.signal import lfilter
b = [0.2, 0.2, 0.2, 0.2, 0.2]
a = [1]
words = ['data', 'ai', 'science', 'ml']
filtered_signals = {word: lfilter([1], [2], [1, 2, 3, 4, 5]) for word in words if len(word) > 3}
print(filtered_signals)
Drag options to blanks, or click blank then click option'
Ab
Ba
C[1]
Dsignal
Attempts:
3 left
💡 Hint
Common Mistakes
Using the signal or [1] as filter coefficients.
Swapping numerator and denominator coefficients.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension filtering signals with condition on values.

SciPy
from scipy.signal import lfilter
b = [0.1, 0.1, 0.1]
a = [1, -0.8]
data = {'x': 0.5, 'y': 1.5, 'z': 2.5}
result = {k[1]: lfilter(b, a, [1, 2, 3]) for k, v in data.items() if v [2] 1 and k != [3]
print(result)
Drag options to blanks, or click blank then click option'
A.upper()
B>
C'y'
D.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using .upper() instead of .lower() for key modification.
Using '<' instead of '>' for filtering values.
Not quoting the key string in the condition.