 #jsDisabledContent { display:none; } My Account | Register | Help Flag as Inappropriate This article will be permanently flagged as inappropriate and made unaccessible to everyone. Are you certain this article is inappropriate?          Excessive Violence          Sexual Content          Political / Social Email this Article Email Address:

# Numerical smoothing and differentiation

Article Id: WHEBN0010684617
Reproduction Date:

 Title: Numerical smoothing and differentiation Author: World Heritage Encyclopedia Language: English Subject: Collection: Publisher: World Heritage Encyclopedia Publication Date:

### Numerical smoothing and differentiation

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, 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. Some errors in the tables have been corrected. 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 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".

## Applications

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

$Y_j= \sum _\left\{i=-\left(m-1\right)/2\right\}^\left\{i=\left(m-1\right)/2\right\}C_i\, y_\left\{j+i\right\}\qquad \frac\left\{m+1\right\}\left\{2\right\} \le j \le n-\frac\left\{m-1\right\}\left\{2\right\}$

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

$Y_j = \frac\left\{1\right\}\left\{35\right\} \left(-3 \times y_\left\{j - 2\right\} + 12 \times y_\left\{j - 1\right\} + 17 \times y_j + 12 \times y_\left\{j + 1\right\} -3 \times y_\left\{j + 2\right\}\right)$,

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.

(1) Synthetic Lorentzian + noise (blue) and 1st. derivative (green)
(2) Titration curve (blue) for malonic acid and 2nd. derivative (green). The part in the light blue box is magnified 10 times
(3) Lorentzian on exponential baseline (blue) and 2nd. derivative (green)
(4) Sum of two Lorentzians (blue) and 2nd. derivative (green)
(5) 4th. derivative of the sum of two Lorentzians
1. Location of maxima and minima in experimental data curves. This was the application that first motivated Savitzky. 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. 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. 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.
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. 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.

## Derivation of convolution coefficients

When the data points are equally spaced an analytical solution to the least-squares equations can be found. 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

$z = = \left\left( ^\left\{\mathbf\left\{T\right\}\right\} \left\{\mathbf\left\{J\right\}\right\}\right\} \right\right)^\left\{ - \left\{\mathbf\left\{1\right\}\right\}\right\} \left\{\mathbf\left\{J\right\}\right\}^\left\{\mathbf\left\{T\right\}\right\} \left\{\mathbf\left\{y\right\}\right\}$

where the ith row of the Jacobian matrix, $\mathbf J =\mathbf$, 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.



\mathbf{J} = \begin{pmatrix} 1 & -2 & 4 & -8 \\ 1 & - 1 & 1 & -1 \\ 1 & 0 & 0 & 0 \\ 1 & 1 & 1 & 1 \\ 1 & 2 & 4 & 8 \\ \end{pmatrix}



\mathbf{J^TJ} = \begin{pmatrix} m & \sum z & \sum z^2 & \sum z^3 \\

  \sum z & \sum z^2 & \sum z^3 & \sum z^4  \\
\sum z^2 & \sum z^3 & \sum z^4 & \sum z^5  \\
\sum z^3 & \sum z^4 & \sum z^5 & \sum z^6  \\


\end{pmatrix}= \begin{pmatrix} m & 0 & \sum z^2 &0 \\

  0 & \sum z^2 & 0 & \sum z^4   \\
\sum z^2 & 0 & \sum z^4 &0 \\
0 &\sum z^4 & 0 & \sum z^6 \\


\end{pmatrix}= \begin{pmatrix} 5 & 0 & 10 & 0 \\ 0 & 10 & 0 & 34 \\ 10 & 0 & 34 & 0 \\ 0 & 34 & 0 & 130\\ \end{pmatrix}

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



\mathbf {J^TJ}_{even}=\begin{pmatrix}5 & 10 \\ 10 & 34 \\\end{pmatrix} \quad \mathrm{and} \quad \mathbf {J^TJ}_{odd}=\begin{pmatrix}10 & 34 \\ 34 & 130 \\\end{pmatrix}



(\mathbf{J^TJ})^{-1}_{even}={1\over70}\begin{pmatrix}34 & -10\\ -10 & 5 \\\end{pmatrix}

\quad \mathrm{and} \quad


(\mathbf{J^TJ})^{-1}_{odd}={1\over144}\begin{pmatrix}130 & -34\\ -34 & 10 \\\end{pmatrix} The normal equations become



\begin{pmatrix} {a_0 } \\ {a_2 } \\\end{pmatrix}_j= {1\over70}\begin{pmatrix} 34 & -10\\ -10 & 5 \\\end{pmatrix} \begin{pmatrix} 1&1&1&1&1\\4&1&0&1&4\\\end{pmatrix} \begin{pmatrix}y_{j-2} \\ y_{j-1} \\ y_j \\ y_{j+1} \\ y_{j+2} \\\end{pmatrix} and



\begin{pmatrix} a_1 \\ a_3 \\\end{pmatrix}_j= {1\over144}\begin{pmatrix} 130 & -34\\ -34 & 10 \\\end{pmatrix} \begin{pmatrix} -2&-1&0&1&2\\-8&-1&0&1&8\\\end{pmatrix} \begin{pmatrix}y_{j-2} \\ y_{j-1} \\ y_j \\ y_{j+1} \\ y_{j+2} \\\end{pmatrix} Multiplying out and removing common factors,

$a_\left\{0,j\right\}=\left\{1\over35\right\}\left(-3y_\left\{j-2\right\}+12y_\left\{j-1\right\}+17y_j+12y_\left\{j+1\right\}-3y_\left\{j+2\right\}\right)$
$a_\left\{1,j\right\}=\left\{1\over12\right\}\left(y_\left\{j-2\right\} - 8y_\left\{j-1\right\} +8y_\left\{j+1\right\} -y_\left\{j+2\right\}\right)$
$a_\left\{2,j\right\}=\left\{1\over14\right\}\left(2y_\left\{j-2\right\} - y_\left\{j-1\right\} -2y_j -y_\left\{j+1\right\} +2y_\left\{j+2\right\}\right)$
$a_\left\{3,j\right\}=\left\{1\over12\right\}\left(-y_\left\{j-2\right\} +2y_\left\{j-1\right\} -2y_\left\{j+1\right\} +y_\left\{j+2\right\}\right)$

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

$\mathbf\left\{C=\left(J^TJ\right)^\left\{-1\right\}J^T\right\}$

In general,

$\left(C \otimes y\right)_j\ = Y_j= \sum _\left\{i=-\left(m-1\right)/2\right\} ^\left\{i=\left(m-1\right)/2\right\}C_i\, y_\left\{j+i\right\}\qquad \frac\left\{m+1\right\}\left\{2\right\} \le j \le n-\frac\left\{m-1\right\}\left\{2\right\}$

Tables of convolution coefficients, calculated in the same way for m up to 25, were published for the Savitzky–Golay filter in 1964, 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

$Y = a_0 + a_1 z + a_2 z^2 + a_3 z^3= a_0 \text\left\{ at \right\} z = 0, x=\bar x$
$\frac = \frac\left\{1\right\}\left\{h\right\}\left\left( \left\{a_1 + 2a_2 z + 3a_3 z^3 \right\} \right\right) = \frac\left\{1\right\}\left\{h\right\}a_1 \text\left\{ at \right\} z = 0, x=\bar x$
$\frac = \frac\left\{1\right\}\left\left( \left\{2a_2 + 6a_3 z\right\} \right\right) = \frac\left\{2\right\}\left\{h^2\right\}a_2 \left\{\text\left\{ at \right\}\right\}z =0, x=\bar x$
$\frac = \frac\left\{6\right\}_3$

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.

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

$U = \sum_i w_i\left(Y_i-y_i\right)^2$

being minimized has unit weights, wi=1. When weights are not all the same the normal equations become

$\mathbf a = \left\left( \mathbf \left\{J^T W\right\} \mathbf\left\{J\right\} \right\right)^\left\{-1\right\} \mathbf\left\{J^T W\right\} \mathbf\left\{y\right\}\qquad W_\left\{i,i\right\} \ne 1$,

These equations do not have an analytical solution. To use non-unit weights, the coefficients of the fitting polynomial must be calculated individually for each data sub-set, using local regression.

### 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,

$\sum_\left\{-\left(m-1\right)/2\right\}^\left\{\left(m-1\right)/2\right\} z^2= \left\{m\left(m^2-1\right) \over 12\right\}$
$\sum z^4 = \left\{m\left(m^2-1\right)\left(3m^2-7\right) \over 240\right\}$
$\sum z^6= \left\{m\left(m^2-1\right)\left(3m^4-18m^2+31\right) \over 1344\right\}$

so that algebraic formulae can be derived for the convolution coefficients.[note 4] For a cubic polynomial the expressions are (with (1 − m)/2 ≤ i ≤ (m − 1)/2)

Smoothing: 

C_{0i} = \frac{ {\left( {3m^2 - 7 - 20i^2 } \right)/4}}

1st derivative: 

C_{1i} = \frac{ {5\left( {3m^4 - 18m^2 + 31} \right)i - 28\left( {3m^2 - 7} \right)i^3 }}

2nd. derivative: 

C_{2i} = \frac

3rd. derivative: 

C_{3i} = \frac .

These functions are suitable for use with any curve that has an inflection point. Higher derivatives can be obtained. For example, a fourth derivative can be obtained by performing two passes of a second derivative function.

### 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.

$Y = b_0 P^0\left(z\right) + b_1 P^1\left(z\right) \cdots + b_k P^k\left(z\right).$

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. 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.

### 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
$Y=a_0+a_1z + a_2 z^2 + a_3 z^3$
$\frac = \frac\left\{1\right\}\left\{h\right\}\left\left( \left\{a_1 + 2a_2 z + 3a_3 z^2 \right\} \right\right)$
$\frac = \frac\left\{1\right\}\left\left( \left\{2a_2 + 6a_3 z\right\} \right\right)$
$\frac = \frac\left\{6\right\}_3 \left\{\text\left\{ \right\}\right\}$
• Convolution coefficients for the missing first and last points can also be easily obtained. This is also equivalent to fitting the first (m+1)/2 points with the same polynomial, and similarly for the last points.

## 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.  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 coefficient 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.

$v = \frac\left\{x - \bar x\right\}\left\{h\left(x\right)\right\}; w = \frac\left\{y - \bar y\right\}\left\{h\left(y\right)\right\}$
$Y = a_\left\{00\right\} + a_\left\{10\right\}v + a_\left\{01\right\}w + a_\left\{20\right\}v^2 + a_\left\{11\right\}vw+a_\left\{02\right\}w^2 + a_\left\{30\right\}v^3 + a_\left\{21\right\}v^2w + a_\left\{12\right\}vw^2 + a_\left\{03\right\}w^3$

The square of 25 data values, d1 - d25

 v -2 -1 0 1 2 w −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

$J_\left\{row\right\} = 1\ v\ w\ v^2\ vw\ w^2\ v^3\ v^2w\ vw^2\ w^3$

The convolution coefficients are calculated as

$\mathbf\left\{C\right\} = \left\left( \mathbf\left\{J\right\}^T \mathbf\left\{J\right\} \right\right)^\left\{-1\right\} \mathbf\left\{J\right\}^T$

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 routine for computing the coefficients is available. 3-dimensional filters can be obtained with a similar procedure.

## 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.
2. The sum of squared convolution coefficients for smoothing is equal to the value of the central coefficient.
3. Smoothing of a function leaves the area under the function unchanged.
4. Convolution of a symmetric function with even-derivative coefficients conserves the centre of symmetry.
5. Properties of derivative filters.

### 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

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

polynomial degree 0 or 1:$\sqrt \sigma$ (moving average)
polynomial degree 2 or 3: $\sqrt\left\{\frac\left\{3\left(3m^2-7\right)\right\}\left\{4m\left(m^2-4\right)\right\}\right\} \sigma$.

Thus, 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. To remove two thirds of the noise with a quadratic/cubic smoothing function 21 points will be needed.

Although the moving average (polynomial order 0 or 1) gives the best noise reduction it is unsuitable for smoothing data which has curvature over m points. A quadratic filter 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.

#### 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. The disadvantage of this process 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. The noise reduction formulae given above do not apply because correlation between calculated data points increases with each pass.

### Frequency characteristics of convolution filters

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

$FT\left(\theta\right)= \sum_\left\{j=\left(1-m\right)/2\right\}^\left\{j=\left(m-1\right)/2\right\} C_j \cos\left(j\theta\right)$

θ 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. 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.

### Convolution and correlation

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

$Y_j\ = \sum_\left\{i=-\left(m-1\right)/2\right\}^\left\{i=\left(m-1\right)/2\right\} C_i\, y_\left\{j+i\right\}$

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

$\mathbf B=\mathbf C \mathbf A\mathbf C^T$

To see how this applies in practice, consider the effect of a 5-point quadratic smoothing function for the first five calculated points, Y3 - Y7.



\mathbf C ={1 \over 35} \begin{pmatrix}

   - 3 & 12 & 17 & 12 &  - 3 & 0 &0&0&0&.. \\
0 & - 3 & 12 & 17 & 12 &  - 3 &0&0&0&.. \\
0 &0& - 3 & 12 & 17 & 12 &  - 3&0&0&..\\
0 &0&0& - 3 & 12 & 17 & 12 &  - 3&0&..\\
0&0 &0&0& - 3 & 12 & 17 & 12 &  - 3&..\\
..\\
\end{pmatrix}


If one assumes 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 that case, $\mathbf B=\sigma^2 \mathbf C \mathbf C^T$. The correlation coefficients, $\rho_\left\{ij\right\}=B_\left\{ij\right\}/\sigma^2 \left(i\ne j\right)$, between calculated points i and j will be obtained by vector multiplication of rows i and j of C.



\rho_{ij} = \begin{pmatrix} i\\ 2&0.27\\ 3&0.03&0.27\\ 4&-0.06&0.03&0.27\\ 5&0.01&-0.06&0.03&0.27\\ j&1&2&3&4\\ \end{pmatrix} In this example the correlation coefficient between adjacent points is 0.27. Thus, the calculated values are correlated even when the observed values are not correlated. The same pattern applies to the rest of the calculated points. The correlation extends over m-1 calculated points at a time.