Savitzky–Golay filter

From formulasearchengine
Jump to navigation Jump to search
Animation showing smoothing being applied, passing through the data from left to right. The red line represents the local polynomial being used to fit a sub-set of the data. The smoothed values are shown as circles.

A Savitzky–Golay filter is a digital filter that can be applied to a set of digital data points for the purpose of smoothing the data, that is, to increase the signal-to-noise ratio without greatly distorting the signal. This is achieved, in a process known as convolution, by fitting successive sub-sets of adjacent data points with a low-degree polynomial by the method of linear least squares . When the data points are equally spaced an analytical solution to the least-squares equations can be found, in the form of a single set of "convolution coefficients" that can be applied to all data sub-sets, to give estimates of the smoothed signal, (or derivatives of the smoothed signal) at the central point of each sub-set. The method, based on established mathematical procedures,[1][2] was popularized by Abraham Savitzky and Marcel J. E. Golay who published tables of convolution coefficients for various polynomials and sub-set sizes in 1964.[3][4] Some errors in the tables have been corrected.[5] The method has been extended for the treatment of 2- and 3-dimensional data.

Savitzky and Golay's paper is one of the most widely cited papers in the journal Analytical Chemistry[6] and is classed by that journal as one of its "10 seminal papers" saying "it can be argued that the dawn of the computer-controlled analytical instrument can be traced to this article".[7]


The data consists of a set of n {xj, yj} points (j = 1, ..., n), where x is an independent variable and yj is an observed value. They are treated with a set of m convolution coefficients, Ci according to the expression

It is easy to apply this formula in a spreadsheet. Selected convolution coefficients are shown in the tables, below. For example, for smoothing by a 5-point quadratic polynomial, m = 5, i = −2, −1, 0, 1, 2 and the jth smoothed data point, Yj, is given by


where, C−2 = −3/35, C−1 = 12 / 35, etc. There are numerous applications of smoothing, which is performed primarily to make the data appear to be less noisy than it really is. The following are applications of numerical differentiation of data.[8]

{{#invoke:Multiple image|render}}
{{#invoke:Multiple image|render}}
  1. Location of maxima and minima in experimental data curves. This was the application that first motivated Savitzky.[4] The first derivative of a function is zero at a maximum or minimum. The diagram shows data points belonging to a synthetic Lorentzian curve, with added noise (blue diamonds). Data are plotted on a scale of half width, relative to the peak maximum at zero. The smoothed curve (red line) and 1st. derivative (green) were calculated with 7-point cubic Savitzky–Golay filters. Linear interpolation of the first derivative values at positions either side of the zero-crossing gives the position of the peak maximum. 3rd. derivatives can also be used for this purpose.
  2. Location of an end-point in a titration curve. An end-point is an inflection point where the second derivative of the function is zero.[9] The titration curve for malonic acid illustrates the power of the method. The first end-point at 4 ml is barely visible, but the second derivative allows its value to be easily determined by linear interpolation to find the zero crossing.
  3. Baseline flattening. In analytical chemistry it is sometimes necessary to measure the height of an absorption band against a curved baseline.[10] Because the curvature of the baseline is much less than the curvature of the absorption band, the second derivative effectively flattens the baseline. Three measures of the derivative height, which is proportional to the absorption band height, are the "peak-to-valley" distances h1 and h2, and the height from baseline, h3.[11]
  4. Resolution enhancement in spectroscopy. Bands in the second derivative of a spectroscopic curve are narrower than the bands in the spectrum: they have reduced half-width. This allows partially overlapping bands to be "resolved" into separate (negative) peaks.[12] The diagram illustrates how this may be used also for chemical analysis, using measurement of "peak-to-valley" distances. In this case the valleys are a property of the 2nd. derivative of a Lorentzian. (x-axis position is relative to the position of the peak maximum on a scale of half width at half height).
  5. Resolution enhancement with 4th. derivative (positive peaks). The minima are a property of the 4th derivative of a Lorentzian.

Moving average


A moving average filter is commonly used with time series data to smooth out short-term fluctuations and highlight longer-term trends or cycles. It is often used in technical analysis of financial data, like stock prices, returns or trading volumes. It is also used in economics to examine gross domestic product, employment or other macroeconomic time series.

An unweighted moving average filter is the simplest convolution filter. Each subset of the data set is fitted by a straight line. It was not included in the Savitzsky-Golay tables of convolution coefficients as all the coefficient values are simply equal to 1/m.

Derivation of convolution coefficients

When the data points are equally spaced, an analytical solution to the least-squares equations can be found.[2] This solution forms the basis of the convolution method of numerical smoothing and differentiation. Suppose that the data consists of a set of n {xj, yj} points (j = 1, ..., n), where x is an independent variable and yj is a datum value. A polynomial will be fitted by linear least squares to a set of m (an odd number) adjacent data points, each separated by an interval h. Firstly, a change of variable is made

where is the value of the central point. z takes the values (e.g. m = 5 → z = −2, −1, 0, 1, 2).[note 1] The polynomial, of degree k is defined as

[note 2]

The coefficients a0, a1 etc. are obtained by solving the normal equations

where the ith row of the Jacobian matrix, , has values 1, zi, zi2, .. .

For example, for a cubic polynomial fitted to 5 points, z= −2, −1, 0, 1, 2 the normal equations are solved as follows.

Now, the normal equations can be factored into two separate sets of equations, by rearranging rows and columns, with

Expressions for the inverse of each of these matrices can be obtained using Cramer's rule

The normal equations become


Multiplying out and removing common factors,

The coefficients of y in these expressions are known as convolution coefficients. They are elements of the matrix

In general,

In matrix notation this example is written as

Tables of convolution coefficients, calculated in the same way for m up to 25, were published for the Savitzky–Golay smoothing filter in 1964,[3][5] The value of the central point, z = 0, is obtained from a single set of coefficients, a0 for smoothing, a1 for 1st. derivative etc. The numerical derivatives are obtained by differentiating Y. This means that the derivatives are calculated for the smoothed data curve. For a cubic polynomial

In general, polynomials of degree (0 and 1),[note 3] (2 and 3), (4 and 5) etc. give the same coefficients for smoothing and even derivatives. Polynomials of degree (1 and 2), (3 and 4) etc. give the same coefficients for odd derivatives.

Algebraic expressions

It is not necessary always to use the Savitzky–Golay tables. The summations in the matrix JTJ can be evaluated in closed form,

so that algebraic formulae can be derived for the convolution coefficients.[13][note 4] Functions that are suitable for use with a curve that has an inflection point are:

Smoothing, polynomial degree 2,3 : (the range of values for i also applies to the expressions below)

1st derivative: polynomial degree 3,4

2nd. derivative: polynomial degree 2,3

3rd. derivative: polynomial degree 3,4

Simpler expressions that can be used with curves that don't have an inflection point are:

Smoothing, polynomial degree 0,1 (moving average):
1st derivative, polynomial degree 1,2:

Higher derivatives can be obtained. For example, a fourth derivative can be obtained by performing two passes of a second derivative function.[14]

Use of orthogonal polynomials

An alternative to fitting m data points by a simple polynomial in the subsidiary variable, z, is to use orthogonal polynomials.

where P0 .. Pk is a set of mutually orthogonal polynomials of degree 0 .. k. Full details on how to obtain expressions for the orthogonal polynomials and the relationship between the coefficients b and a are given by Guest.[2] Expressions for the convolution coefficients are easily obtained because the normal equations matrix, JTJ, is a diagonal matrix as the product of any two orthogonal polynomials is zero by virtue of their mutual orthogonality. Therefore, each non-zero element of its inverse is simply the reciprocal the corresponding element in the normal equation matrix. The calculation is further simplified by using recursion to build orthogonal Gram polynomials. The whole calculation can be coded in a few lines of PASCAL, a computer language well-adapted for calculations involving recursion.[15]

Treatment of first and last points

Savitzky–Golay filters are most commonly used to obtain the smoothed or derivative value at the central point, z = 0, using a single set of convolution coefficients. (m − 1)/2 points at the start and end of the series cannot be calculated using this process. Various strategies can be employed to avoid this inconvenience.

  • The data could be artificially extended by adding, in reverse order, copies of the first (m − 1)/2 points at the beginning and copies of the last (m − 1)/2 points at the end. For instance, with m = 5, two points are added at the start and end of the data y1, ..., yn.
y3,y2,y1, ... ,yn, yn−1, yn−2.
  • Looking again at the fitting polynomial, it is obvious that data can be calculated for all values of z by using all sets of convolution coefficients for a single polynomial, a0 .. ak.
For a cubic polynomial
  • Convolution coefficients for the missing first and last points can also be easily obtained.[15] This is also equivalent to fitting the first (m+1)/2 points with the same polynomial, and similarly for the last points.

Weighting the data

It is implicit in the above treatment that the data points are all given equal weight. Technically, the objective function

being minimized in the least-squares process has unit weights, wi=1. When weights are not all the same the normal equations become


If the same set of diagonal weights is used for all data subsets, W = diag(w1,w2,...wm), an analytical solution to the normal equations can be written down. For example, with a quadratic polynomial,

An explicit expression for the inverse of this matrix can be obtained using Cramer's rule. A set of convolution coefficients may then be derived as


Alternatively the coefficients, C, could be calculated in a spreadsheet, employing a built-in matrix inversion routine to obtain the inverse of the normal equations matrix. This set of coefficients, once calculated and stored, can be used with all calculations in which the same weighting scheme applies. A different set of coefficients is needed for each different weighting scheme.

Two-dimensional convolution coefficients

Two-dimensional smoothing and differentiation can also be applied to tables of data values, such as intensity values in a photographic image which is composed of a rectangular grid of pixels.[16] [17] The trick is to transform part of the table into a row by a simple ordering of the indices of the pixels. Whereas the one-dimensional filter coefficients are found by fitting a polynomial in the subsidiary variable, z to a set of m data points, the two-dimensional coefficients are found by fitting a polynomial in subsidiary variables v and w to a set of m × m data points. The following example, for a bicubic polynomial and m = 5, illustrates the process, which parallels the process for the one dimensional case, above.[18]

The square of 25 data values, d1 − d25

v −2 −1 0 1 2
−2 d1 d2 d3 d4 d5
−1 d6 d7 d8 d9 d10
0 d11 d12 d13 d14 d15
1 d16 d17 d18 d19 d20
2 d21 d22 d23 d24 d25

becomes a vector when the rows are placed one after another.

d = (d1 ... d25)T

The Jacobian has 10 columns, one for each of the parameters a00 − a03 and 25 rows, one for each pair of v and w values. Each row has the form

The convolution coefficients are calculated as

The first row of C contains 25 convolution coefficients which can be multiplied with the 25 data values to provide a smoothed value for the central data point (13) of the 25.

A Matlab[19] routine for computing the coefficients is available. 3-dimensional filters can be obtained with a similar procedure.[16]

Some properties of convolution

  1. The sum of convolution coefficients for smoothing is equal to one. The sum of coefficients for odd derivatives is zero.[20]
  2. The sum of squared convolution coefficients for smoothing is equal to the value of the central coefficient.[21]
  3. Smoothing of a function leaves the area under the function unchanged.[20]
  4. Convolution of a symmetric function with even-derivative coefficients conserves the centre of symmetry.[20]
  5. Properties of derivative filters.[22]

Signal distortion and noise reduction

It is inevitable that the signal will be distorted in the convolution process. From property 3 above, when data which has a peak is smoothed the peak height will be reduced and the half-width will be increased. Both the extent of the distortion and S/N (signal-to-noise ratio) improvement:

  • decrease as the degree of the polynomial increases
  • increase as the width, m of the convolution function increases
Effect of smoothing on data points with uncorrelated noise of unit standard deviation

For example, If the noise in all data points is uncorrelated and has a constant standard deviation, σ, the standard deviation on the noise will be decreased by convolution with an m-point smoothing function to[21]

polynomial degree 0 or 1: (moving average)
polynomial degree 2 or 3: .

These functions are shown in the plot at the right. For example, with a 9-point linear function (moving average) two thirds of the noise is removed and with a 9-point quadratic/cubic smoothing function only about half the noise is removed. Most of the noise remaining is low-frequency noise(see Frequency characteristics of convolution filters, below).

Although the moving average function gives the best noise reduction it is unsuitable for smoothing data which has curvature over m points. A quadratic filter function is unsuitable for getting a derivative of a data curve with an inflection point because a quadratic polynomial does not have one. The optimal choice of polynomial order and number of convolution coefficients will be a compromise between noise reduction and distortion.[23]

Multipass filters

One way to mitigate distortion and improve noise removal is to use a filter of smaller width and perform more than one convolution with it. For two passes of the same filter this is equivalent to one pass of a filter obtained by convolution of the original filter with itself.[24] For example, 2 passes of the filter with coefficients (1/3, 1/3, 1/3) is equivalent to 1 pass of the filter with coefficients (1/9, 2/9, 3/9, 2/9, 1/9).

The disadvantage of multipassing is that the equivalent filter width for n passes of an m-point function is n(m − 1) + 1 so multipassing is subject to greater end-effects. Nevertheless, multipassing has been used to great advantage. For instance, some 40–80 passes on data with a signal-to-noise ratio of only 5 gave useful results.[25] The noise reduction formulae given above do not apply because correlation between calculated data points increases with each pass.

Frequency characteristics of convolution filters

Fourier transform of the 9-point quadratic/cubic smoothing function

Convolution maps to multiplication in the Fourier co-domain. The discrete Fourier transform of a convolution filter is a real-valued function which can be represented as

θ runs from 0 to 180 degrees, after which the function merely repeats itself. The plot for a 9-point quadratic/cubic smoothing function is typical. At very low angle, the plot is almost flat, meaning that low-frequency components of the data will be virtually unchanged by the smoothing operation. As the angle increases the value decreases so that higher frequency components are more and more attenuated. This shows that the convolution filter can be described as a low-pass filter: the noise that is removed is primarily high-frequency noise and low-frequency noise passes through the filter.[26] Some high-frequency noise components are attenuated more than others, as shown by undulations in the Fourier transform at large angles. This can give rise to small oscillations in the smoothed data.[27]

Convolution and correlation

Convolution affects the correlation between errors in the data. The effect of convolution can be expressed as a linear transformation.

By the law of error propagation, the variance-covariance matrix of the data, A will be transformed into B according to

To see how this applies in practice, consider the effect of a 3-point moving average on the first three calculated points, Y2 − Y4, assuming that the data points have equal variance and that there is no correlation between them. A will be an identity matrix multiplied by a constant, σ2, the variance at each point.

In this case the correlation coefficients,

between calculated points i and j will be


In general, the calculated values are correlated even when the observed values are not correlated. The correlation extends over m − 1 calculated points at a time.[28]

Multipass filters

To illustrate the effect of multipassing on the noise and correlation of a set of data, consider the effects of a second pass of a 3-point moving average filter. For the second pass[note 5]

After two passes, the standard deviation of the central point has decreased to = 0.48σ, compared to 0.58σ for one pass. The noise reduction is a little less than would be obtained with one pass of a 5-point moving average which, under the same conditions, would result in the smoothed points having the smaller standard deviation of 0.45σ.

Correlation now extends over a span of 4 sequential points with correlation coefficients

The advantage obtained by performing two passes with the narrower smoothing function is that it introduces less distortion into the calculated data.

See also


Tables of selected convolution coefficients

Consider a set of data points (xj, yj)1 ≤ jn. The Savitzky–Golay tables refer to the case that the step xj − xj − 1 is constant, h. Examples of the use of the so-called convolution coefficients, with a cubic polynomial and a window size, m, of 5 points are as follows.

Smoothing:  ;
1st derivative:  ;
2nd. derivative: .

Selected values of the convolution coefficients for polynomials of degree 1,2,3, 4 and 5 are given in the following tables. The values were calculated using the PASCAL code provided in Gorry.[15]

Coefficients for smoothing
Degree 2/3 (quadratic/cubic) 4/5 (quartic/quintic)
Window size 5 7 9 7 9
−4 −21 15
−3 −2 14 5 −55
−2 −3 3 39 −30 30
−1 12 6 54 75 135
0 17 7 59 131 179
1 12 6 54 75 135
2 −3 3 39 −30 30
3 −2 14 5 −55
4 −21 15
Normalisation 35 21 231 231 429
Coefficients for 1st derivative
Degree 1/2 (linear/quadratic) 3/4 (cubic/quartic)
Window size 5 7 9 5 7 9
−4 −4 86
−3 −3 −3 22 −142
−2 −2 −2 −2 1 −67 −193
−1 −1 −1 −1 −8 −58 −126
0 0 0 0 0 0 0
1 1 1 1 8 58 126
2 2 2 2 −1 67 193
3 3 3 −22 142
4 4 −86
Normalisation 10 28 60 12 252 1,188
Coefficients for 2nd derivative
Degree 2/3 (quadratic/cubic) 4/5 (quartic/quintic)
Window size 5 7 9 5 7 9
−4 28 −126
−3 5 7 −13 371
−2 2 0 −8 −1 67 151
−1 −1 −3 −17 16 −19 −211
0 −2 −4 −20 −30 −70 −370
1 −1 −3 −17 16 −19 −211
2 2 0 −8 −1 67 151
3 5 7 −13 371
4 28 −126
Normalisation 7 42 462 12 132 1716
Coefficients for 3rd derivative
Degree 3/4 (cubic/quartic) 5/6 (quintic/sextic)
Window size 5 7 9 7 9
−4 −14 100
−3 −1 7 1 −457
−2 −1 1 13 −8 256
−1 2 1 9 13 459
0 0 0 0 0 0
1 −2 −1 −9 −13 −459
2 1 −1 −13 8 −256
3 1 −7 −1 457
4 14 −100
Normalisation 2 6 198 8 1144
Coefficients for 4th derivative
Degree 4/5 (quartic/quintic)
Window size 7 9
−4 14
−3 3 −21
−2 −7 −11
−1 1 9
0 6 18
1 1 9
2 -7 −11
3 3 −21
4 14
Normalisation 11 143


  1. With even values of m, z will run from 1 − m to m − 1 in steps of 2
  2. The simple moving average is a special case with k = 0, Y = a0. In this case all convolution coefficients are equal to 1/m.
  3. Smoothing using the moving average is equivalent, with equally spaced points, to local fitting with a (sloping) straight line
  4. The expressions given here are different from those of Madden, which are given in terms of the variable m' = (m − 1)/2.
  5. The same result is obtained with one pass of the equivalent filter with coefficients (1/9, 2/9, 3/9, 2/9, 1/9) and an identity variance-covariance matrix


  1. {{#invoke:citation/CS1|citation |CitationClass=book }}. "Graduation Formulae obtained by fitting a Polynomial."
  2. 2.0 2.1 2.2 {{#invoke:citation/CS1|citation |CitationClass=book }}
  3. 3.0 3.1 {{#invoke:Citation/CS1|citation |CitationClass=journal }}
  4. 4.0 4.1 {{#invoke:Citation/CS1|citation |CitationClass=journal }}
  5. 5.0 5.1 {{#invoke:Citation/CS1|citation |CitationClass=journal }}
  6. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
  7. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
  8. {{#invoke:citation/CS1|citation |CitationClass=book }}
  9. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
  10. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
  11. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
  12. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
  13. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
  14. Template:Harnvb
  15. 15.0 15.1 15.2 {{#invoke:Citation/CS1|citation |CitationClass=journal }}
  16. 16.0 16.1 Template:Cite techreport
  17. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
  18. Template:Cite web
  19. Template:Cite web
  20. 20.0 20.1 20.2 Template:Harvnb
  21. 21.0 21.1 {{#invoke:Citation/CS1|citation |CitationClass=journal }}
  22. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
  23. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
  24. Template:Harnvb
  25. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
  26. Template:Harnvb
  27. {{#invoke:Citation/CS1|citation |CitationClass=journal }}
  28. Template:Harnvb
  • {{#invoke:citation/CS1|citation

|CitationClass=book }}

External links