Bird
Raised Fist0
Signal Processingdata~5 mins

IIR filter structure (feedback loops) in Signal Processing

Choose your learning style10 modes available
Introduction

An IIR filter uses past outputs to shape the current output. This feedback loop helps create filters that can be very efficient and effective.

When you want to remove noise from a sound recording in real time.
When designing a simple equalizer for audio devices.
When smoothing sensor data that changes quickly but needs to stay responsive.
When you need a filter that uses less memory and computation than other types.
When you want to create a filter with a sharp cutoff using fewer coefficients.
Syntax
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).

Examples
This example uses one past input and one past output to calculate the current output.
Signal Processing
y[n] = 0.5 * x[n] + 0.5 * x[n-1] - 0.3 * y[n-1]
This uses two past inputs and two past outputs for a more complex filter.
Signal Processing
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]
Sample Program

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.

Signal Processing
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()
OutputSuccess
Important Notes

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.

Summary

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

(1/5)
1. What is the main characteristic of an IIR filter compared to an FIR filter?
easy
A. It only uses current and past inputs without feedback.
B. It has no memory of past inputs or outputs.
C. It uses feedback from past outputs to calculate the current output.
D. It always produces a finite impulse response.

Solution

  1. 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.
  2. Step 2: Compare with FIR filter

    FIR filters only use current and past input values without feedback, so they do not use past outputs.
  3. Final Answer:

    It uses feedback from past outputs to calculate the current output. -> Option C
  4. Quick Check:

    IIR uses feedback = B [OK]
Hint: IIR filters always use past outputs (feedback) [OK]
Common Mistakes:
  • Confusing IIR with FIR filters
  • Thinking IIR filters have no memory
  • Assuming feedback means no inputs are used
2. Which of the following is the correct difference equation form for a simple IIR filter with feedback coefficients \(a_1\) and feedforward coefficient \(b_0\)?
y[n] = ?
easy
A. y[n] = b_0 * x[n] - a_1 * y[n-1]
B. y[n] = b_0 * x[n] + a_1 * y[n-1]
C. y[n] = b_0 * x[n-1] + a_1 * y[n]
D. y[n] = b_0 * y[n-1] + a_1 * x[n]

Solution

  1. 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\).
  2. 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.
  3. Final Answer:

    y[n] = b_0 * x[n] - a_1 * y[n-1] -> Option A
  4. Quick Check:

    Feedback subtracts past output = A [OK]
Hint: Feedback term usually subtracts past output in IIR [OK]
Common Mistakes:
  • Using addition instead of subtraction for feedback
  • Mixing input and output indices
  • Placing feedback on input instead of output
3. Given the Python code below implementing a simple IIR filter:
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?
medium
A. [0, 1, 1.5, 2.25]
B. [0, 1.0, 1.5, 2.25]
C. [0, 1, 2, 3]
D. [0, 1, 2.5, 4.25]

Solution

  1. 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]
  2. Step 2: Compare to options

    The printed output [0, 1.0, 1.5, 2.25] matches [0, 1.0, 1.5, 2.25].
  3. Final Answer:

    [0, 1.0, 1.5, 2.25] -> Option B
  4. Quick Check:

    Feedback reduces output gradually = C [OK]
Hint: Trace output stepwise using previous output values [OK]
Common Mistakes:
  • Ignoring initial output value y[0]
  • Mixing input and output indices
  • Forgetting feedback sign or coefficient
4. The following code attempts to implement an IIR filter but produces an error:
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?
medium
A. IndexError because y[n-1] is accessed before y has enough elements.
B. SyntaxError due to missing colon in function definition.
C. TypeError because x is not iterable.
D. No error; code runs correctly.

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    No error; code runs correctly. -> Option D
  4. Quick Check:

    Negative indexing avoids IndexError = D [OK]
Hint: Remember Python allows negative indexing safely [OK]
Common Mistakes:
  • Assuming y[n-1] causes IndexError at n=0
  • Confusing syntax errors with runtime errors
  • Ignoring Python's negative index behavior
5. You want to design an IIR filter to smooth a noisy signal. The filter uses the equation:
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?
hard
A. 10
B. 7
C. 3
D. 0

Solution

  1. Step 1: Understand steady-state condition

    At steady state, output does not change, so y[n] = y[n-1] = y_ss (steady-state output).
  2. 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 = 10
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    Steady output equals input for this filter = C [OK]
Hint: Set y[n] = y[n-1] to find steady-state output [OK]
Common Mistakes:
  • Ignoring steady-state assumption
  • Not solving algebraic equation correctly
  • Confusing input and output values
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