Search code examples
mathderivative

How does one calculate the rate of change (derivative) of streaming data?


I have a stream of data that trends over time. How do I determine the rate of change using C#?

It's been a long time since calculus class, but now is the first time I actually need it (in 15 years). Now when I search for the term 'derivatives' I get financial stuff, and other math things I don't think I really need.

Mind pointing me in the right direction?


Solution

  • If you want something more sophisticated that smooths the data, you should look into a a digital filter algorithm. It's not hard to implement if you can cut through the engineering jargon. The classic method is Savitzky-Golay

    If you have the last n samples stored in an array y and each sample is equally spaced in time, then you can calculate the derivative using something like this:

    deriv = 0
    coefficient = (1,-8,0,8,-1)
    N = 5 # points
    h = 1 # second
    for i range(0,N):
       deriv += y[i] * coefficient[i]
    deriv /= (12 * h)
    

    This example happens to be a N=5 filter of "3/4 (cubic/quartic)" filter. The bigger N, the more points it is averaging and the smoother it will be, but also the latency will be higher. You'll have to wait N/2 points to get the derivative at time "now".

    For more coefficients, look here at the Appendix

    https://en.wikipedia.org/wiki/Savitzky%E2%80%93Golay_filter