xrscipy.signal.savgol_filter
- xrscipy.signal.savgol_filter(darray: DataArray, window_length: float, polyorder: int, deriv: int = 0, delta: float = 1.0, dim: str = None, mode: str = 'interp', cval: float = 0.0) DataArray
Apply a Savitzky-Golay filter to an array.
This is a 1-D filter. If x has dimension greater than 1, dim determines the dim along which the filter is applied.
- Parameters:
darray (xarray.DataArray) – The data to be filtered.
window_length (float) – The length of the filter window in the units of the specified dimension. This will be converted to the number of samples based on the coordinate spacing.
polyorder (int) – The order of the polynomial used to fit the samples. polyorder must be less than window_length.
deriv (int, optional) – 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, optional) – The spacing of the samples to which the filter will be applied. This is only used if deriv > 0. Default is 1.0.
dim (str, optional) – The dimension of the array darray along which the filter is to be applied. Default is the only dimension if 1D, otherwise must be specified.
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 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, optional) – Value to fill past the edges of the input if mode is ‘constant’. Default is 0.0.
- Returns:
y – The filtered data.
- Return type:
ndarray, same shape as x
Notes
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):
mode | Ext | Input | Ext -----------+---------+------------------------+--------- '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
Added in version 0.14.0.
Examples
>>> import numpy as np >>> from scipy.signal import savgol_filter >>> np.set_printoptions(precision=2) # For compact display. >>> x = np.array([2, 2, 5, 2, 1, 0, 1, 4, 9])
Filter with a window length of 5 and a degree 2 polynomial. Use the defaults for all other parameters.
Examples
>>> savgol_filter(x, 5, 2) array([1.66, 3.17, 3.54, 2.86, 0.66, 0.17, 1. , 4. , 9. ])
Note that the last five values in x are samples of a parabola, so when mode=’interp’ (the default) is used with polyorder=2, the last three values are unchanged. Compare that to, for example, mode=’nearest’:
Examples
>>> savgol_filter(x, 5, 2, mode='nearest') array([1.74, 3.03, 3.54, 2.86, 0.66, 0.17, 1. , 4.6 , 7.97])