Digital filters
The “Filter” block performs linear frequency-selective filtering of the signals of all four channels. Filters with finite impulse response (FIR) and infinite impulse response (IIR) are available. All channels are processed by a filter with identical coefficients.
Purpose
The filter is used to suppress spectral components of interference (50 Hz mains hum, low-frequency and high-frequency noise) and to isolate the frequency band containing the signal under study.
The filter output is used for level calculation, for the spectrum, waveform and spectrogram (“processed” mode), and for listening to and recording the processed signal. The original signal is kept unchanged. Filter parameters may be changed during a measurement; changes are permitted for the “operator” role.
Basic definitions
- Sampling rate fs and Nyquist frequency fs/2
- A discrete-time signal contains components only in the range 0…fs/2. All filter edge frequencies must lie in this range. Normalized frequency: ω = 2π·f/fs.
- Difference equation and transfer function
- A linear time-invariant discrete-time filter is described by the difference equation a0·y[n] = Σk=0…M bk·x[n−k] − Σk=1…P ak·y[n−k] and the transfer function H(z) = B(z)/A(z) = Σ bkz−k / Σ akz−k. For an FIR filter ak = 0 for k ≥ 1 (non-recursive filter); an IIR filter has feedback (recursive filter).
- Impulse response h[n]
- The filter response to the unit impulse δ[n]. The output signal is the convolution of the input signal with h[n].
- Frequency response, magnitude response, phase response
- H(ejω) is the value of H(z) on the unit circle. The magnitude response is |H(ejω)|, on a logarithmic scale 20·lg|H| dB. The phase response is φ(ω) = arg H(ejω). Level correspondence: −3 dB — 0.707 in amplitude (half power), −6 dB — 0.5, −20 dB — 0.1, −40 dB — 0.01, −60 dB — 0.001.
- Passband, stopband, transition band
- The passband is the frequency range with a gain of about 0 dB; it is characterized by the passband ripple. The stopband is the range of suppressed frequencies; it is characterized by the minimum attenuation. The transition band lies between them; its width Δf determines the selectivity of the filter.
- Cutoff frequency
- The edge frequency of the passband. For a Butterworth filter it is defined at the −3 dB level; for an FIR filter designed by the window method, as the edge frequency of the ideal filter (the magnitude response there is ≈ −6 dB).
- Group delay, linear phase
- Group delay τg(ω) = −dφ/dω. With linear phase τg is constant and the waveform in the passband is not distorted. An FIR filter with a symmetric impulse response has linear phase; the phase response of IIR filters is nonlinear. With the same filter in all channels, the relative time delays between channels are preserved.
- Transient
- The settling interval of the output signal after the start of the input. For an FIR filter it is N − 1 samples. For an IIR filter the system uses the sample index after which |h[n]| < 10−4·max|h[n]| (at most 4096). The value is displayed below the filter parameters.
- Stability
- An IIR filter is stable if all poles of H(z) lie inside the unit circle (|z| < 1). An FIR filter is always stable.
- Filter types by band
- Low-pass filter (LPF), high-pass filter (HPF), band-pass filter (BPF), band-stop (band-reject) filter (BSF).
Fig. 1. Magnitude responses of 4th-order Butterworth filters, fs = 16,276 Hz.
Choosing a filter type
| Task | Recommended filter | Rationale |
|---|---|---|
| Isolating a frequency band (e.g. 80…2000 Hz) | Butterworth BPF, order 2–4 | High selectivity with few coefficients and small delay |
| Suppressing narrow-band interference (50 Hz mains hum) | Biquad notch, Q = 5…10 | Narrow stopband; harmonics are suppressed with the equalizer |
| Suppressing low-frequency components | Butterworth HPF 60…150 Hz, order 2–4 | An FIR filter with a narrow transition band at low frequencies requires a large N |
| Suppressing high-frequency components | Butterworth or FIR LPF | FIR — when linear phase is required |
| Waveform analysis (pulses, transients) | FIR, Hamming or Blackman window | Linear phase, no phase distortion |
| Smoothing slowly varying quantities | Moving average | Minimal computational cost |
| Filter designed in an external package | Custom coefficients | Chebyshev, elliptic, weighting filters, etc. |
FIR filter, window design method
An FIR filter is a non-recursive filter with an impulse response of finite length N:
With a symmetric impulse response h[k] = h[N−1−k] the filter has linear phase; the group delay equals M = (N−1)/2 samples.
Window method. The impulse response of the ideal filter hd[n] has infinite length (sinc function). Its truncation to N samples causes ripple of the magnitude response near the band edges (Gibbs phenomenon). Multiplying hd[n] by a window function w[n] reduces the sidelobe level and increases the transition band width.
HPF: δ[n−M] − hd[n]; BPF: hd,f2[n] − hd,f1[n]; BSF: δ[n−M] − hBPF[n]
h[n] = hd[n]·w[n] / G, G — normalizing factor (|H| = 1 at the passband centre)
Parameters: band type; frequencies f1, f2; length N (odd, 3…511); window function; Kaiser window parameter β.
| Window | Stopband attenuation | Transition band width Δf |
|---|---|---|
| Rectangular | 21 dB | 0.9·fs/N |
| Hann | 44 dB | 3.1·fs/N |
| Hamming | 53 dB | 3.3·fs/N |
| Blackman | 74 dB | 5.5·fs/N |
| Kaiser | A, where β = 0.1102·(A − 8.7) for A > 50 dB | (A − 7.95)/14.36 · fs/N |
Fig. 2. Magnitude response of an FIR LPF, fc = 1 kHz, fs = 16,276 Hz; a) N = 101.
Estimating the filter length. The required length is N ≈ K·fs/Δf, where K is the window coefficient from the table. For example, with fs = 16,276 Hz, Δf = 100 Hz and a Hamming window N ≈ 540, which exceeds the allowed maximum of 511. For narrow transition bands at low frequencies IIR filters are preferable.
Application: tasks requiring linear phase; filtering with wide transition bands. Limitations: large filter length for a narrow transition band; group delay (N−1)/2 and a transient of N−1 samples.
Butterworth filter
The Butterworth filter is an IIR filter with a maximally flat magnitude response in the passband. The squared magnitude response of the analog LPF prototype of order N:
Design. The analog LPF prototype is transformed into an LPF, HPF, BPF or BSF by a frequency transformation, and then into a digital filter by the bilinear transform with prewarping of the edge frequencies. The implementation is a cascade of second-order sections (SOS) in transposed direct form II.
s = 2fs·(1 − z−1)/(1 + z−1) (bilinear transform)
H(z) = Πi (b0i + b1iz−1 + b2iz−2) / (1 + a1iz−1 + a2iz−2)
section: y = b0x + z1; z1 ← b1x − a1y + z2; z2 ← b2x − a2y
Properties: at the cutoff frequency the magnitude response is −3 dB; the asymptotic roll-off is 20·N dB/decade (6·N dB/octave). For BPF and BSF the order of the digital filter is 2N. Parameters: band type; f1, f2; order N = 1…8.
Fig. 3. Magnitude response of a Butterworth LPF, fc = 1 kHz, fs = 16,276 Hz, orders 2 and 8. At 2 kHz: −13 dB and −51 dB.
Application: isolating and suppressing frequency bands, including at low frequencies; tasks with constraints on delay and computational cost. Limitations: nonlinear phase; the group delay is maximal near the cutoff frequency; with increasing order and decreasing bandwidth the transient duration and the oscillation of the impulse response increase.
Biquad section
A biquad section is a second-order IIR filter. The coefficients are calculated by the formulas of R. Bristow-Johnson (Audio EQ Cookbook) from the centre frequency f0, the quality factor Q and, for the peaking filter, the gain G.
notch: b = [1, −2cos ω0, 1], a = [1+α, −2cos ω0, 1−α]
BPF (0 dB gain at f0): b = [α, 0, −α], a = [1+α, −2cos ω0, 1−α]
peaking: b = [1+αA, −2cos ω0, 1−αA], a = [1+α/A, −2cos ω0, 1−α/A]
LPF: b = [(1−cos ω0)/2, 1−cos ω0, (1−cos ω0)/2]; HPF: b = [(1+cos ω0)/2, −(1+cos ω0), (1+cos ω0)/2]; a — as for the notch
Quality factor. For the notch and BPF the −3 dB bandwidth is Δf ≈ f0/Q. The time constant of the transient envelope is τ ≈ Q/(π·f0); for example, with f0 = 50 Hz and Q = 10, τ ≈ 64 ms. For LPF and HPF the value Q = 1/√2 ≈ 0.707 corresponds to a second-order Butterworth filter.
Fig. 4. Magnitude response of a notch filter, f0 = 50 Hz, Q = 2 and Q = 10.
Application: suppressing narrow-band interference (notch); magnitude response correction in a frequency band (peaking filter); second-order LPF and HPF. Limitations: one section suppresses one frequency (the equalizer is used for harmonics); with a high quality factor at a low frequency the transient duration and the sensitivity to deviation of the interference frequency from f0 increase.
Moving average filter
An FIR filter of length L with equal coefficients 1/L. Its magnitude response is described by the Dirichlet kernel:
The zeros of the magnitude response are located at k·fs/L; the first sidelobe level is −13.3 dB; the group delay is (L−1)/2 samples.
Fig. 5. Magnitude response of a moving average filter, L = 8 and L = 32, fs = 16,276 Hz.
Application: smoothing slowly varying quantities; suppressing components at multiples of fs/L. Limitations: low frequency selectivity (high sidelobe level).
Custom coefficients
This mode allows a filter designed in an external package (SciPy, MATLAB, GNU Octave) to be used. Two forms are supported:
- Direct form (b, a): numerator coefficients b0…bM (up to 511) and denominator coefficients a0…aP (up to 21). With an empty denominator the filter is an FIR filter.
- Cascade form (SOS): one line per section in the format “b0 b1 b2 a0 a1 a2” (up to 16 sections). The format matches
the output of
scipy.signalwithoutput="sos".
The coefficients are normalized by a0. Stability is checked from the roots of the denominator; an unstable filter is rejected. For high-order IIR filters the cascade form is recommended: it is less sensitive to coefficient quantization errors.
from scipy.signal import butter
for row in butter(4, [100, 1000], btype="bandpass", fs=16276, output="sos"): print(*row)
The coefficients define the response in normalized frequency. When fs changes, all frequencies of the response scale in proportion to fs; the coefficients should be calculated for the operating sampling rate.
Analysis tools
- Filter frequency response
- Magnitude response of the resulting chain (filter and equalizer) on a logarithmic frequency scale.
- Impulse response
- The first 256 samples of h[n]; for an FIR filter it coincides with its set of coefficients.
- Formulas and coefficient calculation
- Design formulas of the current filter with the parameter values substituted.
- Coefficients of the current filter
- Coefficients in text form (12 significant digits). The “Edit manually” command transfers them to the “Custom coefficients” mode.
- Spectrogram
- Displays the filter edge frequencies. A click sets f1, a click with the Shift key sets f2.
- Spectrum, waveform, listening
- Comparison of the signal before and after processing.
Limitations
- Frequency filtering does not separate signals with overlapping spectra.
- Edge frequencies must be below fs/2. A capture configuration at which the current filter cannot be realized is rejected; when recording the original signal at such fs the filter is not applied.
- Noise in the passband is not suppressed. For white noise the output power of the filter is proportional to the passband width. The self-noise of the INMP441 microphones is about −80 dBFS over the whole band.
- Gain (peaking filter) also increases the noise level and may cause signal clipping during listening.
- Increasing the order and quality factor increases the delay and transient duration; the minimum order providing the required suppression should be used.