# Computes velocity from sampled position

My system is :

A step by step wheel returns its position each 150ms. I want to pilot an horizontal infinite scroll bar with the wheel. The scroll bar needs velocity as input to move. Moreover the faster the wheel rotates the farthest the scroll must move. That means that if I turn the wheel one turn quickly, the bar must go further if I had turned the wheel one turn slowly.

How can I compute the right velocity to give as input to the scroll bar with sampled position of the wheel ?

You have to estimate a velocity based on a regularly sampled signal. The velocity is given by the derivative on a continuous time signal. The approximation of this value with discrete data is a long-lasting topic. @Juancho is correct with the proposal of the $2$-point difference scheme.

As the sampling is never perfect, and $x[i]-x[i-1]$ is not symmetric. So if you do not need fully causal results, there exists more symmetric derivative schemes, such as an average of forward and backward differences: $\frac{1}{2}(x[i]-x[i-1])+(x[i+1]-x[i]) = \frac{x[i+1]-x[i-1]}{2}$. Other schemes can be found in Numerical differentiation formulae (see here for a discussion).

If you need to compute forward derivatives for real-time purposes, a paper (in French, but formulae are readable) is Estimation par maximum de vraisemblance de la dérivée d'un signal bruité, with cares about noise issues.

Take the difference between successive measurements; this difference is proportional to the velocity.

For example, if $x[i]$ is the i-th measurement in degrees, then $(x[i]-x[i-1]/0.15$ will give you the rotation speed in degrees per second.

You need to take care of the origin position (where the measurement will jump between 359 and 0 assuming measurement in degrees).

So if the difference is a big positive value, then subtract 360. Likewise, if the difference is a big negative value, add 360.

For example, if $x[i-1] = 350$ and $x[i]=5$, the difference $x[i]-x[i-1]$ will give -345. Add 360 to get the correct value of +15 degrees. Only after this correction you divide by 0.15s to give the value in degrees per second (if you need that).

This procedure assumes you never rotate more than 180 degrees in one sample time (about 3.5 turns per second).