0
0
SciPydata~10 mins

Applying filters (lfilter, sosfilt) in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Applying filters (lfilter, sosfilt)
Start: Input Signal
Choose Filter Type
lfilter
Apply filter coefficients
Filtered Output Signal
End
The input signal is processed by choosing either lfilter or sosfilt, applying filter coefficients or second-order sections, producing the filtered output.
Execution Sample
SciPy
from scipy.signal import lfilter, sosfilt, butter
b, a = butter(2, 0.3)
x = [1, 2, 3, 4, 5]
y = lfilter(b, a, x)
This code creates a Butterworth filter and applies it to a simple signal using lfilter.
Execution Table
StepInput x[n]Filter Coefficients bFilter Coefficients aCalculationOutput y[n]
11[0.2066, 0.4131, 0.2066][1.0, -0.3695, 0.1958]y[0] = b0*x[0]0.2066
22[0.2066, 0.4131, 0.2066][1.0, -0.3695, 0.1958]y[1] = b0*x[1] + b1*x[0] - a1*y[0]0.9026
33[0.2066, 0.4131, 0.2066][1.0, -0.3695, 0.1958]y[2] = b0*x[2] + b1*x[1] + b2*x[0] - a1*y[1] - a2*y[0]1.9457
44[0.2066, 0.4131, 0.2066][1.0, -0.3695, 0.1958]y[3] = b0*x[3] + b1*x[2] + b2*x[1] - a1*y[2] - a2*y[1]3.0923
55[0.2066, 0.4131, 0.2066][1.0, -0.3695, 0.1958]y[4] = b0*x[4] + b1*x[3] + b2*x[2] - a1*y[3] - a2*y[2]4.1721
6----End of signal, filtering complete
💡 Reached end of input signal x, no more samples to filter.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
x[n][1, 2, 3, 4, 5]12345-
y[n][0, 0, 0, 0, 0]0.20660.90261.94573.09234.17214.1721
Key Moments - 3 Insights
Why does the output y[n] depend on previous outputs y[n-1], y[n-2]?
Because the filter is recursive (IIR), each output uses past outputs as feedback, shown in execution_table rows 2-5 where a1 and a2 coefficients multiply previous y values.
What is the difference between lfilter and sosfilt in applying filters?
lfilter uses direct filter coefficients (b, a), while sosfilt uses second-order sections for better numerical stability, especially for higher order filters.
Why do we need to use butter() before lfilter or sosfilt?
butter() designs the filter and returns coefficients needed by lfilter or sosfilt to apply the filter to the signal.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output y[3] after filtering the input x[3]?
A3.0923
B1.9457
C0.9026
D4.1721
💡 Hint
Check the row where Step is 4 in the execution_table for y[3].
At which step does the filtering process stop according to the execution_table?
AStep 5
BStep 4
CStep 6
DStep 3
💡 Hint
Look for the row with 'End of signal' in the Calculation column.
If the input signal x had one more sample, how would the variable_tracker change?
AIt would remove the last column.
BIt would add one more column for After 6 with new x and y values.
CIt would keep the same columns but change y values.
DIt would reset all values to zero.
💡 Hint
Variable tracker columns represent steps after each input sample is processed.
Concept Snapshot
Applying filters with scipy.signal:
- Use butter() to design filter coefficients (b, a) or sos.
- Use lfilter(b, a, x) for direct filtering.
- Use sosfilt(sos, x) for stable filtering with second-order sections.
- Output depends on current and past inputs and outputs (IIR filters).
- Filtering stops when input signal ends.
Full Transcript
This visual execution shows how scipy.signal applies filters using lfilter and sosfilt. Starting with an input signal, we design filter coefficients using butter(). Then lfilter applies these coefficients step-by-step to produce the filtered output. Each output sample depends on current and previous inputs and previous outputs, demonstrating recursive filtering. The execution table traces each calculation and output value. The variable tracker shows how input and output values change after each step. Key moments clarify why outputs depend on past outputs and the difference between lfilter and sosfilt. The quiz tests understanding of output values, stopping conditions, and variable tracking. This helps beginners see filtering as a stepwise process, not just a black box.