spectrochempy.savgol

savgol(dataset, size=5, order=2, dim=-1, delta=None, **kwargs)[source]

Savitzky-Golay filter.

Wrapper of scpy.signal.savgol(). See the documentation of this function for more details.

Parameters:
  • dataset (NDDataset) – Input dataset to filter.

  • size (int, optional, default: 5) – The size of the smoothing window.

  • order (int, optional, default: 2) – The order of the polynomial used to fit the data. order must be less than size.

  • dim (int or str, optional, default: -1) – Axis along which to apply the filter. Accepts a dimension name (e.g. "x") or an integer index (e.g. -1 for the last axis).

  • delta (float or None, optional, default: None) – Sample spacing passed to scipy.signal.savgol_filter.

    • None (default) — when deriv > 0, the signed spacing is automatically derived from the coordinate of the processed axis if the coordinate is uniformly spaced. On a non-uniform or missing coordinate a warning is emitted and the index-based delta=1.0 is used as a fallback.

    • A numeric value — passed directly to SciPy with its sign. The value is interpreted in the current unit of the selected coordinate. No unit-based correction (_reversed) is applied. For a descending coordinate, supply a negative delta if the derivative should follow the physical axis.

    Changed in version 0.12.5: Default changed from 1.0 to None (auto-detect).

    Changed in version 0.12.5: Explicit delta is now passed to SciPy with its sign. The former _reversed unit-based correction is no longer applied when delta is explicitly provided.

  • **kwargs (keyword arguments, optional) – Additional keyword arguments passed to the filter.

Returns:

NDDataset – Smoothed data.

Other Parameters:
  • deriv (int, optional, default: 0) – The order of the derivative to compute.

  • mode (str, optional, default: ‘nearest’) – The mode parameter determines how the array borders are handled.

  • cval (float, optional, default: 0.0) – Value to fill past edges of input if mode is ‘constant’.

  • log_level (str, optional, default: ‘WARNING’) – The log level for the filter.

See also

Filter

Filter processing.

Notes

When delta is None (the default), the sample spacing is detected from the coordinate and passed directly to scipy.signal.savgol_filter. The Savitzky-Golay algorithm is fundamentally index-based; the detected delta scales the derivative coefficients during the convolution.

When delta is a numeric value, it is passed to SciPy exactly as provided, with its sign. The coordinate units (cm⁻¹, ppm, etc.) have no effect on the result in this case. The user is responsible for the sign convention.

Units. For deriv > 0 with a physically scaled delta (auto-detected from a uniform coordinate or explicitly provided when the coordinate carries units), the output units are source_units / coordinate_units**deriv. For example, a first derivative of absorbance with respect to cm⁻¹ yields absorbance·cm. Smoothing (deriv=0) and index-based fallbacks preserve the source units unchanged. If the source has no units, the result has no units regardless of the coordinate.

Changed in version 0.12.5: Units are now propagated for physically scaled derivative paths. Smoothing and index fallbacks keep source units.

Examples using spectrochempy.savgol

Savitzky-Golay derivatives on a synthetic spectrum

Savitzky-Golay derivatives on a synthetic spectrum