Lesson 12 of 15
FIR Filters
FIR Filters
A Finite Impulse Response (FIR) filter is defined by its impulse response of length . The output is a causal convolution of the input with :
where for . The output has the same length as .
Why FIR?
FIR filters are:
- Always stable — bounded input → bounded output
- Linear phase — symmetric gives constant group delay (no phase distortion)
- Flexible — any frequency response can be approximated
Sinc Lowpass Filter
The ideal lowpass filter with cutoff (normalized, ) has the impulse response:
where and when .
For (quarter-band) with :
Your Task
Implement:
fir_filter(x, h)— causal FIR convolution, output same length assinc_lowpass(fc, N)— returns coefficients for ideal lowpass
import math
def fir_filter(x, h):
result = []
for n in range(len(x)):
val = 0.0
for k in range(len(h)):
if n - k >= 0:
val += h[k] * x[n - k]
result.append(val)
return result
def sinc_lowpass(fc, N):
h = []
center = N // 2
for n in range(N + 1):
x = n - center
if x == 0:
h.append(2 * fc)
else:
h.append(2 * fc * math.sin(math.pi * 2 * fc * x) / (math.pi * 2 * fc * x))
return hPython runtime loading...
Loading...
Click "Run" to execute your code.