Coder Social home page Coder Social logo

mathiswellmann / whittaker_smoother Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 0.0 3.1 MB

A perfect smoother; A discrete time version of spline smoothing for equally spaced data

License: GNU Affero General Public License v3.0

Rust 90.97% Nix 9.03%
derivative filter linear-algebra lu rust signal-processing smoother

whittaker_smoother's Introduction

Whittaker Smoother

Aka Whittaker-Henderson, Whittaker-Eilers Smoother is known as the perfect smoother. Its a discrete-time version of spline smoothing for equally spaced data. It minimizes the functional

$$\sum_{i=0}^n (z_i - y_i)^2 + \lambda \sum_{i=0}^n (\delta ^p z)_i ^2 $$

where y are the datapoints, z is the smoothed function, and $\delta^2 z$ is the pth derivative of $z_i$, which is evaluated numerically. A penalty is imposed on nonsmooth functions, with higher values of $\lambda$ increasing the penalty and leading to a smoother output.

The smoothed output can be obtained by solving the linear system $$x = (W + \lambda * D^T D )^{-1} W y $$ Where W is the weight matrix (Identity matrix in practice) and D is the difference matrix (See difference_matrix for its construction).

Examples

Here we see the wood dataset smoothed whith both order 2 and 3. wood_2 wood_3

Comparison to Moving Averages and Convolution Kernels

Compared to a moving average smoother, this method does not suffer from a group-delay.

Compared to a convolution kernel such as the savitzky-golay filter, the values at the edge are well defined and don't need to be interpolated. The savitzky-golay filter does have a nice flat passband, but suffers from unsatisfactory high-frequency noise, which is not sufficiently suppressed. This is a particular problem when the derivative of the data is of importance.

However, this smoother takes future values into account and therefore suffers from a look-ahead bias.

Usage

To use this smoother in you project, add this to your Cargo.toml:

[dependencies]
whittaker_smoother = "0.1"

Now you can use the smoothing function as such:

use whittaker_smoother::whittaker_smoother;

// Here we use the WOOD_DATASET, but this can be any series that you would like to smooth
let raw = Vec::from_iter(WOOD_DATASET.iter().map(|v| *v as f64));
let lambda = 2e4;
let order = 3;
let smoothed = whittaker_smoother(&raw, lambda, order).unwrap();

And BAM, that's it! There is you perfectly smoothed series.

Further Reading:

See the papers folder for two papers showing additional details of the method.

This implementation was inspired by A python implementation.

Potential Upgrades:

  • Add benchmarks
  • Use sparse matrices if available
  • Add function for computing the optimal lambda based on cross-validation (See eilers2003)

License

Copyright (C) 2020 <Mathis Wellmann [email protected]>

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.

GNU AGPLv3

whittaker_smoother's People

Contributors

mathiswellmann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.