packages

signal::filter::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").

Functions

lfilterSource

pub fn lfilter(b: some RealSamples, a: some RealSamples, x: some RealSamples) throws (SignalError) -> Vector<f64>

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). Throws when a has a pole outside the unit circle.

nabi
use core::array::Vector;

let x: Vector<f64> = [1, 0, 0, 0];
let b: Vector<f64> = [1];
let a: Vector<f64> = [1, -0.5];
println(try signal::filter::lfilter(b, a, x)); // [1.0, 0.5, 0.25, 0.125]

lfilter_stateSource

pub fn lfilter_state(b: some RealSamples, a: some RealSamples, x: some RealSamples, zi: Vector<f64>) throws (SignalError) -> (Vector<f64>, Vector<f64>)

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.

nabi
use core::array::Vector;

let b: Vector<f64> = [0.5, 0.5];
let a: Vector<f64> = [1];
let x1: Vector<f64> = [1, 2];
let x2: Vector<f64> = [3, 4];
let zi: Vector<f64> = [0];
let (y1, z) = try signal::filter::lfilter_state(b, a, x1, zi);
let (y2, _) = try signal::filter::lfilter_state(b, a, x2, z);
println(f"{y1} {y2}"); // [0.5, 1.5] [2.5, 3.5]

lfilter_ziSource

pub fn lfilter_zi(b: some RealSamples, a: some RealSamples) throws (SignalError) -> Vector<f64>

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.

nabi
use core::array::Vector;

let b: Vector<f64> = [1];
let a: Vector<f64> = [1, -0.5];
println(try signal::filter::lfilter_zi(b, a)); // [1.0]

sosfiltSource

pub fn sosfilt(sos: Matrix<f64>, x: some RealSamples) throws (SignalError) -> Vector<f64>

x filtered by the cascade of second-order sections sos (rows [b0, b1, b2, a0, a1, a2]), from rest. The way to run an IIR filter of any order without losing accuracy.

nabi
use signal::filter::{Band, butter, sosfilt};
use core::array::{Array, Vector};

let sos = try butter(4, Band::Lowpass(0.1));
let step: Vector<f64> = Array::full([200], 1.0);
let y = try sosfilt(sos, step);
println(y[199]); // 1.0 (to 1e-6)

sosfilt_stateSource

pub fn sosfilt_state(sos: Matrix<f64>, x: some RealSamples, zi: Matrix<f64>) throws (SignalError) -> (Vector<f64>, Matrix<f64>)

sosfilt from the state zi (a [n, 2] matrix, see sosfilt_zi); also returns the final state.

nabi
use signal::filter::{Band, butter, sosfilt_state, sosfilt_zi};
use core::array::{Array, Vector};

let sos = try butter(2, Band::Lowpass(0.2));
let x: Vector<f64> = Array::full([5], 3.0);
let zi = try sosfilt_zi(sos);
let (y, _) = try sosfilt_state(sos, x, zi .* 3.0);
println(y[0]); // 3.0 (to rounding): no transient

sosfilt_ziSource

pub fn sosfilt_zi(sos: Matrix<f64>) throws (SignalError) -> Matrix<f64>

The [n, 2] state that starts sosfilt_state in the steady state of a unit step.

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

println(try sosfilt_zi(try butter(4, Band::Lowpass(0.3))).shape()); // [2, 2]

filtfiltSource

pub fn filtfilt(b: some RealSamples, a: some RealSamples, x: some RealSamples, padlen: usize? = none) throws (SignalError) -> Vector<f64>

x filtered forward and then backward: zero phase, the magnitude response squared, and no delay. The ends are extended by padlen odd-reflected samples (default 3 * max(len(a), len(b))) and both passes start from the steady state, so there is no start-up transient.

nabi
use core::array::{Array, Vector};

let b: Vector<f64> = [0.25, 0.5, 0.25];
let a: Vector<f64> = [1];
let x: Vector<f64> = Array::full([20], 2.0);
let y = try signal::filter::filtfilt(b, a, x);
println(y[0]); // 2.0 (to rounding)

sosfiltfiltSource

pub fn sosfiltfilt(sos: Matrix<f64>, x: some RealSamples, padlen: usize? = none) throws (SignalError) -> Vector<f64>

filtfilt for second-order sections: the zero-phase way to run an IIR filter of any order. padlen defaults to SciPy's 3 * (2 n + 1 - min(zeros of b2, zeros of a2)) for n sections.

nabi
use signal::filter::{Band, butter, sosfiltfilt};
use core::array::{Array, Vector};

let sos = try butter(4, Band::Lowpass(0.2));
let x: Vector<f64> = Array::full([50], 1.0);
println(try sosfiltfilt(sos, x)[25]); // 1.0 (to rounding)