-
Notifications
You must be signed in to change notification settings - Fork 1
/
featuresLib.py
62 lines (42 loc) · 1.53 KB
/
featuresLib.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
import numpy as np
EPSILON = 0.00001 # For small float values
# Feature extraction functions
'''L2 norm of an array'''
def l2norm(array):
return np.linalg.norm(array, ord=2)
'''Correlation of an array with itself'''
def autocorr(array):
return np.correlate(array, array)[0]
'''Root mean squared of an array'''
def rms(array):
return np.sqrt(np.mean(array ** 2))
'''Zero crossing rate of an array as a fraction of total size of array'''
def zcr(array):
# Locations where array > 0, put -1 and 1 for rising/falling,
# divide by total data points
return len(np.nonzero(np.diff(np.sign(array)))[0]) / len(array)
def msf(freqs, psd_amps):
'''Mean square frequency'''
num = np.sum(np.multiply(np.resize(np.power(freqs, 2), len(psd_amps)), psd_amps))
denom = np.sum(psd_amps)
# In case zero amplitude transform is encountered
if denom <= EPSILON:
return EPSILON
return np.divide(num, denom)
def rmsf(freqs, psd_amps):
'''Root mean square frequency'''
return np.sqrt(msf(freqs, psd_amps))
def fc(freqs, psd_amps):
'''Frequency center'''
num = np.sum(np.multiply(np.resize(freqs, len(psd_amps)), psd_amps))
denom = np.sum(psd_amps)
# In case zero amplitude transform is encountered
if denom <= EPSILON:
return EPSILON
return np.divide(num, denom)
def vf(freqs, psd_amps):
'''Variance frequency'''
return msf(freqs - fc(freqs, psd_amps), psd_amps)
def rvf(freqs, psd_amps):
'''Root variance frequency'''
return np.sqrt(msf(freqs, psd_amps))