Open Lab/Preprocessing · Method
Savitzky Golay Filtering
SG
A local polynomial filtering method used to smooth signals and estimate derivatives while preserving important spectral structure.
What is Savitzky Golay?
Savitzky-Golay filtering is a local least squares polynomial method for smoothing discrete signals and for estimating their derivatives.
A polynomial of chosen degree is fitted to a moving window of neighboring points. For smoothing, the fitted polynomial is evaluated at the center of the window. Derivatives are obtained from derivatives of that same local polynomial.
Savitzky and Golay introduced the procedure in 1964. Gorry later presented a general convolution form of the same least squares construction.
Why use it?
Spectroscopic and other analytical signals often contain high-frequency measurement noise. Direct finite differences used as numerical derivatives can amplify that noise.
Savitzky-Golay filtering is widely used in spectral preprocessing because smoothing and derivative estimation share one local polynomial framework. When the window and polynomial degree are chosen with care, the method can retain local peak shape better than a simple moving average.
It is not universally optimal, and it does not automatically improve a later calibration or classification model. Its value depends on the signal and on the analysis that follows.
How does it work?
The filter moves a symmetric window along the signal. At each valid center point the local observations are approximated by a polynomial in a centered coordinate.
- Select a local window of neighboring observations.
- Fit a polynomial of chosen degree by least squares.
- Evaluate the fitted polynomial at the central position.
- Move the window by one position.
- Repeat across the signal.
For a derivative of order , differentiate the local polynomial and evaluate that derivative at the center. The main choices are window length, polynomial degree, derivative order, and, when a derivative is required, the sampling interval.
Mathematics & algorithm
Let a local window around position contain the samples for . Fit a polynomial of degree .
Mathematical foundation
- center index of the local window
- half-width of the window
- polynomial degree
- fitted polynomial coefficient of order r
- smoothed value at the center
- derivative order
- sampling interval used to scale derivatives
- convolution coefficient for lag k
Interpretation
Equation 3 is smoothing: the intercept of the centered polynomial. Equation 4 is the corresponding derivative when the local coordinate is expressed in sample steps of size Δx. Once the window, degree and derivative order are fixed, the least squares solution can be written as the convolution in Equation 5. Coefficient conventions can differ between implementations.
Savitzky Golay Filtering
InputSignal y, window length w, polynomial degree p, derivative order d, sampling interval Δx
OutputFiltered signal ŷ
- 01for each valid center do
- 02extract the local window
- 03construct the local polynomial design matrix
- 04estimate polynomial coefficients by least squares
- 05if then
- 06evaluate the polynomial at the center
- 07else
- 08evaluate derivative order at the center
- 09end if
- 10end for
- 11apply the selected boundary strategy
- 12return
This pseudocode is a SPARKS representation of the local least squares construction. It is not a verbatim extract from the cited papers. A centered window of odd length w = 2m + 1 is assumed. Boundary values require a separate strategy.
A centered window cannot be formed at the first and last points. Implementations therefore need an edge rule. SciPy savgol_filter defaults to fitting a local polynomial at each edge (mode='interp'). MATLAB sgolayfilt applies the startup and terminal rows of the Savitzky-Golay coefficient matrix.
Visual example
A noisy analytical-like spectrum is smoothed with a cubic Savitzky-Golay filter. The right panel compares window lengths 7, 15 and 31 (light to dark). A longer window reduces noise more and can broaden narrow peaks.
Code
The scientific method is defined by the local least squares construction above. The code below uses documented scientific software rather than a from-scratch copy of a paper. Python uses SciPy savgol_filter. MATLAB uses sgolayfilt for smoothing and sgolay for derivative coefficients.
window_length (SciPy) or frame length (MATLAB) sets the local neighborhood. SciPy requires polyorder to be smaller than the window length. MATLAB requires the polynomial order to be smaller than the frame length, and the frame length to be a positive odd integer. deriv or the selected column of g sets the derivative order. SciPy delta and MATLAB dx scale derivatives by the sampling interval. SciPy defaults to mode='interp' at the edges.
from scipy.signal import savgol_filter y_smooth = savgol_filter( y, window_length=11, polyorder=3, deriv=0) y_derivative = savgol_filter( y, window_length=11, polyorder=3, deriv=1, delta=dx) Practical notes
- Window length is a primary choice. A longer window usually reduces noise more strongly and can distort narrow spectral features.
- Polynomial degree must remain lower than the window length. A degree close to the window length leaves little smoothing.
- Derivative estimates are more sensitive to noise than the smoothed signal, so smoothing and differentiation are chosen together.
- Parameter choices should be judged for the specific signal and for the analysis that follows. No single window and degree is universally optimal.
- Edge handling affects the beginning and the end of the processed signal. SciPy interp mode and MATLAB sgolayfilt use different documented boundary constructions.
References
- 1.
Savitzky, A., & Golay, M. J. E. (1964). Smoothing and Differentiation of Data by Simplified Least Squares Procedures. Analytical Chemistry, 36(8), 1627-1639.
doi:10.1021/ac60214a047 - 2.
Gorry, P. A. (1990). General least-squares smoothing and differentiation by the convolution (Savitzky-Golay) method. Analytical Chemistry, 62(6), 570-573.
doi:10.1021/ac00205a007 - 3.
Rinnan, Å., van den Berg, F. W. J., & Engelsen, S. B. (2009). Review of the most common pre-processing techniques for near-infrared spectra. TrAC Trends in Analytical Chemistry, 28(10), 1201-1222.
doi:10.1016/j.trac.2009.07.007
