-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfeature_extraction.py
94 lines (46 loc) · 1.8 KB
/
feature_extraction.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
import numpy as np
import math
import matplotlib.pyplot as plt
from scipy import signal
import itertools
from scipy.stats import kurtosis, skew,entropy
from math import log, e
from sklearn.utils import shuffle
from tqdm import tqdm
import os
from scipy.io import loadmat
from os import listdir
from os.path import isfile, join
from scipy import stats
from library.signal_filtering import butter_bandpass,butter_bandpass_filter
from scipy.signal import welch
from scipy.integrate import simps
from pywt import wavedec, upcoef
from scipy.integrate import simps
fs = 250
# ___________________________________Features Extraction___________________________________#
def feature_extraction(data): # data is current channel, temp_data are all 23 channels under the same frame
sf = fs
win = sf
# freqs, psd = signal.welch(data, sf, nperseg=win, scaling='density')
freqs, psd = signal.periodogram(data, sf, window= 'hann',scaling='density', detrend='constant')
psd_all = np.zeros((25, ))
for i in range(0, 25):
low, high = i*2+0.5, (i+1)*2+0.5
idx_min = np.argmax(freqs > low) - 1
idx_max = np.argmax(freqs > high) - 1
# print(freqs)
idx = np.zeros(dtype=bool, shape=freqs.shape)
idx[idx_min:idx_max] = True
# print('idx_max', idx_min, idx_max)
psd_all[i] = simps(psd[idx], freqs[idx])
DE_all = np.zeros((25, ))
for m in range(0, 25): #frequency band
new_data = butter_bandpass_filter(data, lowcut= 0.5 + m*2 , highcut= 0.5 +(m+1)*2, fs=250, order=4)
DE_all[m] = 0.5*np.log(2*np.pi*np.exp(1)*np.var(new_data))
features_1 = psd_all.tolist()
features_1 = np.log10(features_1)
features_2 = DE_all
features = np.hstack((features_1, features_2))
return features
###