bundles / scipy 1.17.1 / scipy / signal / _savitzky_golay / savgol_filter
function
scipy.signal._savitzky_golay:savgol_filter
Signature
def savgol_filter ( x , window_length , polyorder , deriv = 0 , delta = 1.0 , axis = -1 , mode = interp , cval = 0.0 ) Summary
Apply a Savitzky-Golay filter to an array.
Extended Summary
This is a 1-D filter. If x has dimension greater than 1, axis determines the axis along which the filter is applied.
Parameters
x: array_likeThe data to be filtered. If
xis not a single or double precision floating point array, it will be converted to typenumpy.float64before filtering.window_length: intThe length of the filter window (i.e., the number of coefficients). If
modeis 'interp',window_lengthmust be less than or equal to the size ofx.polyorder: intThe order of the polynomial used to fit the samples.
polyordermust be less thanwindow_length.deriv: int, optionalThe 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, optionalThe spacing of the samples to which the filter will be applied. This is only used if deriv > 0. Default is 1.0.
axis: int, optionalThe axis of the array
xalong which the filter is to be applied. Default is -1.mode: str, optionalMust 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
modeis 'constant', the padding value is given bycval. 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 degreepolyorderpolynomial is fit to the lastwindow_lengthvalues of the edges, and this polynomial is used to evaluate the lastwindow_length // 2output values.cval: scalar, optionalValue to fill past the edges of the input if
modeis 'constant'. Default is 0.0.
Returns
y: ndarray, same shape as `x`The filtered data.
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
cvalargument.'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
Array API Standard Support
savgol_filter has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ✅ PyTorch ✅ ⛔ JAX ⚠️ no JIT ⛔ Dask ✅ n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
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])✓
savgol_filter(x, 5, 2)
✓savgol_filter(x, 5, 2, mode='nearest')
✓See also
Aliases
-
scipy.signal.savgol_filter