Step size in numerical differentiation - Robotics Stack Exchange most recent 30 from robotics.stackexchange.com 2020-01-25T23:20:39Z https://robotics.stackexchange.com/feeds/question/9756 https://creativecommons.org/licenses/by-sa/4.0/rdf https://robotics.stackexchange.com/q/9756 1 Step size in numerical differentiation donald https://robotics.stackexchange.com/users/9899 2016-05-03T06:54:57Z 2016-05-03T08:05:57Z <p>I get position information and a corresponding timestamp from a motion tracking system (for a rigid body) at 120 Hz. The position is in sub-millimeter precision, but I'm not too sure about the time stamp, I can get it as floating point number in seconds from the motion tracking software. To get the velocity, I use the difference between two samples divided by the $\Delta t$ of the two samples:</p> <p>$\dot{\mathbf{x}} = \dfrac{\mathbf{x}[k] - \mathbf{x}[k-1]}{t[k]-t[k-1]}$.</p> <p>The result looks fine, but a bit noisy at times. A realized that I get much smoother results when I choose the differentiation step $h$ larger, e.g. $h=10$:</p> <p>$\dot{\mathbf{x}} = \dfrac{\mathbf{x}[k] - \mathbf{x}[k-h]}{t[k]-t[k-h]}$.</p> <p>On the other hand, peaks in the velocity signal begin to fade if I choose $h$ too large. Unfortunately, I didn't figure out why I get a smoother signal with a bigger step $h$. Does someone have a hint? Is there a general rule which differentiation step size is optimal with respect to smoothness vs. "accuracy"?</p> <p>This is a sample plot of one velocity component (blue: step size 1, red: step size 10):</p> <p><a href="https://i.stack.imgur.com/3aaOU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3aaOU.png" alt="Sample plot of step size 1 vs. step size 10."></a></p> https://robotics.stackexchange.com/questions/9756/-/9757#9757 1 Answer by Ramesh-X for Step size in numerical differentiation Ramesh-X https://robotics.stackexchange.com/users/1851 2016-05-03T07:59:30Z 2016-05-03T08:05:57Z <p>This answer valid only if $\Delta{t} = \mathbf{t}[k] - \mathbf{t}[k-1]$ is a constant. Then you can rewrite your equation as: $$\dot{\mathbf{x}} = \dfrac{\mathbf{x}[k] - \mathbf{x}[k-1]}{\Delta{t}}$$</p> <p>Consider:</p> <p>$$\dot{\mathbf{x}}_l = \dfrac{1}{h}\sum_{i=1}^{h}\dot{\mathbf{x}_i} = \dfrac{(\mathbf{x}[k] - \mathbf{x}[k-1])+(\mathbf{x}[k-1] - \mathbf{x}[k-2])+\dotsb+(\mathbf{x}[k-h+1] - \mathbf{x}[k-h])}{h\Delta{t}} = \bigg(\dfrac{\mathbf{x}[k] - \mathbf{x}[k-h]}{h\Delta{t}}\bigg)$$ </p> <p>$$h\Delta{t} = \mathbf{t}[k] - \mathbf{t}[k-h]$$</p> <p>Here $\dot{\mathbf{x}}_i$ is the $i^{th}$ sample of the reading and passing it through a moving average filter (which is a low pass filter) you can obtain $\dot{\mathbf{x}_l}$. So $\dot{\mathbf{x}_l}$ is smooth as it is a low pass signal. When you increase the value of $h$ you can minimize the bandwidth of $\dot{\mathbf{x}_l}$. So when you increase the value of $h$ the result is getting more smoother. So peaks begin to fade(Peaks means high frequency components).</p> <p>As I know there isn't a generalize way to determine $h$ to get the result smoother and accurate. You have to choose appropriate $h$ by a trial and error or if you know the transfer function of the sensor, you can use that to determine an appropriate value for $h$. </p>