packages

signal::filter

On this page

Digital filters: FIR and IIR design, filtering (causal and zero-phase), frequency response, resampling and detrending. Filter coefficients follow SciPy: b / a highest power of z^-1 last (b[0] + b[1] z^-1 + ..), second-order sections as rows [b0, b1, b2, a0, a1, a2] of a matrix.

Modules

  • apply Running a filter over a signal: transposed direct form II for b / a, a cascade of such sections for sos, and the forward- backward zero-phase forms with odd extension at both ends and steady- state initial conditions (Gustafsson's method as SciPy's filtfilt does it with method "pad").
  • fir FIR design by the window method: the ideal (sinc) impulse response of the band, times a window, scaled to unit gain at the centre of the first pass band (as SciPy's firwin).
  • iir IIR design the classical way (as SciPy's iirfilter): an analog low-pass prototype's zeros, poles and gain; a frequency transform to the band; the bilinear transform with pre-warped edges; then pairing into second-order sections, poles nearest the unit circle last (the pairing of SciPy's zpk2sos), which keeps high orders well conditioned where a single b / a would not be.
  • resample Changing the sample rate (Fourier method and decimation) and removing trends.
  • response Frequency response of a filter on the unit circle, by Horner's rule at each frequency: O(points * taps), exact to rounding.

Re-exports

apply::{lfilter, lfilter_state, lfilter_zi, sosfilt, sosfilt_state, sosfilt_zi, filtfilt, sosfiltfilt}Source

pub use apply::{lfilter, lfilter_state, lfilter_zi, sosfilt, sosfilt_state, sosfilt_zi, filtfilt, sosfiltfilt}
  • lfilter x filtered by b(z) / a(z) from rest: a[0] y[t] = sum b[k] x[t - k] - sum_(k >= 1) a[k] y[t - k] (transposed direct form II).
  • lfilter_state lfilter from the delay state zi (max(len(a), len(b)) - 1 values, see lfilter_zi); also returns the final state, to filter a long signal block by block.
  • lfilter_zi The state that makes lfilter_state start in the steady state of a unit step: scale it by the first sample to start without a transient.
  • sosfilt x filtered by the cascade of second-order sections sos (rows [b0, b1, b2, a0, a1, a2]), from rest.
  • sosfilt_state sosfilt from the state zi (a [n, 2] matrix, see sosfilt_zi); also returns the final state.
  • sosfilt_zi The [n, 2] state that starts sosfilt_state in the steady state of a unit step.
  • filtfilt x filtered forward and then backward: zero phase, the magnitude response squared, and no delay.
  • sosfiltfilt filtfilt for second-order sections: the zero-phase way to run an IIR filter of any order.

fir::firwinSource

pub use fir::firwin
  • firwin A linear-phase FIR filter of numtaps coefficients for band (edges in units of fs), windowed by window (Hamming by default).

iir::{Iir, Zpk, iirfilter, butter, cheby1, cheby2, bessel, sos_to_ba}Source

pub use iir::{Iir, Zpk, iirfilter, butter, cheby1, cheby2, bessel, sos_to_ba}
  • Iir The analog prototype to design from.
  • Zpk A filter as its zeros, poles and gain: H(z) = gain * prod(z - zeros) / prod(z - poles).
  • iirfilter The digital IIR filter of order n (2n for band-pass and band-stop) of kind for band, edges in units of fs, as zeros, poles and gain.
  • butter A Butterworth filter as second-order sections.
  • cheby1 A Chebyshev type I filter with rp dB of pass-band ripple, as second-order sections.
  • cheby2 A Chebyshev type II filter rs dB down in the stop band, as second-order sections.
  • bessel A Bessel filter (phase-normalized, as SciPy's default), as second-order sections: nearly constant group delay in the pass band.
  • sos_to_ba One (b, a) from second-order sections (the product of the rows).

resample::{resample, decimate, detrend, Detrend}Source

pub use resample::{resample, decimate, detrend, Detrend}
  • resample x resampled to num samples by the Fourier method (as SciPy's resample): the spectrum is cut or zero-padded, splitting or joining the Nyquist bin, and transformed back.
  • decimate x low-pass filtered and kept every qth sample (as SciPy's decimate with zero phase): an order-8 Chebyshev I filter (0.05 dB ripple, cutoff 0.8 / q of Nyquist) run forward and backward, or with fir a Hamming-windowed FIR of 20 q + 1 taps.
  • detrend x minus its mean (Detrend::Constant) or its least-squares line (Detrend::Linear, the default).
  • Detrend What detrend removes.

response::{freqz, sosfreqz, Response}Source

pub use response::{freqz, sosfreqz, Response}
  • freqz The response H(e^jw) = b(e^jw) / a(e^jw) at n frequencies from 0 up to (not including) Nyquist, or the whole circle with whole.
  • sosfreqz freqz for second-order sections: the product of the rows' responses.
  • Response A frequency response: h[k] is the gain at frequency w[k] (in the units of the fs asked for).

Enums

BandSource

pub enum Band

The pass band of a filter, edges in the units of the sampling rate fs the design function takes (with the default fs = 2.0 an edge of 1 is the Nyquist frequency, as SciPy). Every edge lies strictly between 0 and fs / 2, and a band's low edge is below its high one.

nabi
use signal::filter::{Band, butter};

let sos = try butter(4, Band::Lowpass(100.0), fs: 1000.0);
println(sos.shape()); // [2, 6]