-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean.py
168 lines (138 loc) · 5.07 KB
/
clean.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import numpy as np
from scipy.signal import savgol_filter
import matplotlib.pyplot as plt
import sys
import scipy.fft
if __name__ == '__main__':
if len(sys.argv) > 1:
# Check if command line argument is "-D"
if sys.argv[1] == "-D":
filename = "drift.txt"
else:
filename = str(sys.argv[1]) + ".txt"
# Load data from file
data = np.loadtxt("output/" + filename, dtype=str)
x = data[:, 0]
y = data[:, 1]
ynew = []
# Process data points
for i in range(len(y)):
if y[i] != 'None':
d = [float(x[i]), float(y[i])]
ynew.append(d)
else:
k = True
p = 1
while k:
if y[i + p] != 'None':
d = [float(x[i]), (float(ynew[-1][1]) + float(y[i + p])) / 2]
ynew.append(d)
k = False
else:
p += 1
# Sort data points by x-coordinate
ynew = sorted(ynew)
x = [int(i[0] * 10000) for i in ynew]
y = [i[1] for i in ynew]
# Write processed data to file
f = open("output/" + filename, "w")
for i in ynew:
f.write(str(i[0]) + " " + str(i[1]) + "\n")
f.close()
# Apply lowpass filter to y-values
b, a = scipy.signal.butter(3, 0.005, 'lowpass')
yn = scipy.signal.filtfilt(b, a, y, method="gust")
# Calculate absolute difference between original and filtered y-values
yfil = np.absolute(y - yn)
f = [[], []]
p = 0
# Filter out data points with large absolute difference
for i in range(len(y)):
if np.absolute(y[i] - yn[i]) < 100:
f[0].append(float(x[i] / 10000))
f[1].append(y[i])
else:
p += 1
print(len(x), p)
# Plot original data points
plt.scatter(x, y, c="black", s=0.2, alpha=0.8)
plt.show()
plt.clf()
# Plot filtered data points
plt.plot(f[0], f[1], 'k', alpha=0.9, linewidth=0.1)
# Apply another lowpass filter to filtered y-values
b, a = scipy.signal.butter(3, 0.01, 'lowpass')
y2 = scipy.signal.filtfilt(b, a, f[1], method="gust")
# Apply Savitzky-Golay filter to filtered y-values
y1 = savgol_filter(f[1], 301, 4)
# Plot filtered y-values
plt.plot(f[0], y2, 'y--')
plt.grid(axis='y')
plt.show()
plt.clf()
else:
# If no command line argument is provided, open file dialog to select file
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
data = np.loadtxt(file_path, dtype=str)
x = data[:, 0]
y = data[:, 1]
ynew = []
# Process data points
for i in range(len(y)):
if y[i] != 'None':
d = [float(x[i]), float(y[i])]
ynew.append(d)
else:
k = True
p = 1
while k:
if y[i + p] != 'None':
d = [float(x[i]), (float(ynew[-1][1]) + float(y[i + p])) / 2]
ynew.append(d)
k = False
else:
p += 1
# Sort data points by x-coordinate
ynew = sorted(ynew)
x = [int(i[0] * 10000) for i in ynew]
y = [i[1] for i in ynew]
# Write processed data to file
f = open(file_path, "w")
for i in ynew:
f.write(str(i[0]) + " " + str(i[1]) + "\n")
f.close()
# Apply lowpass filter to y-values
b, a = scipy.signal.butter(3, 0.005, 'lowpass')
yn = scipy.signal.filtfilt(b, a, y, method="gust")
# Calculate absolute difference between original and filtered y-values
yfil = np.absolute(y - yn)
f = [[], []]
p = 0
# Filter out data points with large absolute difference
for i in range(len(y)):
if np.absolute(y[i] - yn[i]) < 100:
f[0].append(float(x[i] / 10000))
f[1].append(y[i])
else:
p += 1
print(len(x), p)
# Plot original data points
plt.scatter(x, y, c="black", s=0.2, alpha=0.8)
plt.show()
plt.clf()
# Plot filtered data points
plt.plot(f[0], f[1], 'k', alpha=0.9, linewidth=0.1)
# Apply another lowpass filter to filtered y-values
b, a = scipy.signal.butter(3, 0.005, 'lowpass')
y2 = scipy.signal.filtfilt(b, a, f[1], method="gust")
# Apply Savitzky-Golay filter to filtered y-values
y1 = savgol_filter(f[1], 301, 4)
# Plot filtered y-values
plt.plot(f[0], y2, 'y--')
plt.grid(axis='y')
plt.show()
plt.clf()