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.ordermust be less than size.dim (
intorstr, optional, default: -1) – Axis along which to apply the filter. Accepts a dimension name (e.g."x") or an integer index (e.g.-1for the last axis).delta (
floatorNone, optional, default:None) – Sample spacing passed toscipy.signal.savgol_filter.None(default) — whenderiv > 0, the signed spacing is automatically derived from the retained samples of the processed axis if they are uniformly spaced. Otherwise, the original coordinate before masked samples were removed is used if it is uniform. If neither coordinate is uniform or no coordinate is available, a warning is emitted and the index-baseddelta=1.0is 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 negativedeltaif the derivative should follow the physical axis.
Changed in version 0.12.5: Default changed from
1.0toNone(auto-detect).Changed in version 0.12.5: Explicit
deltais now passed to SciPy with its sign. The former_reversedunit-based correction is no longer applied whendeltais 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
FilterFilter processing.
differentiateDiscoverable interface for Savitzky-Golay derivatives.
Notes
Smoothing and derivative outputs preserve the source dataset name and retain its history, appending one structured
savgolentry per call. This also applies tosavgol_filter,differentiate, andFilter.transform. Derivative titles identify the derivative order, and units follow the scaling rules below.When
deltaisNone(the default), the sample spacing is detected from the coordinate and passed directly toscipy.signal.savgol_filter. The Savitzky-Golay algorithm is fundamentally index-based; the detected delta scales the derivative coefficients during the convolution.Masked data. Pass
mask_policy="split"to filter contiguous unmasked segments separately and use the original coordinate for automatic spacing. Inmode="interp", every unmasked segment must contain at leastsizepoints.By default, masked rows and columns are removed before filtering, and the input mask is restored afterwards. Automatic spacing detection first uses the retained samples if they are uniformly spaced (including regular subsampling). Otherwise it uses the original coordinate if that is uniform, so a masked block does not force index-based scaling. If neither coordinate is uniform, the index-based fallback is used.
Using the original spacing assumes that masking creates gaps without changing the sampling step elsewhere. Masks combining regular subsampling with a masked block can therefore give incorrectly scaled derivatives even far from the gaps, without a warning. In that case, set
deltaexplicitly to the signed spacing of the retained samples outside the gaps (for example, twice the original step when every other sample is removed). This corrects the scaling but not the effects of crossing gaps.With the default mask policy, removing a masked block joins its two sides in the filtered series. Windows spanning this gap can therefore give unreliable values at unmasked points near the gap, even with physical units and spacing. Interior convolution windows can affect up to
size // 2retained points on each side of a gap. The output mask is not expanded to cover these points. This also applies to smoothing and to an explicitdelta. Formode="interp", edge polynomial fits use a full window and can also span a nearby gap.When
deltais 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 > 0with a physically scaled delta (auto-detected from a uniform coordinate or explicitly provided when the coordinate carries units), the output units aresource_units / coordinate_units**deriv. For example, a first derivative of absorbance with respect tocm⁻¹yieldsabsorbance·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