This code creates a noisy sine wave and applies a simple IIR low-pass filter to reduce noise. The plot shows the original noisy signal and the smoother filtered signal.
Signal Processing
import numpy as np
import matplotlib.pyplot as plt
# Sample input signal: noisy sine wave
fs = 100# Sampling frequency
f = 5# Signal frequency
t = np.arange(0, 1, 1/fs)
x = np.sin(2 * np.pi * f * t) + 0.5 * np.random.randn(len(t))
# IIR filter coefficients (simple low-pass)
b = [0.2929, 0.2929] # feedforward coefficients
a = [-0.4142] # feedback coefficients# Initialize output array
y = np.zeros_like(x)
# Apply IIR filterfor n inrange(len(x)):
x_n = x[n]
x_n_1 = x[n-1] if n-1 >= 0else0
y_n_1 = y[n-1] if n-1 >= 0else0
y[n] = b[0]*x_n + b[1]*x_n_1 - a[0]*y_n_1
# Plot input and filtered output
plt.plot(t, x, label='Noisy Input')
plt.plot(t, y, label='Filtered Output')
plt.legend()
plt.xlabel('Time (seconds)')
plt.title('IIR Filter Example')
plt.show()
OutputSuccess
Important Notes
IIR filters use feedback, so past outputs affect current output.
Be careful with coefficients to keep the filter stable.
Initial past values are often set to zero at the start.
Summary
IIR filters use past inputs and outputs to process signals.
They are good for real-time filtering with less memory.
Choosing correct coefficients is important for good results.
Practice
(1/5)
1. What does an IIR filter use to calculate its current output? y[n] = b0*x[n] + b1*x[n-1] + a1*y[n-1]
easy
A. Only past inputs
B. Only current input
C. Only past outputs
D. Both current and past inputs and past outputs
Solution
Step 1: Understand the difference equation
The equation shows output y[n] depends on current input x[n], past input x[n-1], and past output y[n-1].
Step 2: Identify used terms in calculation
Since both inputs (current and past) and past outputs are used, the filter uses all these values.
Final Answer:
Both current and past inputs and past outputs -> Option D
Quick Check:
Uses current input x[n], past input x[n-1], past output y[n-1] = D [OK]
Hint: Look for both x and y terms in the equation [OK]
Common Mistakes:
Thinking IIR uses only inputs
Confusing IIR with FIR filters
Ignoring past output terms
2. Which of the following is the correct Python syntax to implement one step of an IIR filter with coefficients b0, b1, a1 and inputs x and outputs y arrays?
easy
A. y[n] = b0*x[n] + b1*x[n-1] + a1*y[n-1]
B. y[n] == b0*x[n] + b1*x[n-1] + a1*y[n-1]
C. y[n] := b0*x[n] + b1*x[n-1] + a1*y[n-1]
D. y[n] = b0*x[n] + b1*x[n] + a1*y[n]
Solution
Step 1: Check assignment operator
Python uses single equals sign '=' for assignment, so '==' and ':=' are incorrect here.
Hint: Calculate step-by-step using previous y values [OK]
Common Mistakes:
Ignoring previous output y[n-1]
Using wrong indices for x or y
Rounding errors in calculations
4. Identify the error in this IIR filter Python code snippet:
y = [0]*4
b0, b1, a1 = 0.5, 0.3, 0.2
x = [1, 2, 3, 4]
for n in range(1, 4):
y[n] = b0*x[n] + b1*x[n-1] + a1*y[n]
medium
A. Missing initialization of y[0]
B. Incorrect range in for loop
C. Using y[n] on right side instead of y[n-1]
D. Using list instead of numpy array
Solution
Step 1: Analyze the difference equation implementation
The formula should use previous output y[n-1], but code uses y[n] on right side, causing circular dependency.
Step 2: Check other parts
Range(1,4) is correct for indices 1 to 3. y[0] is initialized to 0, so no missing initialization. Using list is allowed but less efficient; not an error.
Final Answer:
Using y[n] on right side instead of y[n-1] -> Option C
Quick Check:
Previous output must be y[n-1], not y[n] = A [OK]
Hint: Check if past output uses correct index y[n-1] [OK]
Common Mistakes:
Using current output y[n] in calculation
Forgetting to initialize y[0]
Wrong loop range causing index errors
5. You want to implement a second-order IIR filter with coefficients: b0=0.2, b1=0.1, b2=0.1, a1=0.3, a2=0.4 Given input signal x = [1, 2, 3, 4, 5], which Python code correctly computes the output y using the difference equation: y[n] = b0*x[n] + b1*x[n-1] + b2*x[n-2] + a1*y[n-1] + a2*y[n-2]?
hard
A. y = [0]*5
y[0] = b0*x[0]
y[1] = b0*x[1] + b1*x[0] + a1*y[0]
for n in range(2, 5):
y[n] = b0*x[n] + b1*x[n-1] + b2*x[n-2] + a1*y[n-1] + a2*y[n-2]
B. y = [0]*5
for n in range(2, 5):
y[n] = b0*x[n] + b1*x[n-1] + b2*x[n-2] + a1*y[n-1] + a2*y[n-2]
C. y = [0]*5
for n in range(0, 5):
y[n] = b0*x[n] + b1*x[n-1] + b2*x[n-2] + a1*y[n-1] + a2*y[n-2]
D. y = [0]*5
y[0] = b0*x[0]
for n in range(1, 5):
y[n] = b0*x[n] + b1*x[n-1] + b2*x[n-2] + a1*y[n-1] + a2*y[n-2]
Solution
Step 1: Handle initial conditions for n=0 and n=1
For n=0, only x[0] is valid, so y[0] = b0*x[0]. For n=1, x[1], x[0], y[0] exist, so y[1] = b0*x[1] + b1*x[0] + a1*y[0].
Step 2: Loop from n=2 to 4 for full difference equation
From n=2 onwards, all terms x[n-2], y[n-2] exist. y = [0]*5
y[0] = b0*x[0]
y[1] = b0*x[1] + b1*x[0] + a1*y[0]
for n in range(2, 5):
y[n] = b0*x[n] + b1*x[n-1] + b2*x[n-2] + a1*y[n-1] + a2*y[n-2] correctly initializes y[0], y[1] and loops from 2 to 4.
Step 3: Check other options
y = [0]*5
for n in range(2, 5):
y[n] = b0*x[n] + b1*x[n-1] + b2*x[n-2] + a1*y[n-1] + a2*y[n-2] misses initial y[0], y[1] initialization. y = [0]*5
for n in range(0, 5):
y[n] = b0*x[n] + b1*x[n-1] + b2*x[n-2] + a1*y[n-1] + a2*y[n-2] loops from 0 and uses negative indices causing errors. y = [0]*5
y[0] = b0*x[0]
for n in range(1, 5):
y[n] = b0*x[n] + b1*x[n-1] + b2*x[n-2] + a1*y[n-1] + a2*y[n-2] misses y[1] initialization and uses range(1,5) which fails at n=1 for x[n-2].
Final Answer:
Code with y[0] and y[1] explicitly initialized before loop from n=2 -> Option A
Quick Check:
Initialize first two outputs before loop = B [OK]
Hint: Initialize first two outputs before looping from n=2 [OK]
Common Mistakes:
Not initializing y[0] and y[1] before loop
Using negative indices without checks
Looping from 0 causing index errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.