# Calculate speed from accelerometer

I am trying to use my accelerometer on my mobile device (smart watch to be specific) to calculate a persons arm swing speed.

The data returned from the accelerometer is in $m/s^2$.

Since the acceleration of a persons arm is not constant I cannot use the equation v0 + at to calculate the velocity.

I have never been good at physics so how do I calculate speed with varied acceleration?

• You have to calculate a numerical integral. In it's most simple form that's just a sum of the acceleration samples multiplied with the time step. Having said that, your accelerometer measures acceleration along three axes, which are rotating relative to an actual physical reference frame. In addition, the accelerometer can not distinguish between gravity ($-g_z$) and an acceleration of 1g towards the floor. Compensating for these things and the error components of the accelerometer is a hard computational problem. – CuriousOne Dec 14 '14 at 1:29
• @CuriousOne I realize there is going to be margins of error so I am not looking for anything extremely accurate, I am going to let the user know that it is an estimated speed – tyczj Dec 14 '14 at 1:34
• The problem is not one of small errors, it's one of a ten times larger signal dependent on the orientation of your device being overlaid to what you are trying to measure. Unless you already have code that corrects for that, the problem is quite hard. – CuriousOne Dec 14 '14 at 1:44
• @CuriousOne - is $g$ a problem? - in a lift when I go down my stomach feels the downward acceleration even when it is less than $g$. I guess the accelerometer in the phone should be able to feel that as well. What do you think? – tom Dec 14 '14 at 3:25
• @tom: The problem is not that the accelerometer can not measure gravitational acceleration, the problem is that it can't tell the difference between gravitational acceleration and an actual acceleration in the inertial system that you want the measurement to be done in. The deeper reason for that is, that the surface of the earth is NOT an inertial system, we just like to pretend that it is. And if you look at the error propagation of a one g constant acceleration overlaid vectorially on a small (0.01-0.1g) acceleration that we typically are interested in, the resulting errors are huge. – CuriousOne Dec 14 '14 at 3:29

You need to integrate acceleration to get the velocity.

$$v(t) = \int_{t=0}^{t} a. dt$$

There are a number of ways of doing this numerically.

I assume that you get these readings regularly with a spacing of $\delta t$, for example $\delta t = 100 ms$ or something like that.

About the simplest way to do it is

$$v(t) = v(0) + \sum a \times \delta t$$

where $v(t)$ is the velocity at time $t$. but there are more sophisticated ways of doing it - I will not repeat them here, but you might want to look at using Simpson's rule, which is described here.

The problem is complicated by velocity being three dimensional - so you need to integrate in each of the three dimensions x, y and z separated.

It depends how the phone gives you the information about the acceleration, but if you get $a_x$, $a_y$ and $a_z$ at regular intervals then you can do the following...

vx += ax * dt;
vy += ay * dt;
vz += az * dt;


if you get accleration as a raw number and angle then you will have to convert from I guess polar coordinates to xyz components to be able to add them up.

Total speed, $|v|$ is, of course, given by $|v|=\sqrt{v_x^2 + v_y^2 + v_z^2}$

I would, of course, try to start at $v=0$

Curious One, raises a really interesting point about $g$ - the best way to test this is to code it and try it - shake the phone and see if the velocity returns to zero when it is at rest after shaking it or moving it....

... can you post your results if you do this and try it out?

Another issue is twisting the phone and twisting the accelerometer - this would require you to think about angular acceleration etc., but the basic principles outlined here would be the same if you needed to think about angles.

• Yes the information comes in very fast, about 20 samples every 1000ms. The information from the sensor is in the form of $a_x$, $a_y$, $a_z$. I have already done some testing to see how much drift there is in the sensor when still and there is a fair amount of drift, I am going to put in a calibration of sorts to try to filter out the noise – tyczj Dec 14 '14 at 4:52

That's why when you want to integrate numerically the vertical acceleration you have to subtract 1g from the vertical acceleration you measure from the accelerometer

• Why do you need to subtract 1g? – Kyle Kanos Jul 29 '15 at 16:09
• Because 1g does not produce any "visible" velocity, but as said above, the accelerometer can't distinguish between gravity and other accelerations. So you subtract 1g knowing that any other acceleration seen apart from that one will have produced an actual velocity pointing in some direction: if the acceleration measured is less than 9.8, then the velocity is negative so pointing towards the floor, if it's more than 9.8 it will be a positive velocity pointing towards the ceiling/sky. (Conventionally the 1g vector points from the floor to the ceiling). hope i've been clear enough! – Simone Jul 30 '15 at 10:54