views:

65

answers:

3

Savitzky-Golay smoothing filter can be used to calculate the coefficients so as to calculate the smoothed y-values by applying the coefficients to the adjacent values. The smoothed curve looks great.

According to the papers, the coefficients can also be used to calculate the derivatives up to 5th order. The coefficients calculation parameter ld would need to be set to the order of derivatives. For the first derivative, the appropriate setting is ld=1, and the value of the derivative is the accumulated sum divided by the sampling interval h.

My question is: how to use the obtained coefficients to calculate the accumulated sum? how is the derivative calculated? any sample code?

A: 

Cookbook / SavitzkyGolay

Robert Karl
A: 

This Wikipedia page helped me design a S-G filter.

Doug Currie
The wiki is not clear about the relations of smoothed Y values to the values of {a0,a1,a2,...,ak}, especially on the moving x. So, to calculate the first derivative at (xi,yi), do we always assume Z = 0? or use the formula (a1 + 2 * a2 * z + 3 * a3 * z^2) / h? what are the values of a1, a2 and a3 in relation to yi or the smoothed Yi? The formula does give sort of right shape, but at the flattened top of the first derivative, take a sine wave as example, the value at flattened points should be zero, how to achieve that?
Lin Song Yang
A: 

To calculate the derivatives using Savitzky-Golay smoothing filter, the polynomial coefficients computation has a parameter b, the value b[derivative] must be set to 1.0, the array be will be used in the LU decomposition call.

The key to get derivatives right is to understand the polynomial formula: Y = a0 + a1 * z + a2 * z^2 + ... + ak * z^k. The values a0, a1, a2, ..., ak are actually the smoothed values within the moving window, z = (x - x0) / h, for the centre point of the moving window, we can assume z = 0 since x = x0.

Therefore, in the derivative calculations:

dY/dx = a1/h; and d2Y/dx2 = 2a2/h^2.

Where a1, a2 are the smoothed values of y by using the coefficients calculated on the corresponding derivatives.

Lin Song Yang