packages

interpolate::smooth::savgol

Savitzky-Golay filters (1964): each output is the value (or a derivative) at the window's centre of the least-squares polynomial through the window, which is a fixed correlation with weights from the pseudo-inverse of the window's Vandermonde matrix.

Enums

EdgeSource

pub enum Edge

How savgol_filter treats the first and last window / 2 samples.

nabi
use interpolate::Edge;

let y = try interpolate::savgol_filter([1, 2, 4, 7, 11, 16, 22], 5, 2, mode: Edge::Interp);
assert((y[0] - 1.0).abs() < 1e-12);       // the edge fit is the quadratic itself
  • Interp§

    Source

    Fit the polynomial to the first (last) whole window and evaluate it there (the default).

  • Mirror§

    Source

    Reflect about the end sample, without repeating it: y[2] y[1] | y[0] y[1] ....

  • Nearest§

    Source

    Repeat the end sample.

  • Constant(f64)§

    Source

    Pad with this value.

  • Wrap around: the signal is periodic.

Functions

savgol_coeffsSource

pub fn savgol_coeffs(window: usize, polyorder: usize, deriv: usize = 0, delta: f64 = 1.0) throws (InterpError) -> Vector<f64>

The correlation weights of a Savitzky-Golay filter: output i is sum_j c[j] y[i + j - window / 2], the deriv-th derivative of the degree-polyorder least-squares fit over the window, for samples delta apart. window must be odd and above polyorder; a deriv above polyorder gives zeros.

nabi
let c = try interpolate::savgol_coeffs(5, 2);
assert((c[0] * 35.0 + 3.0).abs() < 1e-12 && (c[2] * 35.0 - 17.0).abs() < 1e-12);

savgol_filterSource

pub fn savgol_filter<Y: AsVector<f64>>(y: Y, window: usize, polyorder: usize, deriv: usize = 0, delta: f64 = 1.0, mode: Edge = Edge::Interp) throws (InterpError) -> Vector<f64>

y filtered by a Savitzky-Golay filter (see savgol_coeffs), the ends handled by mode. A polynomial of degree up to polyorder passes unchanged (its derivative, with deriv). O(n window). Interp needs window <= y.len(), the others more than window / 2 samples.

nabi
use interpolate::Edge;

let y = try interpolate::savgol_filter([0, 1, 4, 9, 16, 25], 3, 2);
assert((y[3] - 9.0).abs() < 1e-12);
let d = try interpolate::savgol_filter([0, 1, 4, 9, 16, 25], 5, 2, deriv: 1, mode: Edge::Nearest);
assert((d[2] - 4.0).abs() < 1e-12);