For a project I'm working on I'm using an accelerometer which measures acceleration in 3 directions, x, y and z.

My question is: How can I calculate the total acceleration in a certain direction from these 3 values?

Considering this simple graph layout: enter image description here

My initial idea is:

  • Take the sqrt of (x^2 + z^2) to calculate the resulting value in the zx plane.
  • Take this value, square it and add y^2, take the square root of that
  • Final equation: Sqrt(y^2 + Sqrt(x^2 + z^2))

Is this correct? On some sites I see x^2 + y^2 + z^2 being used, but I don't know if that's right and why it's right.

EDIT: I just figured that taking the Sqrt of (x^2 + z^2) and squaring it just results back in x^2 + z^2, so that's why I can use x^2 + y^2 + z^2.

Another thing: Do I have to normalize for gravity? I think I do, but how would I go about this? Do I need to know the exact position and tilt of my device as it will be in the end?


Your procedure gives:

$$ a_{xz} = \sqrt{a_x^2 + a_z^2} $$


$$ a_{total} = \sqrt{a_y^2 + a_{xz}^2} $$

but if you substitute for $a_{xz}$ in the second equation you get:

$$ a_{total} = \sqrt{a_y^2 + (\sqrt{a_x^2 + a_z^2})^2} = \sqrt{a_y^2 + a_x^2 + a_z^2}$$

so you don't need to split the calculation into two steps.

Your accelerometer may already exclude the acceleration due to gravity. If it doesn't then yes you need to use the inclination to work out the three components of gravity then subtract them from $a_x$, $a_y$ and $a_z$. It's hard to say exactly how to do this without knowing how your phone reports it's inclination.

response to comment:

Suppose you have your device held flat so $a_z$ = -1. Now move the device downwards at and angle of $\theta$ as shown below:


Assuming it's moving in the $xz$ plane the value of $a_z$ will be decreased a bit and the value of $a_x$ will increase from zero. Suppose you're applying an acceleration to the phone of $2g\space cos(\theta)$ - you'll see why i've chosen this value in a moment. Now the values of $a_x$ and $a_z$ are:

$$ a_x = 2g\space cos\theta \space sin\theta $$

$$ a_z = g - 2g \space cos^2 \theta $$

You now calculate $a_{total}$ by just squaring and adding as we discussed above to get:

$$a_{total}^2 = 4g^2 \space sin^2\theta \space cos^2\theta + g^2 + 4g^2 \space cos^4\theta - 4g^2 \space cos^2\theta $$

and a bit of rearrangement gives:

$$a_{total}^2 = g^2 + 4g^2 \space cos^2\theta \left( sin^2\theta + cos^2\theta - 1\right) $$

and because $sin^2\theta + cos^2\theta = 1$ the quantity in the brackets is zero so you end up with:

$$a_{total}^2 = g^2 $$

that is:

$$a_{total} = g $$

which is the same as when the phone is stationary. So it's possible to be accelerating the phone and still have the total acceleration come out as $g$ ($g$ = -1 in the phone's units). That's why just subtracting one isn't a reliable way to tell if the phone is accelerating.

  • $\begingroup$ Ok, using this calculation I get a final G-value of around 1 when the device isn't moving, so I just subtract 1 to get the final acceleration in G. Then I normalize it to m/s2 and it sits at around 0.24 max, but I guess this is due to limitations of the device. Seems to work. $\endgroup$ – Davio Oct 25 '12 at 8:21
  • $\begingroup$ Subtracting 1 from $a_{total}$ only works if the acceleration is in the same direction as gravity i.e. upwards. For accelerations in other directions you need to work out the components of the gravitational acceleration and subtract them from $a_x$ etc. $\endgroup$ – John Rennie Oct 25 '12 at 8:26
  • $\begingroup$ Okay and how would I do that? Remember, all I have are the values from these 3 axes. $\endgroup$ – Davio Oct 25 '12 at 8:27
  • $\begingroup$ You need the info from the inclinometer, which will tell you what angle your $x$, $y$ and $z$ axes are. Actually I'm assuming the $x$, $y$ and $z$ axes are always relative to the phone, and maybe they aren't. If you hold the phone as still as possible but rotate it do the values of $a_x$, $a_y$ and $a_z$ change as you rotate the phone? $\endgroup$ – John Rennie Oct 25 '12 at 8:34
  • $\begingroup$ Yes, these values change as I tilt the device (it's not a phone). When I lay it flat on the table, Ax, Ay are close to 0 and Az = -1 (gravity pulling down). So when I use the formula mentioned above I think I'm getting close to the actual value. The thing is that I need to detect if the device has a total acceleration of > 2 m/s2 for a period of > 3 seconds. If so, the device is considered to be moving. $\endgroup$ – Davio Oct 25 '12 at 8:37

protected by Qmechanic Mar 12 '14 at 22:55

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.