# How to calculate the horizontal acceleration?

I am trying to measure the acceleration and deceleration of a car by using an 3 axis accelerometer which is build in an iPhone 5s. Placing the iPhone flat inside the car with the y axis to the top of the car this works pretty ok. But now I want to be able to place the iPhone basically arbitrary inside the car. (like in the picture below)

(source: everythingicafe.com)

Is there a way to calculate only the horizontal acceleration of the device in such a placement? (I am aware that you won't be able to differ a directional acceleration but that's sufficent for my project. If I am wrong, I would be glad to know if it would work)

To do the calculations the following data is available

• User Acceleration (uX,uY,uZ) - only the acceleration the user imparts to the device
• the total acceleration of the device (tX,tY,tZ) - user acceleration plus gravity
• Gyro Data (gX,gY,gZ) - the device’s rate of rotation around it's axes
• the attitude of the device (quaternion, rotation matrix, (pitch,roll,yaw))

• Measure the total acceleration $a_x, a_y, a_z$ when the car is stationary, and you can calculate the rotation matrices to convert between the phone axes and the car axes. Then you can convert the readings from the phone to accelerations relative to the car. – John Rennie Aug 6 '14 at 11:21
• nb if you do write an app to do this coordinate correction, see if you can't get it published in the iStore. I can think of a lot of uses for such a gizmo. Keep in mind there are already "level" apps which determine the static rotation of a phone, so you could integrate with them. – Carl Witthoft Aug 6 '14 at 12:40
• Thanks @JohnRennie for answering. I am pretty sure your approach would work as well. But because it was easier for me I implemented Floris approach. – riik Aug 6 '14 at 21:48

If you don't care about the direction of the horizontal acceleration, the answer is yes.

When the car is stationary (user acceleration very small, below some limit you define for the RMS of the three axes) you measure the vector $\vec g$ for the total acceleration - this is "down".

Now during motion you find the user acceleration perpendicular to this vector with these steps:

Normalize $\vec g$ to unit length: $\vec n$

Take dot product of unit gravity and user acceleration: $d=\vec n \cdot \vec u$

Subtract vertical component from user acceleration: $\vec h = \vec u - d \vec n$

Finally take the magnitude of this answer (square root of sum of squares of components) for the total horizontal acceleration.

To separate out the acceleration into lateral (from car turning) and linear (accelerate/brake) you would have to do a similar procedure to find the remaining orientation by looking for horizontal acceleration when there is no corresponding rotation - this tells you which way the phone is facing.

• Thanks a lot! I just implemented it the way you described it and it works! – riik Aug 6 '14 at 21:43
• I am now trying to figure out how to differ between lateral and linear acceleration. Based on GPS I can detect at what point in time there is only a linear acceleration happening. It would be great if you could tell me what calculations are required to then obtain the linear acceleration. – riik Aug 30 '14 at 15:41