An IIR filter uses past outputs to shape the current output. This feedback loop helps create filters that can be very efficient and effective.
IIR filter structure (feedback loops) in Signal Processing
y[n] = (b0 * x[n]) + (b1 * x[n-1]) + ... + (bM * x[n-M]) - (a1 * y[n-1]) - ... - (aN * y[n-N])
x[n] is the current input sample.
y[n] is the current output sample.
The b coefficients multiply input samples, and the a coefficients multiply past output samples (feedback).
y[n] = 0.5 * x[n] + 0.5 * x[n-1] - 0.3 * y[n-1]
y[n] = 0.2 * x[n] + 0.1 * x[n-1] + 0.1 * x[n-2] - 0.4 * y[n-1] - 0.1 * y[n-2]
This code creates a step input signal and applies a simple IIR filter using the difference equation with feedback loops. It then plots both the input and filtered output to show how the filter smooths the step.
import numpy as np import matplotlib.pyplot as plt # Define input signal: a step signal x = np.zeros(50) x[10:] = 1 # Step starts at sample 10 # Coefficients for a simple IIR filter b = [0.5, 0.5] # feedforward coefficients a = [1.0, -0.3] # feedback coefficients (a[0] is always 1) # Initialize output array y = np.zeros_like(x) # Apply IIR filter using difference equation for n in range(len(x)): # Feedforward part ff = 0 for i in range(len(b)): if n - i >= 0: ff += b[i] * x[n - i] # Feedback part fb = 0 for j in range(1, len(a)): if n - j >= 0: fb += a[j] * y[n - j] y[n] = ff - fb # Plot input and output plt.plot(x, label='Input x[n]') plt.plot(y, label='Output y[n]') plt.title('Simple IIR Filter with Feedback Loop') plt.xlabel('Sample n') plt.ylabel('Amplitude') plt.legend() plt.grid(True) plt.show()
The feedback loop means the output depends on past outputs, so the filter can 'remember' past inputs.
Make sure the first feedback coefficient a[0] is 1 for the standard form.
Improper coefficients can make the filter unstable, causing outputs to grow without bound.
An IIR filter uses both past inputs and past outputs to calculate the current output.
Feedback loops allow the filter to be efficient and have sharp responses.
Understanding the difference equation helps you design and implement these filters.
Practice
Solution
Step 1: Understand IIR filter definition
An IIR (Infinite Impulse Response) filter uses feedback loops, meaning it uses past output values to compute the current output.Step 2: Compare with FIR filter
FIR filters only use current and past input values without feedback, so they do not use past outputs.Final Answer:
It uses feedback from past outputs to calculate the current output. -> Option CQuick Check:
IIR uses feedback = B [OK]
- Confusing IIR with FIR filters
- Thinking IIR filters have no memory
- Assuming feedback means no inputs are used
y[n] = ?Solution
Step 1: Recall standard IIR difference equation
The standard form is \(y[n] = b_0 x[n] - a_1 y[n-1]\) where feedback uses subtraction of past output scaled by \(a_1\).Step 2: Check each option
y[n] = b_0 * x[n] - a_1 * y[n-1] matches the correct form with subtraction and correct indexing.Final Answer:
y[n] = b_0 * x[n] - a_1 * y[n-1] -> Option AQuick Check:
Feedback subtracts past output = A [OK]
- Using addition instead of subtraction for feedback
- Mixing input and output indices
- Placing feedback on input instead of output
def iir_filter(x, b0, a1):
y = [0]
for n in range(1, len(x)):
y.append(b0 * x[n] - a1 * y[n-1])
return y
x = [0, 1, 2, 3]
b0 = 1
a1 = 0.5
output = iir_filter(x, b0, a1)
print(output)What is the output printed?
Solution
Step 1: Trace the code step-by-step
y = [0]
n=1: y.append(1 * 1 - 0.5 * 0) = 1.0 y = [0, 1.0]
n=2: y.append(1 * 2 - 0.5 * 1.0) = 1.5 y = [0, 1.0, 1.5]
n=3: y.append(1 * 3 - 0.5 * 1.5) = 2.25 y = [0, 1.0, 1.5, 2.25]Step 2: Compare to options
The printed output [0, 1.0, 1.5, 2.25] matches [0, 1.0, 1.5, 2.25].Final Answer:
[0, 1.0, 1.5, 2.25] -> Option BQuick Check:
Feedback reduces output gradually = C [OK]
- Ignoring initial output value y[0]
- Mixing input and output indices
- Forgetting feedback sign or coefficient
def iir_filter(x, b0, a1):
y = [0]
for n in range(len(x)):
y.append(b0 * x[n] - a1 * y[n-1])
return y
x = [1, 2, 3]
b0 = 1
a1 = 0.5
print(iir_filter(x, b0, a1))What is the cause of the error?
Solution
Step 1: Analyze loop and list indexing
The loop runs from n=0 to len(x)-1. At n=0, code tries to access y[n-1] = y[-1], which is the last element of y (initially y=[0]). This is valid in Python, so no error here.Step 2: Check list growth and indexing
At n=0, y has 1 element, y[-1] is y[0]=0, so no error.
At n=1, y has 2 elements, y[-1] is last element, no error.
Actually, no IndexError occurs due to negative indexing.
But the output list y grows longer than input x by 1 element, which may be unintended but not an error.
Re-examine code carefully: No syntax or type errors.
Therefore, no error occurs, so No error; code runs correctly. is correct.Final Answer:
No error; code runs correctly. -> Option DQuick Check:
Negative indexing avoids IndexError = D [OK]
- Assuming y[n-1] causes IndexError at n=0
- Confusing syntax errors with runtime errors
- Ignoring Python's negative index behavior
y[n] = 0.3 * x[n] + 0.7 * y[n-1]If the input signal is constant at 10, what steady-state output value will the filter produce?
Solution
Step 1: Understand steady-state condition
At steady state, output does not change, so y[n] = y[n-1] = y_ss (steady-state output).Step 2: Solve equation for y_ss
Substitute y[n] = y[n-1] = y_ss and x[n] = 10:
y_ss = 0.3 * 10 + 0.7 * y_ss
y_ss - 0.7 * y_ss = 3
0.3 * y_ss = 3
y_ss = 3 / 0.3 = 10Final Answer:
10 -> Option AQuick Check:
Steady output equals input for this filter = C [OK]
- Ignoring steady-state assumption
- Not solving algebraic equation correctly
- Confusing input and output values
Ready to prove it?
Pass the test and this pattern moves unknown
