forked from LukasKG/SHL_GAN
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sliding_window.py
56 lines (42 loc) · 1.54 KB
/
sliding_window.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
# -*- coding: utf-8 -*-
from scipy.stats import kurtosis, skew
import numpy as np
# Zero Crossing Count
def zcc(data,*args,**kwargs):
return np.nonzero(np.diff(data > 0))[0].shape[0]
# Mean Crossing Count
def mcc(data,*args,**kwargs):
return np.nonzero(np.diff((data-np.mean(data)) > 0))[0].shape[0]
FXdict = {
"mean": (np.mean,None),
"std": (np.std,None),
"median": (np.median,None),
"zcr": (zcc,None),
"mcr": (mcc,None),
"kurtosis": (kurtosis,None),
"skew": (skew,None)
}
def extract_features(stack,FX_list):
# Number of extracted features
FX_size = len(FX_list)
# Number of output channels
CH_size = len(stack)
# Number of samples
SA_size = stack[0].shape[0]
data = np.empty((SA_size,CH_size*FX_size),dtype=np.float64)
for nn in range(SA_size):
for cc in range(CH_size):
win = stack[cc][nn]
for ff,FX_name in enumerate(FX_list):
# Check for NaN values and replace with median
if np.isnan(win.min()):
median = np.nanmedian(win)
for idx in np.argwhere(np.isnan(win)):
win[idx] = median
# Load Feature function
FX, FXOpts = FXdict[FX_name]
# Compute the feature
fx = FX(win,FXOpts)
# Update the output vector
data[nn,cc*FX_size+ff] = fx
return data