I have a list of positions as a function of time data and want to apply the Savitzky–Golay method to find the first derivative. What is the approach I should take. I already know how to apply the filter.


closed as unclear what you're asking by KraZug, Daniel Lichtblau, Henrik Schumacher, Bill Watts, m_goldberg Feb 4 at 22:37

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ I have still not clue which package you use and I have the strong feeling that most (if not all) other users are in the same position... $\endgroup$ – Henrik Schumacher Feb 4 at 20:08
  • $\begingroup$ Possible duplicate of Savitzky-Golay Filter to smooth noisy data $\endgroup$ – KraZug Feb 4 at 20:10
  • $\begingroup$ I guess i don't know how to frame the question.Ignore the package part. I already know how to apply filters and just want to know how to get derivative. What i'm basically asking is what's the approach to take in applying Savitzky–Golay method to find the first derivative. $\endgroup$ – Maine Feb 4 at 20:12
  • 3
    $\begingroup$ @Maine Please add a minimal example to your question - copy-pasteable code that will work on any given computer - or we simply won't be able to help you. $\endgroup$ – Carl Lange Feb 4 at 20:53

This may be what you want. First I generate a sine wave with some noise.

data = Table[{t, Sin[2 π 123 t] + RandomReal[{-0.2, 0.2}]}, {t, 0,
    0.02, 0.0005}]; Dimensions@data

{41, 2}

 ListLinePlot[data, Mesh -> All],
 Plot[Sin[2 π 123 t], {t, 0, 0.02}, PlotStyle -> Purple]

Mathematica graphics

Now we generate the kernel of a derivative filter for Savitzky Golay smoothing.

k = SavitzkyGolayMatrix[{5}, 3, 1];

Look this up in Help. We now apply this to the data using ListConvolve

c = ListConvolve[k, data[[All, 2]]];

Here I have lost 5 points from the start and end of the data due to the convolution process. Putting this back with the time base and plotting the analytical derivative of the sin curve (which is cos) we can see if the smoothed derivative has worked.

 ListLinePlot[Transpose[{data[[All, 1]][[6 ;; -6]], c}], Mesh -> All],
 Plot[-0.0005 2 π 123 Cos[2 π 123 t], {t, 0, 0.2}, 
  PlotStyle -> Purple]

Mathematica graphics

This is a reasonable calculation of the derivative. I had to put in the time increment because that is not included in the filter. I am not sure why I needed the negative sign. I will have to experiment more.

Hope that helps.


Not the answer you're looking for? Browse other questions tagged or ask your own question.