Bird
Raised Fist0
SciPydata~10 mins

IIR filter design (butter, cheby1) in SciPy - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - IIR filter design (butter, cheby1)
Choose filter type: Butterworth or Chebyshev1
↓
Set filter order and cutoff frequencies
↓
Call scipy.signal function butter() or cheby1()
↓
Get filter coefficients (b, a)
↓
Use coefficients to filter data or analyze frequency response
↓
End
This flow shows how to design an IIR filter by choosing type, setting parameters, getting coefficients, and applying the filter.
Execution Sample
SciPy
from scipy.signal import butter, cheby1
b, a = butter(3, 0.2)
print(b, a)
Design a 3rd order Butterworth lowpass filter with cutoff 0.2 (normalized frequency) and print coefficients.
Execution Table
StepActionInput ParametersOutput (b coefficients)Output (a coefficients)
1Call butter()order=3, cutoff=0.2[0.0181, 0.0543, 0.0543, 0.0181][1.0, -1.476, 0.9565, -0.1976]
2Print coefficientsb, a arraysPrinted b coefficientsPrinted a coefficients
3EndNo further action--
💡 Filter coefficients computed and printed; execution ends.
Variable Tracker
VariableStartAfter butter() callFinal
bNone[0.0181, 0.0543, 0.0543, 0.0181][0.0181, 0.0543, 0.0543, 0.0181]
aNone[1.0, -1.476, 0.9565, -0.1976][1.0, -1.476, 0.9565, -0.1976]
Key Moments - 2 Insights
Why do the coefficients b and a have different values?
The b coefficients represent the numerator polynomial and a coefficients the denominator polynomial of the filter transfer function. For a 3rd order filter, b has length 4 (order+1) and a also length 4, but their values differ because they define different parts of the filter.
What does the cutoff frequency 0.2 mean in butter(3, 0.2)?
The cutoff 0.2 is normalized frequency, meaning 0.2 times the Nyquist frequency (half the sampling rate). So the filter passes frequencies below 0.2 * Nyquist and attenuates above.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of the first b coefficient after butter() call?
A0.0181
B1.0
C-1.476
D0.2
💡 Hint
Check the 'Output (b coefficients)' column in step 1 of the execution table.
At which step does the filter coefficients get printed?
AStep 1
BStep 3
CStep 2
DNever printed
💡 Hint
Look at the 'Action' column in the execution table for printing.
If we change the order from 3 to 5, how will the length of b coefficients change?
ALength will be 5
BLength will be 6
CLength will be 3
DLength stays the same
💡 Hint
Recall that for order N, coefficient arrays have length N+1 as shown in variable_tracker.
Concept Snapshot
IIR filter design with scipy.signal:
- Use butter(order, cutoff) or cheby1(order, ripple, cutoff)
- cutoff is normalized frequency (0 to 1, 1 = Nyquist)
- Returns b (numerator) and a (denominator) coefficients
- Use coefficients to filter signals or analyze response
- Order controls filter sharpness and complexity
Full Transcript
This visual execution shows how to design an IIR filter using scipy.signal butter() function. First, you choose the filter type and parameters like order and cutoff frequency. Then you call butter() with these parameters. The function returns two arrays: b and a, which are the filter coefficients. These coefficients define the filter's behavior. The example shows a 3rd order Butterworth lowpass filter with cutoff 0.2 normalized frequency. The coefficients are printed. The variable tracker shows how b and a change from None to their computed values. Key moments clarify why b and a have different roles and what cutoff frequency means. The quiz questions test understanding of coefficients values, steps, and effect of changing order. The snapshot summarizes the main points for quick reference.

Practice

(1/5)
1. What is the main difference between a Butterworth and a Chebyshev IIR filter in scipy.signal?
easy
A. Butterworth filters are FIR, Chebyshev filters are IIR.
B. Butterworth filters have ripples, Chebyshev filters have a smooth response.
C. Butterworth filters are only for high-pass, Chebyshev only for low-pass.
D. Butterworth filters have a smooth frequency response, Chebyshev filters have ripples in the passband.

Solution

  1. Step 1: Understand Butterworth filter characteristics

    Butterworth filters are designed to have a maximally flat frequency response in the passband, meaning no ripples.
  2. Step 2: Understand Chebyshev filter characteristics

    Chebyshev filters allow ripples in the passband to achieve a sharper cutoff compared to Butterworth filters.
  3. Final Answer:

    Butterworth filters have a smooth frequency response, Chebyshev filters have ripples in the passband. -> Option D
  4. Quick Check:

    Butterworth smooth, Chebyshev ripples = D [OK]
Hint: Butterworth = smooth, Chebyshev = ripples [OK]
Common Mistakes:
  • Confusing which filter has ripples
  • Thinking Butterworth is FIR
  • Assuming filter types are limited to passband types
2. Which of the following is the correct way to design a 4th order Butterworth lowpass filter with cutoff frequency 0.3 (normalized) using scipy.signal.butter?
easy
A. b, a = butter(0.3, 4, btype='low')
B. b, a = butter(4, 0.3, btype='low')
C. b, a = butter(4, 0.3, type='lowpass')
D. b, a = butter(order=4, cutoff=0.3, kind='low')

Solution

  1. Step 1: Recall butter function signature

    The function signature is butter(N, Wn, btype='low', analog=False, output='ba'), where N is order and Wn is cutoff frequency.
  2. Step 2: Check correct parameter usage

    b, a = butter(4, 0.3, btype='low') uses butter(4, 0.3, btype='low'), which matches the signature correctly.
  3. Final Answer:

    b, a = butter(4, 0.3, btype='low') -> Option B
  4. Quick Check:

    Order first, cutoff second, btype='low' = A [OK]
Hint: Order first, cutoff second, btype='low' in butter() [OK]
Common Mistakes:
  • Swapping order and cutoff parameters
  • Using wrong keyword names like 'type' or 'kind'
  • Passing cutoff frequency as first argument
3. What will be the output shape of the coefficients when running this code?
from scipy.signal import cheby1
b, a = cheby1(3, 1, 0.4, btype='high')
medium
A. (3,), (3,)
B. (3, 3), (3, 3)
C. (4,), (4,)
D. (2,), (2,)

Solution

  1. Step 1: Understand cheby1 output

    The function returns numerator (b) and denominator (a) coefficients arrays each of length N+1, where N is filter order.
  2. Step 2: Calculate length for order 3

    For order 3, length is 3+1 = 4, so both b and a have shape (4,).
  3. Final Answer:

    (4,), (4,) -> Option C
  4. Quick Check:

    Length = order + 1 = 4 [OK]
Hint: Coefficients length = order + 1 [OK]
Common Mistakes:
  • Assuming length equals order
  • Expecting 2D arrays instead of 1D
  • Confusing numerator and denominator shapes
4. Identify the error in this code snippet for designing a Chebyshev type I filter:
from scipy.signal import cheby1
b, a = cheby1(5, 0.5, 0.2, btype='bandpass')
medium
A. The cutoff frequency should be a list or tuple for bandpass, not a single float.
B. The ripple parameter is missing and required.
C. The order parameter should be a float, not integer.
D. The btype 'bandpass' is not supported by cheby1.

Solution

  1. Step 1: Check bandpass cutoff frequency format

    For bandpass filters, cutoff frequency must be a sequence (list or tuple) of two frequencies, not a single float.
  2. Step 2: Verify other parameters

    Order as integer and ripple (second parameter) are correctly provided; btype 'bandpass' is valid.
  3. Final Answer:

    The cutoff frequency should be a list or tuple for bandpass, not a single float. -> Option A
  4. Quick Check:

    Bandpass cutoff needs two frequencies = C [OK]
Hint: Bandpass cutoff must be two frequencies in a list/tuple [OK]
Common Mistakes:
  • Passing single float for bandpass cutoff
  • Confusing ripple parameter position
  • Using unsupported btype values
5. You want to design a Chebyshev type I lowpass filter with order 6, 0.5 dB ripple, and cutoff frequency 0.25. Which code snippet correctly creates the filter and applies it to a signal x using scipy.signal.lfilter?
hard
A. from scipy.signal import cheby1, lfilter b, a = cheby1(6, 0.5, 0.25, btype='low') y = lfilter(b, a, x)
B. from scipy.signal import cheby1, lfilter b, a = cheby1(6, 0.5, 0.25, btype='high') y = lfilter(a, b, x)
C. from scipy.signal import butter, lfilter b, a = butter(6, 0.25, btype='low') y = lfilter(b, a, x)
D. from scipy.signal import cheby1, lfilter b, a = cheby1(0.5, 6, 0.25, btype='low') y = lfilter(b, a, x)

Solution

  1. Step 1: Verify cheby1 parameters order

    Order is first parameter (6), ripple second (0.5), cutoff third (0.25), and btype='low' for lowpass.
  2. Step 2: Check lfilter usage

    lfilter takes numerator coefficients (b), denominator coefficients (a), and signal (x) in that order.
  3. Final Answer:

    from scipy.signal import cheby1, lfilter b, a = cheby1(6, 0.5, 0.25, btype='low') y = lfilter(b, a, x) -> Option A
  4. Quick Check:

    Correct order and lfilter usage = A [OK]
Hint: Order, ripple, cutoff in cheby1; lfilter(b, a, x) [OK]
Common Mistakes:
  • Swapping order and ripple parameters
  • Using butter instead of cheby1
  • Swapping b and a in lfilter
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard