Signal smoothing
Reduce noise in sampled sensor or experimental data while retaining local polynomial structure.
SciPy · Function
Applies a one-dimensional Savitzky–Golay filter to an array, optionally computing a derivative. For multidimensional input, filtering occurs along the selected axis.
The function applies a one-dimensional Savitzky–Golay operation along the selected axis. It uses a local polynomial defined by window_length and polyorder; setting deriv above zero returns a derivative, scaled by the sample spacing delta.
scipy.signal.savgol_filter(x, window_length, polyorder, deriv=0, delta=1.0, axis=-1, mode='interp', cval=0.0)| Name | Type | Description |
|---|---|---|
x | array_like | Input array containing the data to filter. |
window_length | int | Length of the filter window, meaning the number of coefficients. |
polyorder | int | Order of the polynomial used to fit samples; it must be less than window_length. |
deriv | int, optional | Derivative order to compute; defaults to 0 and must be nonnegative. |
delta | float, optional | Sample spacing used when deriv is greater than zero; defaults to 1.0. |
axis | int, optional | Axis along which the filter is applied; defaults to -1. |
mode | str, optional | Boundary treatment mode; defaults to 'interp'. |
cval | float, optional | Value used outside the input when mode is 'constant'; defaults to 0.0. |
ndarray: filtered data, with the same shape as x.
import numpy as np
from scipy.signal import savgol_filter
x = np.linspace(0, 2 * np.pi, 101)
noisy = np.sin(x) + 0.08 * np.cos(17 * x)
smoothed = savgol_filter(noisy, window_length= nine if False else nine, polyorder=3)
print(smoothed.shape)Smooths a one-dimensional noisy signal while preserving its array length.
Reduce noise in sampled sensor or experimental data while retaining local polynomial structure.
Estimate slopes or higher-order changes in evenly or regularly spaced samples.
Process each row, column, or other dimension of a multidimensional array.
It smooths a one-dimensional array with a Savitzky–Golay filter. For multidimensional input, axis selects the dimension along which filtering occurs.
Follow-up: How would you smooth each row of a matrix? Set axis to the row-sample dimension, commonly axis=-1 when samples are stored in columns.
window_length specifies the number of coefficients in the filter window, while polyorder specifies the polynomial degree used for fitting. polyorder must be smaller than window_length.
Follow-up: What constraint does the default mode add? window_length must also be no larger than the size of x.
deriv selects the derivative order; zero performs ordinary filtering without differentiation. When deriv is positive, delta supplies the sample spacing used for derivative scaling.
Follow-up: When is delta ignored? It is only used when deriv is greater than zero.
scipy.signal.savgol_coeffs