Last active
January 4, 2016 12:08
-
-
Save LittleWat/6ef3759ece25c2ba2aca to your computer and use it in GitHub Desktop.
pythonで加速度のデータから行動認識をやってみる① ref: http://qiita.com/LittleWat/items/f4156cffd714f2377bb0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
con = sqlite3.connect(dbBaseName + ".db") #dbBaseNameを指定してください | |
accDf = pd.read_sql_query("SELECT * from acc", con) #"acc"という名前に加速度データが記録してあります. | |
con.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def calcFFTenergy(col): | |
X = np.fft.fft(col) # FFT | |
squaredAmplitudeSpectrum = np.array([(c.real ** 2 + c.imag ** 2) for c in X]) | |
return squaredAmplitudeSpectrum.sum() / len(col) | |
def corrMat2series(df): | |
""" | |
:param df: pd.DataFrame.corr() | |
:return: pd.Series | |
""" | |
import itertools as it | |
combinationList = list(it.combinations(df.columns, 2)) # make 2 combinations | |
name = ["corr_" + "".join(list(x)) for x in combinationList] | |
corrArr = [] | |
for combination in combinationList: | |
corrArr.append(df.loc[combination[0], combination[1]]) | |
corrSeries = pd.Series(corrArr, index=name) | |
return corrSeries | |
windowSize = 128 #2のべき乗 | |
slidingSize = int(windowSize/2 ) | |
endIdx = smallDF.shape[0] | |
cnt = 0 | |
outDF = pd.DataFrame() | |
while (cnt + windowSize < endIdx): | |
windowDF = smallDF.iloc[cnt:cnt + windowSize, :] | |
meanSeries = windowDF.mean().add_prefix("mean_") | |
stdSeries = windowDF.std().add_prefix("std_") | |
energySeries = windowDF.apply(calcFFTenergy).add_prefix("energy_") | |
corrSeries = corrMat2series(windowDF.corr()) | |
allSeries = pd.concat([meanSeries, stdSeries, energySeries, corrSeries]) | |
allSeries.name = windowDF.index[0] | |
outDF = outDF.append(allSeries) | |
cnt += slidingSize |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
smallDF.loc[:,"time"] = smallDF.index | |
smallDF.index =smallDF.time.apply(lambda x: datetime.fromtimestamp(x/1000)) #データ型の変更 | |
del smallDF["time"] | |
smallDF = smallDF.resample('33L', how='mean') #33msecでリサンプリング | |
smallDF = smallDF.interpolate() #若干の欠損が出るので,線形補間 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//manager.registerListener(this, s, SensorManager.SENSOR_DELAY_GAME); | |
manager.registerListener(this, s, 20000); //20000μsec(=50Hz) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
import sqlite3 | |
from datetime import * |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment