Skip to content

Savitzky-Golay filter

Fact Value
Also called Regression-based filters, Local polynomial regression
Status Implemented and tested
Family Adaptive and statistical filters
Engine Browser
Type Linear, spatial, separable convolution from least squares
Output Image
Operator shown as dynamic-kernel (exact)
Support radius (size - 1) / 2
Code web/src/core/filters/adaptive/savgol.ts, savgolFilter
Operator code web/src/core/filters/adaptive/savgol.ts, savgolCoefficients
Channels RGB, Brightness only, Color only

Overview

The Savitzky-Golay filter fits a polynomial to the values of each window by least squares and keeps its value at the center. The fit follows peaks and ridges, so the filter smooths noise while keeping their height and width better than a box or Gaussian filter. The fit always uses the same weights for a given size and degree, so the filter is a fixed kernel.

A plot of one row with a narrow peak, a wide peak and a small ripple. The box filter flattens the narrow peak. The Savitzky-Golay filter keeps most of its height and smooths the ripple. A plot of one row with a narrow peak, a wide peak and a small ripple. The box filter flattens the narrow peak. The Savitzky-Golay filter keeps most of its height and smooths the ripple.
With the same size, the Savitzky-Golay filter keeps the height of the narrow peak. The box filter flattens it.

How it works

  1. Fit a polynomial of the chosen degree to the values of the window.
  2. Read the value of the polynomial at the center.
  3. This read-off is a weighted sum with fixed weights, so run it as a kernel along rows, then columns.

Mathematical foundation

\[ c = \bigl[(A^{\mathsf T} A)^{-1} A^{\mathsf T}\bigr]_{0}, \qquad A_{ij} = (i - m)^j, \qquad I_{\text{out}} = c \ast_{x} (c \ast_{y} I) \]

The formula shows the default parameter values.

where

  • \(c\): The smoothing weights
  • \(A\): The matrix of powers of the positions
  • \(m\): The center position, (n - 1) / 2
  • \(p\): The polynomial degree

A has one row per window position i, from 0 to n - 1, and one column per power j, from 0 to p. m = (n - 1) / 2 is the center. The least-squares fit of a polynomial to the window values y has coefficients (A^T A)^-1 A^T y. Its value at the center is the first coefficient, so the weights c are the first row of that matrix. They do not depend on the data, so the filter is a fixed kernel. It runs along rows, then along columns.

Least squares gives the polynomial coefficients as (A^T A)^-1 A^T times the window values. The value at the center is the constant coefficient, so the weights are the first row of that matrix. For 5 values and degree 2 they are (-3, 12, 17, 12, -3) / 35, from the tables of Savitzky and Golay. Degrees 0 and 1 give the plain mean, because a line through symmetric points has its mean at the center. Higher degrees keep more of the shape of peaks and remove less noise.

Kernel or operator

The Inspector shows the kernel and its row and column factors. The negative weights at the ends are what keep peaks from flattening. The Impulse response demo in the Learn panel shows the kernel as an image.

Algorithm

  1. Build A from the window size and degree, and solve the normal equations for the weights.
  2. Pad the image with the border mode.
  3. Correlate each row with the weights, then each column.
  4. Clip the result to 0 to 255.

Parameters

Parameter Default Values Meaning
Window size size 7 3 to 25, odd px The number of values n in each row and column of the fit.
Polynomial degree order 2 0 to 10 The degree p of the fitted polynomial. It is lowered to size - 1 when it is larger.
Border mode border Reflect 101 (default) Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap How pixels outside the image are filled when the window reaches past the edge.
Channels channelScope RGB RGB, Brightness only, Color only Which part of the color image the filter changes.

Implementation

savgolCoefficients in web/src/core/filters/adaptive/savgol.ts solves the normal equations by Gaussian elimination, once per size and degree. The filter correlates rows, then columns, with these weights. This fits a polynomial in x times a polynomial in y, as when scipy.signal.savgol_filter runs along both axes. The filter runs in the browser engine, in Web Workers, through the shared filter framework in web/src/core/filters/run.ts. It handles the border mode, the channel scope, alpha and the mapping to 8 bits. Each strip of the image gets a halo as wide as the support radius, so strips give exactly the pixels of a whole-image run.

Worked example

For a window of 5 values and degree 2, the weights from the code are:

\[ c = \begin{bmatrix} -0.08571 & 0.34286 & 0.48571 & 0.34286 & -0.08571 \end{bmatrix} = \frac{1}{35}\begin{bmatrix} -3 & 12 & 17 & 12 & -3 \end{bmatrix} \]

These are the classic weights from the 1964 paper. They add up to 1, and the two outer weights are negative. The negative weights let the fit follow a peak instead of flattening it. For the row 40, 60, 200, 60, 40 the filter gives (-3 x 40 + 12 x 60 + 17 x 200 + 12 x 60 - 3 x 40) / 35 = 131.43. A box filter of the same size gives 80.

Complexity

  • Time: \(O(N\,n)\)
  • Memory: \(O(N)\)

N is the number of pixels per channel and n the window size. The weights are computed once.

Artifacts and limitations

Limitations

  • Removes less noise than a Gaussian of the same size.
  • Negative weights can overshoot near steps.

Artifacts

  • Small ripples next to sharp edges with high degrees.

Tests

The reference correlates with the weights of scipy.signal.savgol_coeffs. Before it is used, the script checks it against scipy.signal.savgol_filter on both axes with the mirror mode, which is reflect 101. The test covers four size and degree pairs for all five border modes.

References