-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocess.py
87 lines (54 loc) · 1.79 KB
/
preprocess.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
import glob
import numpy as np
class sampleSet:
def __init__(self,features,labels):
self.features = features
self.labels = labels
def countZeroCrossing(data):
count = 0
for idx in range(len(data)-1):
if data[idx]*data[idx+1] < 0:
count = count + 1
return count
def peakCount(data):
count = 0
std = np.std(data)
for idx in range(len(data)-2):
if data[idx+1] > std*2 and (data[idx+1] - data[idx]) * (data[idx+2] - data[idx+1]) < 0:
count = count + 1
return count
allFeatures = []
def extractFeatures(sensorReadings):
colNum = np.shape(sensorReadings)[1]
features = np.zeros((colNum-1,8))
for col in range(1,colNum):
# average
features[col-1,0] = np.average(sensorReadings[:,col])
# std
features[col-1,1] = np.std(sensorReadings[:,col])
# peak count
features[col-1,2] = peakCount(sensorReadings[:,col])
# median
features[col-1,3] = np.median(sensorReadings[:,col])
# min
features[col-1,4] = np.min(sensorReadings[:,col])
# max
features[col-1,5] = np.max(sensorReadings[:,col])
# zero crossing
features[col-1,6] = countZeroCrossing(sensorReadings[:,col])
# max - min
features[col-1,7] = features[col-1,5] - features[col-1,4]
f = features.reshape(1,(colNum-1)*8)[0]
return f
# Main function
if __name__ == "__main__":
for r in glob.glob('test/samples/close/*'):
print r
data = np.load(r)
f = extractFeatures(data)
if allFeatures == []:
allFeatures = f
else:
allFeatures = np.vstack((allFeatures,f))
print np.shape(allFeatures)
np.save('test/preprocessed/close.npy',allFeatures)