savitzky_golay (pyleoclim.utils.filter.savitzky_golay)

pyleoclim.utils.filter.savitzky_golay(ys, window_length=None, polyorder=2, deriv=0, delta=1, axis=- 1, mode='interp', cval=0)[source]

Smooth (and optionally differentiate) data with a Savitzky-Golay filter.

The Savitzky-Golay filter removes high frequency noise from data. It has the advantage of preserving the original shape and features of the signal better than other types of filtering approaches, such as moving averages techniques.

The Savitzky-Golay is a type of low-pass filter, particularly suited for smoothing noisy data. The main idea behind this approach is to make for each point a least-square fit with a polynomial of high order over a odd-sized window centered at the point.

Uses the implementation from scipy.signal: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.savgol_filter.html

Parameters
  • ys (array) – the values of the signal to be filtered

  • window_length (int) –

    The length of the filter window. Must be a positive integer.

    If mode is ‘interp’, window_length must be less than or equal to the size of ys. Default is the size of ys.

  • polyorder (int) –

    The order of the polynomial used to fit the samples. polyorder Must be less than window_length.

    Default is 2

  • deriv (int) –

    The order of the derivative to compute.

    This must be a nonnegative integer. The default is 0, which means to filter the data without differentiating

  • delta (float) –

    The spacing of the samples to which the filter will be applied.

    This is only used if deriv>0. Default is 1.0

  • axis (int) – The axis of the array ys along which the filter will be applied. Default is -1

  • mode (str) – Must be ‘mirror’, ‘constant’, ‘nearest’, ‘wrap’ or ‘interp’. This determines the type of extension to use for the padded signal to which the filter is applied. When mode is ‘constant’, the padding value is given by cval. See the Notes for more details on ‘mirror’, ‘constant’, ‘wrap’, and ‘nearest’. When the ‘interp’ mode is selected (the default), no extension is used. Instead, a degree polyorder polynomial is fit to the last window_length values of the edges, and this polynomial is used to evaluate the last window_length // 2 output values.

  • cval (scalar) – Value to fill past the edges of the input if mode is ‘constant’. Default is 0.0.

Returns

yf – ndarray of shape (N), the smoothed signal (or it’s n-th derivative).

Return type

array

References

  1. Savitzky, M. J. E. Golay, Smoothing and Differentiation of

    Data by Simplified Least Squares Procedures. Analytical Chemistry, 1964, 36 (8), pp 1627-1639.

Numerical Recipes 3rd Edition: The Art of Scientific Computing

W.H. Press, S.A. Teukolsky, W.T. Vetterling, B.P. Flannery Cambridge University Press ISBN-13: 9780521880688

SciPy Cookbook: shttps://github.com/scipy/scipy-cookbook/blob/master/ipython/SavitzkyGolay.ipynb

Notes

Details on the mode option:

  • ‘mirror’: Repeats the values at the edges in reverse order. The value closest to the edge is not included.

  • ‘nearest’: The extension contains the nearest input value.

  • ‘constant’: The extension contains the value given by the cval argument.

  • ‘wrap’: The extension contains the values from the other end of the array.