-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeature_eng.py
93 lines (70 loc) · 2.63 KB
/
feature_eng.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
# this file provides all feature engineering functions
class FeatureEng:
"""
Feature engineering class contains method for feature engineering.
INPUT
data: input data in a form of vector
fs: sampling frequency
"""
def __init__(self, data, fs):
# initializing FeatureEng class
self.data = data
self.fs = fs
def power_spec(self, keep_f):
"""
power_spec method takes signal as input and calculates power spectral density
INPUT
keep_f: keep frequency
"""
# import libraries
from scipy.signal import welch
f, Pxx_den = welch(x=self.data, fs=self.fs,
nperseg=self.fs * 2, nfft=self.fs * 4)
return Pxx_den[f < keep_f]
def histogram(self, nr_bins=100, range=[-3, 3], as_density=True):
"""
histogram function calculates histogram of input function for given input data
INPUT
nr_bins: resolution of histogram
range: numeric range to calculate histogram
as_density: return result as density or count
"""
# import libraries
import numpy as np
# calculate histogram
my_hist = np.histogram(a=self.data,
bins=np.linspace(range[0], range[1], nr_bins+1),
density=as_density)[0]
return my_hist
def hilbert_transform(self):
"""
hilbert transofrm on input data
This function return amplitude envelope and instantaneous phase and instantaneous frequency
"""
# import necessary libraries
from scipy.signal import hilbert
import numpy as np
# calculate hilbert transformation
analytic_signal = hilbert(self.data)
# get amplitude envelope of the signal
amplitude_envelope = np.abs(analytic_signal)
# get instantaneous phase
instantaneous_phase = np.unwrap(np.angle(analytic_signal))
# get instantaneous frequency
instantaneous_frequency = (np.diff(instantaneous_phase) /
(2.0*np.pi) * self.fs)
return amplitude_envelope, instantaneous_frequency, instantaneous_phase
def autocorrelation(self, lag=1):
"""
autocorrelation function calculates auto correlation for given signal until lag = n
"""
# necesary libs
import numpy as np
# full correlation
my_corr = np.correlate(self.signal, self.signal, mode='full')
return my_corr[int(len(my_corr)/2):]
def filter(self):
"""
apply different signal filtering
"""
pass