stulp / dmpbbo

Python/C++ library for Dynamical Movement Primitives and Black-Box Optimization
GNU Lesser General Public License v2.1
226 stars 89 forks source link

what is the principle of the function "diffnc" in /python/dmp/Trajectory.py #50

Closed RanCao2018 closed 5 years ago

RanCao2018 commented 5 years ago

Hello, anyone can help to explain the principle of the function "diffnc" in /python/dmp/Trajectory.py? I guess that it is used to calculate the difference derivative, but I cannot understand how it works. Do there have any related expansion or link? Thank you😀

stulp commented 5 years ago

Numerical differentiation can be implemented as linear convolution. In Matlab for instance, diff(X) is basically the same as conv(X,[1,-1]). If you know the sample interval dt, it would be diff(X)/dt and conv(X,[1,-1]/dt)

https://bugra.github.io/work/notes/2014-06-13/first-second-derivative-convolution-quadratic-fitting-and-all-that-via-MCMC/#Taking-Derivatives-through-convolution

diffnc (as originally implemented by Stefan Schaal in Matlab, see the comments) works the same, but also smooths the signal by taking both neighbouring samples into account, i.e. conv(X,[1,0,-1]/2*dt)

diffnc is essentially a "minimal" version of this https://en.wikipedia.org/wiki/Savitzky%E2%80%93Golay_filter with m=3 and k=1.

In Python it would perhaps be more sensible to use numpy.gradient. But I copied diffnc from previous Matlab code I was using to ensure the result would be the same.

RanCao2018 commented 5 years ago

I got it. Thank you