Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ml-savitzky-golay-generalized

mljs10.2kMIT4.2.0TypeScript support: included

Savitzky–Golay filter in Javascript

signal, filtering, transformation, savitzky, Golay, derivative

readme

savitzky-golay-generalized

General Least-Squares Smoothing and Differentiation by the Convolution (Savitzky-Golay) Method Peter A. Gorry

Pretty much the same as the savitzky-golay method, but without border problems, and without inventing points. It can be maybe merged into the savitzky-golay project but by the now this is the version used in the last GSD project.

I'll try an automatic parameter tunning based on the SNR or in the entropy of the signal.

Usage

npm i ml-savitzky-golay-generalized
const {sgg} = require("ml-savitzky-golay-generalized");
sgg(dataY, deltaX|X, options)

Parameters

dataY

The data to be filtered.

deltaX | X

deltaX specifies the difference between 2 consecutive points of the independent: deltaX = X[i+1] - X[i]. Specficiying a deltaX suppose that all your points are equally spaced on the independent variable. If your points are not equally spaced in the ordinate variable, then you have to provide explicitly your X values. The algorithm will use the average deltaX within each bin of 'windowSize' points to approximate the derivatives. This fast approximation only works if the X is almost locally equally spaced.

options

windowSize:

The odd number of points to approximate the regression polynomial. Default 9

derivative:

The grade of the derivative. 0 by default (Smoothing)

polynomial:

The order of the regression polynomial. Default 3

Examples

const { sgg } = require('ml-savitzky-golay-generalized');

Smoothing example

const options = {
  windowSize: 15,
  derivative: 0,
  polynomial: 3,
};

const noiseLevel = 0.1;
const data = new Array(200);
for (let i = 0; i < data.length; i++)
  data[i] =
    Math.sin((i * Math.PI * 2) / data.length) +
    (Math.random() - 0.5) * noiseLevel;
const answer = sgg(data, (Math.PI * 2) / data.length, options);
console.log(answer);

First derivative test (Equally spaced x)

const options = {
  windowSize: 45,
  derivative: 1,
  polynomial: 3,
};

const noiseLevel = 0.1;
const data = new Array(200);
for (let i = 0; i < data.length; i++) {
  data[i] =
    Math.sin((i * Math.PI * 2) / data.length) +
    (Math.random() - 0.5) * noiseLevel;
}
const answer = sgg(data, (Math.PI * 2) / data.length, options);
console.log(answer);

First derivative test x as vector(It could be non-equally spaced!!)

const options = {
  windowSize: 47,
  derivative: 1,
  polynomial: 3,
};

const noiseLevel = 0.1;
const data = new Array(200);
const x = new Array(200);
for (let i = 0; i < data.length; i++) {
  data[i] =
    Math.sin((i * Math.PI * 2) / data.length) +
    (Math.random() - 0.5) * noiseLevel;
  x[i] = (i * Math.PI * 2) / data.length;
}

const ans = sgg(data, (Math.PI * 2) / data.length, options);
const ans2 = sgg(data, x, options);

changelog

Changelog

4.2.0 (2024-04-17)

Features

4.1.1 (2024-04-15)

Bug Fixes

  • dummy commit to increase version (53d3d63)

4.1.0 (2024-04-15)

Features

  • allow NumberArray rather that DoubleArray (078a0f9)

4.0.1 (2022-02-25)

Bug Fixes

4.0.0 (2022-02-09)

⚠ BREAKING CHANGES

  • export sgg and SGGOptions
  • no default export

Features

3.0.0 (2022-02-05)

⚠ BREAKING CHANGES

  • use Float64Array internally and as return value
  • remove test support for Node.js 8 and 10, add support for Node.js 14 and 16

Features

  • use Float64Array internally and as return value (4d40428)

Bug Fixes

Miscellaneous Chores

  • remove test support for Node.js 8 and 10, add support for Node.js 14 and 16 (4d297fb)

2.0.3 (2020-11-19)

Bug Fixes

2.0.2 (2020-04-13)

Bug Fixes

  • fix calculation of right-hand-side border (06efae8)

2.0.1 (2020-02-28)

Bug Fixes

2.0.0 (2020-02-26)

Bug Fixes

1.1.1 (2017-07-24)

1.0.4 (2017-07-24)

Bug Fixes

  • use range for ml-stat dependency (aa9ecd1)