user3123955 - 1 year ago 156
Python Question

Numpy or SciPy Derivative function for non-uniform spacing?

I was wondering if numpy or scipy had a method in their libraries to find the numerical derivative of a list of values with non-uniform spacing. The idea is to feed in the timestamps that correspond to the values and then for it to use the timestamps to find the numerical derivative.

You can create your own functions using numpy. For the derivatives using forward differences (edit thanks to @EOL, but note that NumPy's `diff()` is not a differentiate function):

``````def diff_fwd(x, y):
return np.diff(y)/np.diff(x)
``````

"central" differences (it is not necessarily central, depending on your data spacing):

``````def diff_central(x, y):
x0 = x[:-2]
x1 = x[1:-1]
x2 = x[2:]
y0 = y[:-2]
y1 = y[1:-1]
y2 = y[2:]
f = (x2 - x1)/(x2 - x0)
return (1-f)*(y2 - y1)/(x2 - x1) + f*(y1 - y0)/(x1 - x0)
``````

where `y` contains the function evaluations and `x` the corresponding "times", such that you can use an arbitrary interval.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download