-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilterplot.py
More file actions
145 lines (115 loc) · 2.87 KB
/
Copy pathfilterplot.py
File metadata and controls
145 lines (115 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""this file moves through the sensor data and rejects deltas larger than
a given threshold"""
import matplotlib.pyplot as plt
import numpy as np
import sys
threshold = 100
"""given a row from a csv return a list of the elements in the row."""
def parseRow(row):
elements = []
word = ''
for c in row:
if c == ',':
elements.append(int(word))
word = ''
elif c == '\n':
elements.append(int(word))
word = ''
else:
word = word + c
return elements
def quietGetElement(N, i):
try:
n = N[i]
except:
n = None
if i < 0:
n = None
return n
def outsideDelta(a, b, d):
if abs(a-b) > d:
return True
return False
def getRow(array, i):
return array[i]
def getColumn(array, j):
column = []
for row in array:
column.append(row[j])
return column
"""return true if the value n in N is seperated
from it's neighbors by more than t."""
def getOutliers(N, d):
if len(N) < 3:
print "you need more elements to filter."
return None
outliers = []
lastn = None
n = None
nextn = None
for i in range(len(N)):
lastn = quietGetElement(N, i-1)
n = quietGetElement(N, i)
nextn = quietGetElement(N, i+1)
if lastn == None:#we are at the first element
if outsideDelta(n, nextn, d):
outliers.append((lastn, n, nextn, i))
elif nextn == None:#we are at the last element
if outsideDelta(n, lastn, d):
outliers.append((lastn, n, nextn, i))
else:#we are in the middle of the list
if outsideDelta(n, lastn, d) and outsideDelta(n, nextn, d):
outliers.append((lastn, n, nextn, i))
return outliers
path = 'logfile.csv'
if len(sys.argv) >= 2:
path = sys.argv[1]
column = 0
if len(sys.argv) >= 3:
column = int(sys.argv[2])
with open(path, 'rw') as file:
lines = []
for line in file:
items = parseRow(line)
lines.append(items)
if len(lines[0]) < column:
print "the requested column does not exist."
sys.abort()
data = []
for line in lines:
data.append(line)
filterColumn = getColumn(data, column)
outliers = getOutliers(filterColumn, 200)
for outlier in outliers:
mean = ((outlier[0] + outlier[2])/2, outlier[3])
data[mean[1]][column] = mean[0]
t = getColumn(data, 0)
lidar = getColumn(data, 1)
big = getColumn(data, 2) #this is the raw sensor data
small = getColumn(data, 3) #this is the filtered sensor data
for i in range(len(big)):
b = big[i]
b = (b-250)*(980/430)
big[i] = b
for i in range(len(small)):
s = small[i]
s = (s-250)*(980/430)
small[i] = s
delta = []
for i in range(len(big)):
d = big[i] - small[i]
delta.append(d)
movingAve = []
for i in range(len(big)):
s = 0
window = 5
for j in range(window):
if i-window/2 > 0 and i-window/2 < len(big):
n = big[(i-window/2) + j]
else:
n = big[i]
movingAve[i] = s
#add the range that the manufacturer quotes
#add more options for filtering
plt.plot(t, big, 'y', t, small, 'b') #delta, 'r'
plt.show()