# Getting position from an accelerometer on an Android phone

I know that integrating acceleration twice will give me position (acceleration-->velocity-->position) but how can I do all this when I all I have are a set of data points (ex: 1 second = some # m/s^2); how can I take these data points and integrate them?

Does this have something to do with numerical integration?

-
Yes, most certainly: pure integration of data is hazardous for the reasons given in my answer and practically illustrated by George GOrdon's answer. – WetSavannaAnimal aka Rod Vance May 28 '14 at 12:51

Even if you also have gyroscopes, the errors from them and from the accelerometers will be too large when integrated over any useful period of time and the results for position will have huge errors.

What you would be constructing is an inertial navigation system (INS). This requires highly accurate instruments such as you find in the INS used in military and other aircraft. An accurate INS is much bigger than you could fit in a smartphone - even those ridiculously large ones they are marketing nowadays!

Even a military-grade INS has errors and its data is usually mixed with GPS data to provide a highly accurate and resilient navigation system. This integrated system is also used in commercial aircraft.

-

I'd like to add a different way to state George S. Gordon's answer, which is an excellent practical way to say that pure integration of a timeseries can be a hazardous thing to do because the variance of the absolute error, given enough time, will surpass any positive value, no matter how big. Thus his answer illustrates a point of considerable importance to data processing and experimental physics.

What this means is that, depending on your application, your results will be useful only for a finite time, after which you must recalibrate your calculation somehow using knowledge of the position gleaned from some independent measurement.

To illustrate further: any general data processing algorithm applied to a timeseries often means that you are effectively calculating your result by a recurrence relationship; at the $k^th$ step we estimate our "result" $Y(k)$ from our raw data $X(k)$ where $X$ and $Y$ are generally column vectors:

$$Y(k+1) = f(Y(k),\,X(k))\tag{1}$$

where $f:\mathbb{R}^N\times \mathbb{R}^N\to \mathbb{R}^N$ is some general nonlinear function. In reality, there is noise $N(k)$ added to the raw data, so you instead compute:

$$\tilde{Y}(k+1) = f(\tilde{Y}(k),\,X(k)+N(k))\tag{2}$$

Sometimes, and you should always strive to arrange your data processing so that this is so if you can, the above recurrence can be stable. This means that no matter how many steps you go into the data processing, the variance of the absolute estimation error $\tilde{Y}-Y$ stays bounded. For example, if our estimator is a linear recurrence relationship:

$$Y(k+1) = A\,Y(k)+B\,X(k)\tag{3}$$

where $A$ is a square matrix whose every eigenvalue lies strictly inside the unit circle on the complex plane then the error variance is $\tilde{N}^T\, C^T\, C \,\tilde{N}$ where

$$C = B + A B + A^2 B + A^3 B + \cdots\tag{4}$$

and $\tilde{N}$ is a vector of noise standard deviations so that the variance converges to the finite value $\tilde{N}^T\,B^T\,((\mathrm{id} - A)^{-1})^T (\mathrm{id} - A)^{-1}\,B\,\tilde{N}$. We NEVER need to recalibrate our estimator in this case, if we are happy with this bounded variance.

But in your case, this is not so. You are doing a double integration. Your state equations, discretised, are:

$$\begin{array}{lcl}v(k) &=& v(k-1) + a(k) \,\Delta\\x(k) &=& x(k-1) + v(k)\, \Delta\end{array}\tag{5}$$

where $a,\,v,\,x$ are, naturally, the acceleration, velocity and position and $\Delta$ your discretisation interval. The $a(k)$ are your "raw data" and your estimator is as in equation (3) with $A = \mathrm{id}$ and $B(k) = \left(\begin{array}{c}1\\1\end{array}\right)\,\Delta\,a(k)$. You can see that the series (4) grows without bound, hence the need for recalibration at regular intervals.

-

Theoretically these calculations are possible. But it gets complicated by the fact that each of the three accelerometers (one each in an axis perpendicular to the other two) are constantly changing in orientation. You could track orientation in a similar way with data from electronic gyros. I'm not sure if android devices include gyros. You are also limited by the resolution of your data and errors would creep in rather quickly limiting the accuracy for fairly short durations.

-