spectrochempy.savgol_filter

savgol_filter(dataset, window_length=5, polyorder=0, deriv=0, delta=1.0, mode='interp', cval=0.0, **kwargs)[source]

Apply a Savitzky-Golay filter to a NDDataset.

Wrapper of scpy.signal.savgol(). If dataset has dimension greater than 1, dim determines the axis along which the filter is applied.

Parameters
  • dataset (|NDDataset|) – The dataset to be filtered. If dataset.data is not a single or double precision floating point array, it will be converted to type numpy.float64 before filtering.

  • window_length (int) – The length of the filter window (i.e. the number of coefficients). window_length must be a positive odd integer.

  • polyorder (int) – The order of the polynomial used to fit the NDDataset. polyorder must be less than window_length.

  • deriv (int, optional) – The order of the derivative to compute. This must be a non-negative integer. The default is 0, which means to filter the data without differentiating.

  • delta (float, optional) – The spacing of the samples to which the filter will be applied. This is only used if deriv > 0. Default is 1.0.

  • mode (str, optional) – 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 :py:scipy.signal.savgol_filter: 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, optional) – Value to fill past the edges of the input if mode is ‘constant’. Default is 0.0.

  • **kwargs – Optional keyword parameters (see Other Parameters).

Returns

NDDataset (same shape as x. data units are removed when deriv > 1) – The filtered data.

Other Parameters
  • dim (str or int, optional, default=’x’.) – Specify on which dimension to apply this method. If dim is specified as an integer it is equivalent to the usual axis numpy parameter.

  • inplace (bool, optional, default=False.) – True if we make the transform inplace. If False, the function return a new object.

Notes

Even spacing of the axis coordinates is NOT checked. Be aware that Savitzky-Golay algorithm is based on indexes, not on coordinates.

Details on the mode options:

  • 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.

For example, if the input is [1, 2, 3, 4, 5, 6, 7, 8], and window_length is 7, the following shows the extended data for the various mode options (assuming cval is 0)

*============+=========+========================+=========+ | ‘mirror’ | 4 3 2 | 1 2 3 4 5 6 7 8 | 7 6 5 | | ‘nearest’ | 1 1 1 | 1 2 3 4 5 6 7 8 | 8 8 8 | | ‘constant’ | 0 0 0 | 1 2 3 4 5 6 7 8 | 0 0 0 | | ‘wrap’ | 6 7 8 | 1 2 3 4 5 6 7 8 | 1 2 3 | +————+———+————————+———+

See also

smooth

Smooth the data using a window with requested size.

Examples

>>> dataset = scp.read('irdata/nh4y-activation.spg')
>>> dataset.savgol_filter(window_length=5, polyorder=0)
NDDataset: [float64] a.u. (shape: (y:55, x:5549))