Finding the first and second derivative of a spline (only from data, function unknown)

410 Views Asked by At

I have received data values for a spline (which was already fit to some ndvi data). I just have only the data points of the spline and do not know the function that the spline follows. My goal is to find the first and second derivatives of the fitted spline. After reading up several posts most suggests, using a finite difference method approximation to calculate the derivatives of the spline data points.

This can be achieved using pythons (numpy package) diff method which calculates the difference between successive data points. (out[i] = a[i+1] - a[i]) where a is an array. From what I presume the derivative should be the difference in successive points divide by delta x (which is one day in this situation).

ndvi_vals = df['fitted.values'].to_numpy()
successive_diff = np.diff(ndvi_vals)
#deravative = successive_diff / 1 day
ndvi_derv = successive_diff / 1 

This part of the code calculates this. However, when I plot the original spline and its derivative, it's not what I expected. The first derivative has a far smaller amplitude and is well below the spline. It doesn't, for example, look similar to how a sin function and its derivative (cos function) would look like. Any ideas about what I may be doing wrong?.

Access to the data and jupyter notebook is here : https://github.com/imantha-das/GEE/blob/master/Finding%20Deravatives/Finding_Derivatives.ipynb