Blame view

preProc.py 6.93 KB
e45a8e9b   José Rolo   Script inicial co...
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
import math
import numpy as np
import matplotlib.pyplot as plt



def readFile(filename):
	data = []
	f = open(filename)
	lines = f.readlines()
	for l in lines:
		lineList = l.split(',')
		data.append({'order':lineList[0], 'accel':[float(lineList[1]),float(lineList[2]),float(lineList[3])]})
	return data

def FFTconversion(data):
	accelXList = [i['accel'][0] for i in data]
	fftAccelX = np.fft.fft(accelXList).real
	accelYList = [i['accel'][1] for i in data]
	fftAccelY = np.fft.fft(accelYList).real
	accelZList = [i['accel'][2] for i in data]
	fftAccelZ = np.fft.fft(accelZList).real
	out = []
	'''
	print len(fftAccelX),len(fftAccelY),len(fftAccelZ)
	
	plt.plot(range(0,len(accelXList)),accelXList,'-')
	plt.plot(range(0,len(accelYList)),accelYList,'-')
	plt.plot(range(0,len(accelZList)),accelZList,'-')
	
	plt.figure()
	
	plt.plot(range(0,len(fftAccelX)),[math.pow(i,2) for i in fftAccelX],'-') # Quadrado da transformada de fourier
	plt.plot(range(0,len(fftAccelY)),[math.pow(i,2) for i in fftAccelY],'-')
	plt.plot(range(0,len(fftAccelZ)),[math.pow(i,2) for i in fftAccelZ],'-')
	
	plt.figure()
	norma = [math.sqrt(math.pow(i[0],2) + math.pow(i[1],2) + math.pow(i[2],2)) for i in zip(fftAccelX,fftAccelY,fftAccelZ)]
	plt.plot(range(0,len(norma)),norma,'-')
	
	plt.show()
	'''
	
	for i in range(0,len(fftAccelX)):
		out.append({'order':i, 'accel':[fftAccelX[i],fftAccelY[i],fftAccelZ[i]]})
	return out

def calcSampleMeasures(data):
	out = []
	#frame = []
	prevModule = 0
	for d in data:
		d['accel_module'] = module(d['accel'])
		d['phi_angle'] = calcPhi(d['accel'])*180/math.pi
		d['theta_angle'] = calcTheta(d['accel'])*180/math.pi
		d['module_variation'] = math.fabs(d['accel_module'] - prevModule)
		out.append(d)
		prevModule = d['accel_module']
	return out


def buildFrames(data,n=10):
	out = []
	def divideListNParts(l, n):
		ret = []
		for i in xrange(0, len(l), n):
			ret.append(l[i:i+n])
		return ret
	frames = divideListNParts(data, n)
	for f in frames:
		modulesList = [i['accel_module'] for i in f]
		modulesVarList = [i['module_variation'] for i in f]
		thetaList = [i['theta_angle'] for i in f]
		phiList = [i['phi_angle'] for i in f]
		accelXFourier = np.fft.fft([i['accel'][0] for i in f]).real
		accelYFourier = np.fft.fft([i['accel'][1] for i in f]).real
		accelZFourier = np.fft.fft([i['accel'][2] for i in f]).real
		FourierNorm = [math.sqrt(math.pow(i[0],2) + math.pow(i[1],2) + math.pow(i[2],2)) for i in zip(accelXFourier, accelYFourier, accelZFourier)]
		#print max(FourierNorm[2:20])#Walk Detecting
		
		#plt.plot(range(0,len(FourierNorm)),FourierNorm,'-')
		#plt.show()
		FourierMax_20HZ = abs(max(FourierNorm[2:30]) - min(FourierNorm[2:20]))#max(FourierNorm[2:20])#Walk Detecting
		
		
		thetaMean = np.mean(thetaList)
		thetaMin = min(thetaList)
		thetaMax = max(thetaList)
		
		phiMean = np.mean(phiList)
		phiMin = min(phiList)
		phiMax = max(phiList)
		
		modulesMean = np.mean(modulesList)
		modulesMin = min(modulesList)
		modulesMax = max(modulesList)
		
		modulesVarMean = np.mean(modulesVarList)
		modulesVarMin = min(modulesVarList)
		modulesVarMax = max(modulesVarList)
		
		out.append({
			'FourierMax20Hz':FourierMax_20HZ,
			'thetaMean':thetaMean,
			'thetaMin':thetaMin,
			'thetaMax':thetaMax,
			'phiMean':phiMean,
			'phiMin':phiMin,
			'phiMax':phiMax,
			'modulesMean':modulesMean,
			'modulesMin':modulesMin, 
			'modulesMax':modulesMax, 
			'modulesVarMean':modulesVarMean, 
			'modulesVarMin':modulesVarMin, 
			'modulesVarMax':modulesVarMax})
	return out
	
def generateCSV(filename,className,frameLength,data,start=True):#,startIndex=1):
	f = open(filename,'a')
	i = 1#startIndex
	
	if(start):
		f.write("thetaMean,thetaMin,thetaMax,phiMean,phiMin,phiMax,modulesMean,modulesMin,modulesMax,modulesVarMean,modulesVarMin,modulesVarMax,FourierMax20Hz,class\n")
	for l in data:
		f.write(#str(i)+","+
			#str(frameLength)+","+
			str(l['thetaMean'])+","+
			str(l['thetaMin'])+","+
			str(l['thetaMax'])+","+
			str(l['phiMean'])+","+
			str(l['phiMin'])+","+
			str(l['phiMax'])+","+
			str(l['modulesMean'])+","+
			str(l['modulesMin'])+","+
			str(l['modulesMax'])+","+
			str(l['modulesVarMean'])+","+
			str(l['modulesVarMin'])+","+
			str(l['modulesVarMax'])+","+
			str(l['FourierMax20Hz'])+","+
			className+"\n")
		i+=1
	f.close()
	
	
def calcPhi(vector):
	return math.atan(vector[0]/vector[1])
def calcTheta(vector):
	return math.atan(vector[2]/(math.sqrt(math.pow(vector[0],2) + math.pow(vector[1],2))))
def module(vector):
	return math.sqrt(math.pow(vector[0],2) + math.pow(vector[1],2) + math.pow(vector[2],2))

###################################################################################################################

frameLength = 100
className = ["Correr","Bicicleta","Andar","Carro"]
filenameIN = ["Correr.csv","Bike.csv","Andar2.csv","Carro2.csv"]
filenameOUT = "AllOUT.csv"
#start_i = 211

for i in range(0,len(className)):
	
	data = readFile(filenameIN[i])
	#data2 = FFTconversion(data)
	out = calcSampleMeasures(data)
	print out
	#out2 = buildFrames(out,frameLength)

	#generateCSV(filenameOUT,className[i],frameLength,out2,(i==0))

'''
a1 = plt.subplot(3,1,1)#a1 = plt.subplot(341)
a1.plot(range(0,len(out2)),[i['modulesMin'] for i in out2],'-',color='red')
a1.set_title('Modules Minimum')
a2 = plt.subplot(3,1,2)#a2 = plt.subplot(345)
a2.plot(range(0,len(out2)),[i['modulesMax'] for i in out2],'-',color='green')
a2.set_title('Modules Maximum')
a3 = plt.subplot(3,1,3)#a3 = plt.subplot(349)
a3.plot(range(0,len(out2)),[i['modulesMean'] for i in out2],'-',color='blue')
a3.set_title('Modules Mean')

plt.figure()

b1 = plt.subplot(3,1,1)#b1 = plt.subplot(342)
b1.plot(range(0,len(out2)),[i['modulesVarMin'] for i in out2],'-',color='red')
b1.set_title('Modules Variation Minimum')
b1 = plt.subplot(3,1,2)#b1 = plt.subplot(346)
b1.plot(range(0,len(out2)),[i['modulesVarMax'] for i in out2],'-',color='green')
b1.set_title('Modules Variation Maximum')
b1 = plt.subplot(3,1,3)#b1 = plt.subplot(3,4,10)
b1.plot(range(0,len(out2)),[i['modulesVarMean'] for i in out2],'-',color='blue')
b1.set_title('Modules Variation Mean')

plt.figure()

c1 = plt.subplot(3,1,1)#c1 = plt.subplot(343)
c1.plot(range(0,len(out2)),[i['thetaMin'] for i in out2],'-',color='red')# modulo ??
c1.set_title('Theta Minimum')
c1 = plt.subplot(3,1,2)#c1 = plt.subplot(347)
c1.plot(range(0,len(out2)),[i['thetaMax'] for i in out2],'-',color='green')
c1.set_title('Theta Maximum')
c1 = plt.subplot(3,1,3)#c1 = plt.subplot(3,4,11)
c1.plot(range(0,len(out2)),[i['thetaMean'] for i in out2],'-',color='blue')
c1.set_title('Theta Mean')

plt.figure()

d1 = plt.subplot(3,1,1)#d1 = plt.subplot(344)
d1.plot(range(0,len(out2)),[i['phiMin'] for i in out2],'-',color='red')
d1.set_title('Phi Minimum')
d1 = plt.subplot(3,1,2)#d1 = plt.subplot(348)
d1.plot(range(0,len(out2)),[i['phiMax'] for i in out2],'-',color='green')
d1.set_title('Phi Maximum')
d1 = plt.subplot(3,1,3)#d1 = plt.subplot(3,4,12)
d1.plot(range(0,len(out2)),[i['phiMean'] for i in out2],'-',color='blue')
d1.set_title('Phi Mean')

plt.subplots_adjust(hspace = 0.4)

plt.show()
'''