Skip to content

Designing a FIR filter

A FIR filter is a list of taps: each output is their weighted sum of the last inputs. More taps give a sharper edge between what passes and what is stopped, at the cost of time. dspk.ops.filter.firwin designs them (a windowed sinc, as scipy’s firwin); dspk.ops.filter.fir runs them on the device (centered=True: no delay).

import numpy as np
import dspk
from plating import signals
s = signals.synthetic()
CUTOFF = 1_000.0
designs = {n: dspk.ops.filter.firwin(n, CUTOFF, sample_rate=s.sample_rate) for n in (31, 101, 301)}
# Frequency response of each design (the Fourier transform of its taps)
nfft = 1 << 16
freqs = np.fft.rfftfreq(nfft, 1 / s.sample_rate)
responses = {n: np.abs(np.fft.rfft(taps, nfft)) for n, taps in designs.items()}
for n, r in responses.items():
print(f"{n:4d} taps: gain at {2 * CUTOFF:.0f} Hz {20 * np.log10(r[np.searchsorted(freqs, 2 * CUTOFF)]):6.1f} dB")
# Run the 301-tap design centred (odd length: no delay)
out = dspk.ops.filter.fir(s.data, designs[301], centered=True)

Designing a FIR filter

The whole script is playground/02_filtering/04_fir_design.py. Run it with uv run python playground/02_filtering/04_fir_design.py.