preProc.py 6.93 KB
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()
'''