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
applyRunning a filter over a signal: transposed direct form II forb/a, a cascade of such sections forsos, and the forward- backward zero-phase forms with odd extension at both ends and steady- state initial conditions (Gustafsson's method as SciPy'sfiltfiltdoes it with method "pad").firFIR 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'sfirwin).iirIIR design the classical way (as SciPy'siirfilter): 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'szpk2sos), which keeps high orders well conditioned where a singleb/awould not be.resampleChanging the sample rate (Fourier method and decimation) and removing trends.responseFrequency 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}lfilterxfiltered byb(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_statelfilterfrom the delay statezi(max(len(a), len(b)) - 1values, seelfilter_zi); also returns the final state, to filter a long signal block by block.lfilter_ziThe state that makeslfilter_statestart in the steady state of a unit step: scale it by the first sample to start without a transient.sosfiltxfiltered by the cascade of second-order sectionssos(rows[b0, b1, b2, a0, a1, a2]), from rest.sosfilt_statesosfiltfrom the statezi(a[n, 2]matrix, seesosfilt_zi); also returns the final state.sosfilt_ziThe[n, 2]state that startssosfilt_statein the steady state of a unit step.filtfiltxfiltered forward and then backward: zero phase, the magnitude response squared, and no delay.sosfiltfiltfiltfiltfor second-order sections: the zero-phase way to run an IIR filter of any order.
fir::firwinSource
pub use fir::firwinfirwinA linear-phase FIR filter ofnumtapscoefficients forband(edges in units offs), windowed bywindow(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}IirThe analog prototype to design from.ZpkA filter as its zeros, poles and gain:H(z) = gain * prod(z - zeros) / prod(z - poles).iirfilterThe digital IIR filter of ordern(2nfor band-pass and band-stop) ofkindforband, edges in units offs, as zeros, poles and gain.butterA Butterworth filter as second-order sections.cheby1A Chebyshev type I filter withrpdB of pass-band ripple, as second-order sections.cheby2A Chebyshev type II filterrsdB down in the stop band, as second-order sections.besselA Bessel filter (phase-normalized, as SciPy's default), as second-order sections: nearly constant group delay in the pass band.sos_to_baOne(b, a)from second-order sections (the product of the rows).
resample::{resample, decimate, detrend, Detrend}Source
pub use resample::{resample, decimate, detrend, Detrend}resamplexresampled tonumsamples by the Fourier method (as SciPy'sresample): the spectrum is cut or zero-padded, splitting or joining the Nyquist bin, and transformed back.decimatexlow-pass filtered and kept everyqth sample (as SciPy'sdecimatewith zero phase): an order-8 Chebyshev I filter (0.05 dB ripple, cutoff0.8 / qof Nyquist) run forward and backward, or withfira Hamming-windowed FIR of20 q + 1taps.detrendxminus its mean (Detrend::Constant) or its least-squares line (Detrend::Linear, the default).DetrendWhatdetrendremoves.
response::{freqz, sosfreqz, Response}Source
pub use response::{freqz, sosfreqz, Response}freqzThe responseH(e^jw) = b(e^jw) / a(e^jw)atnfrequencies from 0 up to (not including) Nyquist, or the whole circle withwhole.sosfreqzfreqzfor second-order sections: the product of the rows' responses.ResponseA frequency response:h[k]is the gain at frequencyw[k](in the units of thefsasked for).
Enums
BandSource
pub enum BandThe 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]