add graph_imu.py
[protos/imu.git] / graph_imu.py
1 import sys, re
2 import matplotlib
3 matplotlib.use('QT4Agg')
4 import numpy as np
5 import matplotlib.pyplot as plt
6
7 FLOAT = "([-+]?[0-9]*\.?[0-9]+)"
8 INT = "([-+]?[0-9][0-9]*)"
9
10 FILTER = 1
11
12 tab_t = []
13 tab_g1 = []
14 tab_g2 = []
15 tab_g3 = []
16 tab_a1 = []
17 tab_a2 = []
18 tab_a3 = []
19 tab_m1 = []
20 tab_m2 = []
21 tab_m3 = []
22
23 f = open(sys.argv[1])
24 while True:
25     l = f.readline()
26     if l == "":
27         break
28
29     regex = "%s.*gyro\s*%s\s*%s\s*%s\s*"%(INT, FLOAT, FLOAT, FLOAT)
30     regex += "accel\s*%s\s*%s\s*%s\s*"%(FLOAT, FLOAT, FLOAT)
31     regex += "magnet\s*%s\s*%s\s*%s\s*"%(FLOAT, FLOAT, FLOAT)
32
33     m = re.match(regex, l)
34     if m:
35         t, g1, g2, g3, a1, a2, a3, m1, m2, m3 = map(lambda x: float(x), m.groups())
36         tab_t.append(t)
37         tab_g1.append(g1)
38         tab_g2.append(g2)
39         tab_g3.append(g3)
40         tab_a1.append(a1)
41         tab_a2.append(a2)
42         tab_a3.append(a3)
43         tab_m1.append(m1)
44         tab_m2.append(m2)
45         tab_m3.append(m3)
46         print t, g1, g2, g3, a1, a2, a3, m1, m2, m3
47
48 def mean(tab, n):
49     ret = []
50     for i in range(len(tab)-n):
51         s = reduce(lambda x,y:x+y, tab[i:i+n], 0)
52         ret.append(s/n)
53     return ret
54
55 tab_a1 = mean(tab_a1, FILTER)
56 tab_a2 = mean(tab_a2, FILTER)
57 tab_a3 = mean(tab_a3, FILTER)
58 tab_g1 = mean(tab_g1, FILTER)
59 tab_g2 = mean(tab_g2, FILTER)
60 tab_g3 = mean(tab_g3, FILTER)
61 tab_m1 = mean(tab_m1, FILTER)
62 tab_m2 = mean(tab_m2, FILTER)
63 tab_m3 = mean(tab_m3, FILTER)
64
65 # line, = plt.plot(tab_t[:-FILTER], tab_a1, 'r-')
66 # line, = plt.plot(tab_t[:-FILTER], tab_a2, 'g-')
67 # line, = plt.plot(tab_t[:-FILTER], tab_a3, 'b-')
68
69 line, = plt.plot(tab_t[:-FILTER], tab_g1, 'r-')
70 line, = plt.plot(tab_t[:-FILTER], tab_g2, 'g-')
71 line, = plt.plot(tab_t[:-FILTER], tab_g3, 'b-')
72
73 # line, = plt.plot(tab_t[:-FILTER], tab_m1, 'r-')
74 # line, = plt.plot(tab_t[:-FILTER], tab_m2, 'g-')
75 # line, = plt.plot(tab_t[:-FILTER], tab_m3, 'b-')
76
77
78 #dashes = [10, 5, 100, 5] # 10 points on, 5 off, 100 on, 5 off
79 #line.set_dashes(dashes)
80
81 plt.show()
82
83