Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 332
makerofthings7 Avatar asked Dec 28 '22 02:12

makerofthings7


2 Answers

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

like image 179
Mark Lakata Avatar answered Jan 10 '23 05:01

Mark Lakata


You need both the data value V and the corresponding time T, at least for the latest data point and the one before that. The rate of change can then be approximated with Eulers backward formula, which translates into

dvdt = (V_now - V_a_moment_ago) / (T_now - T_a_moment_ago);

in C#.

like image 27
Tomas Aschan Avatar answered Jan 10 '23 05:01

Tomas Aschan