scipy.signal.savgol_filter#
- scipy.signal.savgol_filter(x, window_length, polyorder, deriv=0, delta=1.0, axis=-1, mode='interp', cval=0.0)[source]#
Apply a Savitzky-Golay filter to an array.
This is a 1-D filter. If x has dimension greater than 1, axis determines the axis along which the filter is applied.
- Parameters:
- xarray_like
The data to be filtered. If x is not a single or double precision floating point array, it will be converted to type
numpy.float64
before filtering.- window_lengthint
The length of the filter window (i.e., the number of coefficients). If mode is ‘interp’, window_length must be less than or equal to the size of x.
- polyorderint
The order of the polynomial used to fit the samples. polyorder must be less than window_length.
- derivint, 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.
- deltafloat, optional
The spacing of the samples to which the filter will be applied. This is only used if deriv > 0. Default is 1.0.
- axisint, optional
The axis of the array x along which the filter is to be applied. Default is -1.
- modestr, 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.
- cvalscalar, optional
Value to fill past the edges of the input if mode is ‘constant’. Default is 0.0.
- Returns:
- yndarray, same shape as x
The filtered data.
See also
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.
>>> 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’:
>>> 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])