-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculate_Feature.py
169 lines (144 loc) · 4.66 KB
/
Calculate_Feature.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
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 27 18:10:21 2021
@author: nickxia
"""
from scipy.fftpack import fft
from scipy import signal
import matplotlib.pyplot as plt
import numpy as np
import math
# import pywt
from pyemd import EMD
def np_move_avg(a,n,mode="same"):
return(np.convolve(a, np.ones((n,))/n, mode=mode))
class Get_Feature():
def __init__ (self,data,code):
self.data = np_move_avg(data,15,mode="same")
self.code = code
self.result = []
##############Time_Domain################
def mean(self):
#for i in range (self.data.shape[1]):
tem = self.data
self.result.append(np.mean(tem))
def var(self):
#for i in range (self.data.shape[1]):
tem = self.data
self.result.append(np.var(tem))
def standard(self):
#for i in range (self.data.shape[1]):
tem = self.data
self.result.append(np.std(tem))
def Per_75(self):
#for i in range (self.data.shape[1]):
tem = self.data
self.result.append(np.percentile(tem,75))
def Per_25(self):
#for i in range (self.data.shape[1]):
tem = self.data
sfper = np.percentile(tem,75)
Q1 = np.percentile(tem,25)
self.result.append((sfper - Q1))
#################Frequency_Domain###################
####Estimate power spectral density using Welch’s method####.
def mean_PSD(self):
#for i in range (self.data.shape[1]):
tem = self.data
freqs, psd = signal.welch(tem)
self.result.append(np.mean(psd))
def med_PSD(self):
#for i in range (self.data.shape[1]):
tem = self.data
freqs, psd = signal.welch(tem)
self.result.append(np.median(psd))
def MNF_PSO(self):
#mean frequecy of PSD
#for i in range (self.data.shape[1]):
tem = self.data
freqs, psd = signal.welch(tem)
#print(len(freqs),len(psd))
s = []
for j in range (len(freqs)):
s.append(freqs[j]*psd[j])
self.result.append((sum(s)/sum(psd)))
def MDF_PSO(self):
#for i in range (self.data.shape[1]):
tem = self.data
freqs, psd = signal.welch(tem)
s = 0
for j in range (len(freqs)):
s = s + psd[j]
if (sum(psd)/2)<= s:
MDF = freqs[j]
self.result.append(MDF)
def Entropy(self):
#for i in range (self.data.shape[1]):
tem = self.data
_, psd = signal.welch(tem,100)
psd_norm = np.divide(psd, psd.sum())
se = -np.multiply(psd_norm, np.log2(psd_norm)).sum()
self.result.append(se)
def EMD(self):
#for i in range (self.data.shape[1]):
tem = self.data
imf = []
emd = EMD
# IMFs = emd.emd(tem)
IMFs = emd.EMD(tem)
for n, j in enumerate (IMFs):
if n<=2: # pick the previous 3 item of IMF
#imf.append(i)
e = (sum(j**2))/5
imf.append(e)
res = tem-j
res_e = (sum(res**2))/5
imf.append(res_e)
self.result.append(imf[0])
self.result.append(imf[1])
self.result.append(imf[2])
self.result.append(imf[3])
def FFT(self):
#for i in range (self.data.shape[1]):
tem = self.data
N = 64
cor_X= fft(tem)
ps_cor = np.abs(cor_X)
self.result.append(ps_cor[1])
self.result.append(ps_cor[2])
self.result.append(ps_cor[3])
self.result.append(ps_cor[4])
self.result.append(ps_cor[5])
self.result.append(ps_cor[6])
self.result.append(ps_cor[7])
self.result.append(ps_cor[8])
self.result.append(ps_cor[9])
self.result.append(ps_cor[10])
###################Time-Frequency Domain########################
def cal_result(self):
#print("calculating the handcrafted features")
if self.code[0] == 1:
self.mean()
if self.code[1] == 1:
self.var()
if self.code[2] == 1:
self.standard()
if self.code[3] == 1:
self.Per_75()
if self.code[4] == 1:
self.Per_25()
if self.code[5] == 1:
self.mean_PSD()
if self.code[6] == 1:
self.med_PSD()
if self.code[7] == 1:
self.MNF_PSO()
if self.code[8] == 1:
self.MDF_PSO()
if self.code[9] == 1:
self.Entropy()
if self.code[10] == 1:
self.EMD()
if self.code[11] == 1:
self.FFT()
return self.result