forked from ScreamingPigeon/ms-analysis-23
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MSjerkfftanalysis -.py
235 lines (177 loc) · 6.44 KB
/
MSjerkfftanalysis -.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import pandas as pd
from matplotlib import pyplot as plt
import csv
from scipy.misc import derivative
from scipy.fft import rfft, rfftfreq
import numpy as np
import os
import scipy.fftpack
from scipy import integrate
import xlsxwriter
def plotData(path, idx2): #plots raw data, idx2 is first accelerometer column, should be 0
subdata = pd.read_excel(path)
time = subdata.iloc[:,0]
volt1 = subdata.iloc[:,idx2]
volt2 = subdata.iloc[:,idx2+1]
volt3 = subdata.iloc[:,idx2+2]
plt.plot(time,volt1)
plt.plot(time,volt2)
plt.plot(time,volt3)
def plotDer(path, idx2, color, scale):
#path, column index, color, scale
subdata = pd.read_excel(path)
time = subdata.iloc[:,0]
volt1 = subdata.iloc[:,idx2]
volt2 = subdata.iloc[:,idx2+1]
volt3 = subdata.iloc[:,idx2+2]
derivativex = []
for i in range(1, len(volt1)):
derivativex.append((volt1[i] - volt1[i-1])/0.0125)
time2 = time.drop((time.size-1))
#plt.plot(time2, derivativex)
#plt.show()
#plt.savefig("seq_charts_2s/"+path+"derivative.png")
#plt.clf()
derivativey = []
for i in range(1, len(volt2)):
derivativey.append((volt2[i] - volt2[i-1])/0.0125)
time2 = time.drop((time.size-1))
#plt.plot(time2, derivativey)
#plt.show()
#plt.savefig("seq_charts_2s/"+path+"derivative.png")
#plt.clf()
derivativez = []
for i in range(1, len(volt1)):
derivativez.append((volt3[i] - volt3[i-1])/0.0125)
time2 = time.drop((time.size-1))
#plt.plot(time2, derivativez)
#plt.show()
#plt.savefig("seq_charts_2s/"+path+"derivative.png")
#plt.clf()
resultant = []
time3 = time2.drop(time2.size-1)
for i in range(1, len(derivativex)):
resultant.append(np.sqrt(derivativex[i]**2 + derivativey[i]**2 + derivativez[i]**2))
plt.plot(time3, resultant, color, alpha=0.5)
plt.ylim(0, scale)
def plotFft(path, idx2, color, scale, xscale):
subdata = pd.read_excel(path)
time = subdata.iloc[:,0]
volt1 = subdata.iloc[:,idx2]
volt2 = subdata.iloc[:,idx2+1]
volt3 = subdata.iloc[:,idx2+2]
derivativex = []
for i in range(1, len(volt1)):
derivativex.append((volt1[i] - volt1[i-1])/0.0125)
time2 = time.drop((time.size-1))
derivativey = []
for i in range(1, len(volt2)):
derivativey.append((volt2[i] - volt2[i-1])/0.0125)
time2 = time.drop((time.size-1))
derivativez = []
for i in range(1, len(volt1)):
derivativez.append((volt3[i] - volt3[i-1])/0.0125)
time2 = time.drop((time.size-1))
resultant = []
time3 = time2.drop(time2.size-1)
for i in range(1, len(derivativex)):
resultant.append(np.sqrt(derivativex[i]**2 + derivativey[i]**2 + derivativez[i]**2))
# Number of samplepoints
N = len(resultant)
# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
#old fourier code
#yf = scipy.fft.rfft(resultant)
#yinteg = (2.0/N * np.abs(yf[:N//2]))
#xf = np.linspace(0.0, 1.0/(2.0*T), N//2)
#plt.plot(xf, 2.0/N * np.abs(yf[:N//2]))
#new fourier code
xf = scipy.fft.rfftfreq(N,T)
#makes xf scale equal to normal x scale
xfn = [i * 5.45625 for i in xf]
yf = scipy.fft.rfft(resultant)
yinteg = np.abs(yf)
plot, = plt.plot(np.abs(xfn),np.abs(yf))
#plt.plot(np.abs(yf), xf, color)
#numbers are frequency ranges, probably should be changed if needed
PDf1 = xfn[3:6]
essentialf2 = xfn[5:12]
PDf3 = xfn[3:5]
essentialf4 = xfn[8:12]
enhanced = xfn[12:18]
Norm = xfn[0:400]
xtest1 = xfn[12:14]
xtest2 = xfn[8:10]
#trapint(xf, yf, 3, 6)
integ1 = yinteg[3:6]
integ2 = yinteg[5:12]
integ3 = yinteg[3:5]
integ4 = yinteg[8:12]
integ5 = yinteg[12:18]
normal = yinteg[0:400]
integA = np.trapz(integ1)
integB = np.trapz(integ2)
integC = np.trapz(integ3)
integD = np.trapz(integ4)
integE = np.trapz(integ5)
Entire = np.trapz(Norm)
#integA = np.trapz(integ1, PDf1, T)
#integB = np.trapz(integ2, essentialf2, T)
#integC = np.trapz(integ3, PDf3, T)
#integD = np.trapz(integ4, essentialf4, T)
#Entire = np.trapz(Norm, normal, T)
#integA = integrate.trapz(PDf1, integ1)
#integB = integrate.trapz(essentialf2, integ2)
#integC = integrate.trapz(PDf3, integ3)
#integD = integrate.trapz(essentialf4, integ4)
#Entire = integrate.trapz(normal, Norm)
plt.ylim(0, scale)
plt.xlim(0, xscale)
#plt.axhline(y = 300, xmin = 0, xmax = 50)
plt.show()
ydata = plot.get_ydata()
xdata = plot.get_xdata()
nintegA = integA/Entire
nintegB = integB/Entire
nintegC = integC/Entire
nintegD = integD/Entire
nintegE = integE/Entire
#plt.axvline(x = 16, color = 'red')
#print(yinteg[5], ydata[5], xdata[16], xdata[1], len(xdata))
#print("<", test1, test2)
#print(xfn)
print("<", integA, integB, integC, integD, Entire, ">")
print(nintegA, nintegB, nintegC, nintegD)
values = [nintegA, nintegB, nintegC, nintegD]
#print(values)
with open('RTUCR.csv', mode='a', newline='') as f:
write = csv.writer(f)
write.writerow(values)
def getAll(path, idx, idx2, color1, color2, scale): #outputs all resultant graphs
directory = path
for filename in os.scandir(directory):
filepath = filename.path
plt.clf()
plotDer(filepath, idx, idx2, color1, scale)
plotDer(filepath, idx, idx2+6, color2, scale)
plt.xlabel('Time')
plt.ylabel('Derivative of resultant')
plt.title(filename.path)
plt.savefig(filename.name + ".png")
plt.clf()
def allFft(path, idx, idx2, color1, scale, xscale): #outputs all relative fft values
directory = path
for filename in os.scandir(directory):
filepath = filename.path
print(filename, filepath)
plt.clf()
plotFft(filepath, idx, idx2, color1, scale, xscale)
plt.xlabel('Frequency Domain')
plt.ylabel('Amplitude')
plt.title(filename.path)
#plt.savefig("FFT" + filename.name + ".png")
#with open('FFT_integrals.csv', 'w') as f:
#write = csv.writer(f)
#write.writerow(values)
plt.clf()