-
Notifications
You must be signed in to change notification settings - Fork 2
/
Preprocess.py
59 lines (42 loc) · 1.59 KB
/
Preprocess.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
import scipy.signal
import numpy as np
import matplotlib.pyplot as plt
class Filtering:
def __init__(self):
pass
def ButterBandPass(self, Rt, lowcut, highcut, order, fs, data, axis=0):
# Rt: whether its Real-time
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = scipy.signal.butter(order, [low, high], btype='bandpass')
if Rt:
return scipy.signal.filtfilt(b, a, data, axis)
else:
return scipy.signal.lfilter(b, a, data, axis) # real-time filter
def EllipordBandPass(self, lowcut, highcut, order, fs, data, axis=0):
Wp = [lowcut/fs*2, highcut/fs*2]
Ws = [(lowcut-0.5)/fs*2, (highcut+0.5)/fs*2]
N, Wn = scipy.signal.ellipord(Wp, Ws, 3, 60, True)
b, a = scipy.signal.ellip(N, 3, 60, Wn, 'bandpass', True)
return scipy.signal.filtfilt(b, a, data, axis)
class Normalize:
def __init__(self):
pass
def norm_min_max(self, data): # 极差变换法
return (data - np.min(data)) / (np.max(data)-np.min(data))
def norm_mean_std(self, data): # 0均值标准化
mean = np.mean(data, axis=0)
std_dev = np.std(data, axis=0)
return (data - mean) / std_dev
def norm_linear(self, data): # 线性比例变换法
return data / np.max(data)
class Character:
def __init(self):
pass
def fftPlot(self, data, fs):
plt.plot(np.arange(0, len(data)*fs/len(data), 1*fs/len(data)),
abs(np.fft.fft(data)*2/len(data)))
plt.xlim(0, 20)
plt.ylim(0, 10)
plt.show()