# Double Integrating acceleration data to obtain position: 2 Problems

I have a data sample from an accelerometer from my phone (pretty bad accelerometer though). I'm trying to double integrate it in order to obtain the position as a function of time. I'm using a program in Python3 to integrate it, here is the code:

nm = input("Filename: ")

fil1 = open(nm,"r")
i = 0
s = 0.0
fil2 = open("int1.dat","w")

for line in fil1:
xi,yi = line.split()
if i == 0:
x = float(xi) ; y = float(yi)
if i > 0:
s = s  + (float(xi)-x)*(float(yi)+y)/2
x = float(xi)
y = float(yi)
i = i + 1
fil2.write("{} {} \n".format(xi,s))
fil2.close()
#I just integrated the file. Now, i will integrate it again:
fil3 = open("int1.dat","r")
s = 0.0
i = 0
fil4 = open("int2.dat","w")
for line in fil3:
xi,yi = line.split()
if i == 0:
x = float(xi) ; y = float(yi)
if i > 0:
s = s  + (float(xi)-x)*(float(yi)+y)/2
x = float(xi)
y = float(yi)
i = i + 1
fil4.write("{} {} \n".format(xi,s))
fil3.close()
fil4.close()


As you can see i'm approximating the area using trapezes.

The acceleration data was obtained in a movement as follows:

1. Started at a position that i defined as $x=0$.
2. Moved in straight line towards the end of an table, and stopped.
3. Waited a few seconds.
4. Returned to the original position

So, the graph of position x time should me something similar to a "hill": it goes up, than it goes down. It's value $x(t)$ at the peak would represent the total length of the table.

However, things are not working quite well. I took out the noise by removing the mean value of all $|a(t)| < 0.15$. However, it still did not sum up well. The graphs get pretty bad. So, here is my first problem: The noise stays too big when i integrate it. And so, my first question is: How can i reduce the noise?

Anoter problem that is happening and that i'm finding odd is that my position doesn't "go and come back" to where it started. It starts in $0$, goes to $-7$ (which is approximately the length of the table) and then, instead of coming back to $0$, it goes all the way down to $-14$. Why is this? I can't seem to find any particular reason for the integral not reckognizing the fact that i am coming backwards.

I tried to upload the graphics, but i couldn't right now. I will upload them as soon as i can. Much appreciate.

• Is it possible that the accelerometer is reporting absolute values? Can you give a link to the documentation? Also, if there is noise of value $\epsilon$ at time $t$, it will cause position to be off by $\epsilon(T-t)$ at time $T$, so you would expect errors to grow with time ($\propto T^{3/2}$), and that might be a limitation of your setup. – Kirill Jul 4 '17 at 23:52
• I think that the relative error remains constant. (This means that the absolute error increases with the data). As for absolute value, no: i've got both negative and positive acceleration data. – embedded_dev Jul 5 '17 at 2:42
• You may want to watch the following Google Tech Talk: Sensor fusion on Android devices: a revolution in motion processing (2010) – Rodrigo de Azevedo Jul 5 '17 at 13:57