scipy.signal designs digital filters (lowpass, highpass, bandpass) and applies them with filtfilt for zero-phase smoothing of sensor data.
Common workflow
- Design filter with
butter,cheby1, etc. - Get coefficients
b, aor SOS form - Apply
sosfiltfiltfor forward-backward filtering
Moving average alternative
Simple np.convolve or signal.savgol_filter for denoising without full IIR design—pick based on latency and phase requirements.
Butterworth lowpass sketch
import numpy as np
from scipy import signal
b, a = signal.butter(4, 0.1, btype='low')
x = np.random.default_rng(0).normal(size=100)
y = signal.filtfilt(b, a, x)
print('input std:', x.std(), 'filtered std:', y.std())
Important interview questions and answers
- Q: filtfilt vs lfilter?
A: filtfilt runs forward and backward for zero phase delay—better for offline analysis. - Q: Cutoff normalized?
A: For butter, Wn is fraction of Nyquist (0–1), not Hz—scale by sample rate.
Self-check
- What does a lowpass filter remove?
- Why normalize cutoff to Nyquist?
Tip: filtfilt is for offline analysis; real-time streams often use causal lfilter.
Interview prep
- filtfilt?
Forward-backward filter—zero phase for offline data.
- Cutoff units?
Normalized to Nyquist in butter—multiply by fs/2 for Hz.