Lesson 13 of 15
IIR Filters
IIR Filters
An Infinite Impulse Response (IIR) filter uses feedback — the output depends on both the input and previous outputs. The simplest first-order IIR:
with (causal initialization).
Stability
The filter is stable when . If , the output diverges.
Exponential Smoother
A common application is the exponential moving average (EMA):
This is iir_filter(x, a=1-alpha, b=alpha). The parameter controls smoothing:
- close to 1: fast response, little smoothing
- close to 0: slow response, heavy smoothing
Example
Signal through filter with , :
The impulse response decays exponentially — hence "infinite" (theoretically never reaches zero).
Your Task
Implement:
iir_filter(x, a, b)— causal first-order IIR:exponential_smoother(x, alpha)— callsiir_filter(x, 1-alpha, alpha)
def iir_filter(x, a, b):
y = []
y_prev = 0.0
for xi in x:
yi = b * xi + a * y_prev
y.append(yi)
y_prev = yi
return y
def exponential_smoother(x, alpha):
return iir_filter(x, 1 - alpha, alpha)Python runtime loading...
Loading...
Click "Run" to execute your code.