Search code examples
pythonnumpyderivative

Why my derivative is not equal to theorical value?


I make a derivative computation on sinusoidale signal. I make simple python program to test it:

signal = np.sin(time_s.to_numpy())
for index in range(1, len(signal)-1):
        first[index] = signal[index ]-signal[index -1] /(2*dt) * 100
        second[index] = signal[index+1 ] - 2*signal[index ]+signal[index -1] /(dt*dt)
        signal_display[index] = signal[index ]* 2000

enter image description here

My first derivative function should like a cosinus?


Solution

  • You are missing parentheses in the numerator of your derivatives, and you are scaling them inappropriately. They should look like this:

    first[index] = (gyroX_filtered[index] - gyroX_filtered[index - 1]) / dt 
    second[index] = (gyroX_filtered[index + 1] - 2 * gyroX_filtered[index] + gyroX_filtered[index - 1]) / dt**2