from scipy.signal import butter, lfilter
import scipy
import numpy as np
def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
y = scipy.signal.lfilter(b, a, data)
return y
def butter_bandpass(lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='band')
return b, a
def one_hot(y_):
y_ = y_.reshape(len(y_))
y_ = [int(xx) for xx in y_]
n_values = np.max(y_) + 1
return np.eye(n_values)[np.array(y_, dtype=np.int32)]
import numpy as np
def extract(input, n_classes, n_fea, time_window, moving):
xx = input[:, :n_fea]
yy = input[:, n_fea:n_fea + 1]
new_x = []
new_y = []
number = int((xx.shape[0] / moving) - 1)
for i in range(number):
ave_y = np.average(yy[int(i * moving):int(i * moving + time_window)])
if ave_y in range(n_classes + 1):
new_x.append(xx[int(i * moving):int(i * moving + time_window), :])
new_y.append(ave_y)
else:
new_x.append(xx[int(i * moving):int(i * moving + time_window), :])
new_y.append(0)
new_x = np.array(new_x)
new_x = new_x.reshape([-1, n_fea * time_window])
new_y = np.array(new_y)
new_y.shape = [new_y.shape[0], 1]
data = np.hstack((new_x, new_y))
data = np.vstack((data, data[-1])) # add the last sample again, to make the sample number round
return data