# 5-point stencil or Savitsky-Golay for differences

Started by April 25, 2019
```I had some code computing the second difference of a sampled signal as
(x(n-1)-2x(n)+x(n+1))/(square of interval)
I only needed a crude estimate of the second derivative in this case.

Then I came across "five point stencils"
By this method, the second difference is
(-x(n-2)+16x(n-1)-30n(n)+16x(n+1)-x(n+2))/12/(square of interval)

And then 5-sample Savitsky-Golay formula, which seems rather different
(2x(n-2)-x(n-1)+2x(n)-x(n+1)+2x(n+2))/7/(square of interval)

Are either of these "better" in any sense?
```
```On Thursday, April 25, 2019 at 3:13:58 AM UTC-5, conr...@gmail.com wrote:

> I only needed a crude estimate of the second derivative in this case.

Not sure whether this will be helpful or not, but
http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/smooth-low-noise-differentiators/
and
```
```On Thursday, April 25, 2019 at 8:35:11 PM UTC+8, Greg Berchin wrote:
>
> Not sure whether this will be helpful or not, but
> http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/smooth-low-noise-differentiators/

Thanks for that reference.
It seems the first formula was derived for a higher-order polynomial,
explaining the different coefficients.

```
```Old but interesting topic:

On Thursday, April 25, 2019 at 10:13:58 AM UTC+2, conr...@gmail.com wrote:
> I had some code computing the second difference of a sampled signal as
> (x(n-1)-2x(n)+x(n+1))/(square of interval)
> I only needed a crude estimate of the second derivative in this case.
>
> Then I came across "five point stencils"
> By this method, the second difference is
> (-x(n-2)+16x(n-1)-30n(n)+16x(n+1)-x(n+2))/12/(square of interval)

This first 5-point method is the only choice of coefficients that
gives you correct results for polynomials up to the 5th order.  In
terms of the frequency spectrum, its transfer function is a Taylor
approximation of the ideal 2nd order differentiator's transfer
function with 0 Hz as starting point.  So, it's a good approximation
around 0 Hz and continues to get worse at higher frequencies.

> And then 5-sample Savitsky-Golay formula, which seems rather different
> (2x(n-2)-x(n-1)+2x(n)-x(n+1)+2x(n+2))/7/(square of interval)

I'm certain you meant the factor -2 instead of +2 in the center for
x(n).  Then, this would also be a Savitsky-Golay filter but based on
a lower order polynomial.  It is only exact for polynomials up to the
3rd order.  The motivation to do this is to perform some kind of
noise reduction as well.  When you know your signal is approximated
well as a 3rd order polynomial but due to noise it's not, such a
filter might be useful in that it might reduce some noise.  For
example, if you know that at higher frequencies your signal is
dominated by noise, this filter probably works better than the former
filter.

If you already know your signal and noise statistics in terms of their
spectra (and maybe even their cross spectral density), there is a
better way: A spectral-domain FIR design tool that makes

conv(signal+noise, FIR)

the best approximation (in terms of RMS) to the "true 2nd order
derivative of signal".

By using an SG filter you *implicitly* assume that almost all the
energy of `signal` is concentrated around 0 Hz while `noise` is
is assumed to be white noise.

> Are either of these "better" in any sense?

Depends on your case.

sg
```