0
$\begingroup$

In the following snippet, I am differentiating a sine wave using the central difference equation, first through misc.derivative and then through convolution with the kernel [1/2, 0, -1/2]

import numpy as np
import scipy.misc as misc
import scipy.signal as sig
ncycles = 5.0
nsamples = 500.0
x = np.linspace(0,ncycles*2*np.pi,nsamples)
dydxmisc = misc.derivative(np.sin,x,dx=0.1)
dydxconv = sig.convolve(np.sin(x), [0.5,0.0,-0.5], mode='same')

As shown in the plots below, misc.derivative produces the expected output when dx is small enough, but when using convolution the derivative is attenuated. enter image description here

The attenuation changes based on number of cycles and number of samples and is a factor of pi but I cannot figure out what the exact relationship is. Can anyone explain why the attenuation happens and what the relationship is? In other words, how do I have to scale the kernel in order to get a derivative of the correct dimension?

$\endgroup$
1
$\begingroup$

Your filter forgets that in the discrete differences equation, you divide the difference in value by the difference in $x$; so, you'd need to multiply the result with ncycles*2*np.pi/nsamples to get the same.

Other than that, you're not compensating for the group delay of your FIR filter, either, so you want to shift the result by half the filter length, too.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.