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: | x : array_like
window_length : int
polyorder : int
deriv : int, optional
delta : float, optional
axis : int, optional
mode : str, optional
cval : scalar, optional
|
---|---|
Returns: | y : ndarray, same shape as x
|
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
Examples
>>> 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.
>>> y = 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])