TExcelTAD
Home › Fourier Analysis & Signals › FIR Filter Design

FIR Filter Design

A finite impulse response (FIR) filter computes each output as a weighted sum of the last MM inputs. FIR filters are always stable, can have exactly linear phase (no waveform distortion), and are designed directly from a desired frequency response. The classic method, the windowed sinc, takes the ideal impulse response, truncates it, and tapers it with a window.

The windowed-sinc method

For a low-pass with cutoff fcf_c at sampling rate fsf_s and an odd number of taps M=2K+1M = 2K + 1:

  1. Ideal response: hd[n]=2fcfssinc⁡ ⁣(2fcfs(n−K))h_d[n] = 2\frac{f_c}{f_s}\operatorname{sinc}\!\left(2\frac{f_c}{f_s}(n - K)\right), centered at KK.
  2. Multiply by a window w[n]w[n] of length MM.
  3. Normalize so ∑h=1\sum h = 1 (unit gain at DC).

The window's sidelobes set the stopband attenuation, and its main-lobe width sets the transition width.

python · try it yourself
import numpy as np

fs, fc, M = 1000.0, 100.0, 61
n = np.arange(M)
ideal = 2 * fc / fs * np.sinc(2 * fc / fs * (n - (M - 1) / 2))
windows = {"rectangular": np.ones(M), "hamming": np.hamming(M), "blackman": np.blackman(M)}

for name, w in windows.items():
    h = ideal * w
    h /= h.sum()
    H = np.abs(np.fft.rfft(h, 8192))
    f = np.fft.rfftfreq(8192, 1 / fs)
    worst = [round(float(20 * np.log10(H[f > edge].max())), 1) for edge in (125, 150, 200)]
    print(f"{name:12s} worst attenuation above 125, 150, 200 Hz (dB): {worst}")
rectangular  worst attenuation above 125, 150, 200 Hz (dB): [-27.1, -30.9, -37.9]
hamming      worst attenuation above 125, 150, 200 Hz (dB): [-41.1, -61.1, -62.1]
blackman     worst attenuation above 125, 150, 200 Hz (dB): [-26.3, -75.5, -83.4]

The rectangular window (plain truncation) stalls between −27-27 and −38-38 dB. Hamming reaches about −61-61 dB from 150 Hz on. Blackman goes deepest, about −75-75 dB above 150 Hz and −83-83 dB above 200 Hz, but its wider transition means that just above the cutoff, at 125 Hz, it is still only at −26-26 dB.

Linear phase

A symmetric impulse response, h[n]=h[M−1−n]h[n] = h[M - 1 - n], has frequency response H(eiω)=A(ω)e−iω(M−1)/2H(e^{i\omega}) = A(\omega) e^{-i\omega(M-1)/2} with real AA: every frequency is delayed by the same (M−1)/2(M-1)/2 samples. Waveforms keep their shape, which matters for ECG, audio transients and data communication.

python · try it yourself
import numpy as np

M = 31
n = np.arange(M)
h = 0.2 * np.sinc(0.2 * (n - 15)) * np.hamming(M)
h /= h.sum()
print("symmetric:", np.allclose(h, h[::-1]))
for w in (0.05, 0.1, 0.2):
    H = np.sum(h * np.exp(-1j * w * n))
    print(f"omega {w}: phase / omega = {np.angle(H) / w:.4f} samples of delay (expected -{(M - 1) / 2})")
symmetric: True
omega 0.05: phase / omega = -15.0000 samples of delay (expected -15.0)
omega 0.1: phase / omega = -15.0000 samples of delay (expected -15.0)
omega 0.2: phase / omega = -15.0000 samples of delay (expected -15.0)

Designing other types

  • High-pass: spectral inversion, δ[n−K]−hLP[n]\delta[n - K] - h_\text{LP}[n].
  • Band-pass: difference of two low-passes, or a low-pass modulated by cos⁡(2πf0n/fs)\cos(2\pi f_0 n/f_s).
  • Optimal designs: the Parks-McClellan (Remez) algorithm minimizes the maximum error, giving equiripple filters that need fewer taps than windowed sincs for the same spec.

In software

  • scipy.signal.firwin(numtaps, cutoff, window=..., fs=...) implements the windowed-sinc method; remez and firls implement optimal designs; freqz checks the result.
  • A rule of thumb for the length with a Hamming window: M≈3.3fs/ΔfM \approx 3.3 f_s / \Delta f taps for a transition width Δf\Delta f.

Exercises

Exercise 1: Estimate the length

You need a Hamming-windowed low-pass with a 50 Hz transition band at fs=8000f_s = 8000 Hz. About how many taps?

Answer

About 530.

Solution

M≈3.3⋅8000/50=528M \approx 3.3 \cdot 8000 / 50 = 528; round to an odd number such as 529. Sharp transitions at high sampling rates need long FIR filters, which is where FFT convolution or IIR designs help.

Exercise 2: Delay of a linear-phase FIR

What is the delay of a 101-tap linear-phase FIR at 48 kHz?

Answer

50 samples, about 1.04 ms.

Solution

(M−1)/2=50(M-1)/2 = 50 samples, 50/48000≈1.0450/48000 \approx 1.04 ms.

Exercise 3: A high-pass by inversion (code)

Build a 61-tap high-pass with cutoff 100 Hz at fs=1000f_s = 1000 Hz by spectral inversion of the Hamming low-pass, and print its gain at 0, 50, 200 and 400 Hz.

Answer

About 0 at 0 and 50 Hz, about 1 at 200 and 400 Hz.

Solution
python · try it yourself
import numpy as np
fs, fc, M = 1000.0, 100.0, 61
n = np.arange(M)
lp = 2 * fc / fs * np.sinc(2 * fc / fs * (n - 30)) * np.hamming(M)
lp /= lp.sum()
hp = -lp
hp[30] += 1                                   # delta at the center minus the low-pass
for f in (0, 50, 200, 400):
    H = np.sum(hp * np.exp(-2j * np.pi * f / fs * n))
    print(f, round(abs(H), 4))
0 0.0
50 0.0039
200 0.9992
400 0.9997

Test yourself

Why are FIR filters always stable?

  1. they use feedback
  2. their impulse response is finite, so it is absolutely summable
  3. they are causal
  4. they have linear phase

A symmetric FIR impulse response gives…

  1. minimum phase
  2. linear phase (constant delay)
  3. zero gain at DC
  4. an unstable filter

In the windowed-sinc method, the window mainly controls…

  1. the cutoff frequency
  2. stopband attenuation and transition width
  3. the sampling rate
  4. the filter's delay only