-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils_sig.py
167 lines (130 loc) · 4.53 KB
/
utils_sig.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import numpy as np
from scipy.fft import fft
from scipy import signal
from scipy.signal import butter, filtfilt
def butter_bandpass(sig, lowcut, highcut, fs, order=2):
# butterworth bandpass filter
sig = np.reshape(sig, -1)
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='band')
y = filtfilt(b, a, sig)
return y
def butter_ba(lowcut, highcut, fs, order=2):
# butterworth bandpass b and a
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='band')
return b, a
def butter_bandpass_batch(sig_list, lowcut, highcut, fs, order=2):
# butterworth bandpass filter (batch version)
# signals are in the sig_list
y_list = []
for sig in sig_list:
sig = np.reshape(sig, -1)
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='band')
y = filtfilt(b, a, sig)
y_list.append(y)
return np.array(y_list)
def hr_fft(sig, fs, harmonics_removal=True):
# get heart rate by FFT
# return both heart rate and PSD
sig = sig.reshape(-1)
sig = sig * signal.windows.hann(sig.shape[0])
sig_f = np.abs(fft(sig))
low_idx = np.round(0.6 / fs * sig.shape[0]).astype('int')
high_idx = np.round(4 / fs * sig.shape[0]).astype('int')
sig_f_original = sig_f.copy()
sig_f[:low_idx] = 0
sig_f[high_idx:] = 0
peak_idx, _ = signal.find_peaks(sig_f)
sort_idx = np.argsort(sig_f[peak_idx])
sort_idx = sort_idx[::-1]
peak_idx1 = peak_idx[sort_idx[0]]
peak_idx2 = peak_idx[sort_idx[1]]
f_hr1 = peak_idx1 / sig.shape[0] * fs
hr1 = f_hr1 * 60
f_hr2 = peak_idx2 / sig.shape[0] * fs
hr2 = f_hr2 * 60
if harmonics_removal:
if np.abs(hr1-2*hr2)<10:
hr = hr2
else:
hr = hr1
else:
hr = hr1
x_hr = np.arange(len(sig))/len(sig)*fs*60
return hr, fft(sig), x_hr
def hr_fft_batch(sig_list, fs, harmonics_removal=True):
# get heart rate by FFT (batch version)
# return both heart rate and PSD
hr_list = []
for sig in sig_list:
sig = sig.reshape(-1)
sig = sig * signal.windows.hann(sig.shape[0])
sig_f = np.abs(fft(sig))
low_idx = np.round(0.6 / fs * sig.shape[0]).astype('int')
high_idx = np.round(4 / fs * sig.shape[0]).astype('int')
sig_f_original = sig_f.copy()
sig_f[:low_idx] = 0
sig_f[high_idx:] = 0
peak_idx, _ = signal.find_peaks(sig_f)
sort_idx = np.argsort(sig_f[peak_idx])
sort_idx = sort_idx[::-1]
peak_idx1 = peak_idx[sort_idx[0]]
peak_idx2 = peak_idx[sort_idx[1]]
f_hr1 = peak_idx1 / sig.shape[0] * fs
hr1 = f_hr1 * 60
f_hr2 = peak_idx2 / sig.shape[0] * fs
hr2 = f_hr2 * 60
if harmonics_removal:
if np.abs(hr1-2*hr2)<10:
hr = hr2
else:
hr = hr1
else:
hr = hr1
# x_hr = np.arange(len(sig))/len(sig)*fs*60
hr_list.append(hr)
return np.array(hr_list)
def normalize(x):
return (x-x.mean())/x.std()
def SNR_get(waveform, gt_hr, fs, filtered=False):
waveform = np.reshape(waveform, -1)
if filtered:
waveform = butter_bandpass(waveform, 0.6, 4, 60)
N = waveform.shape[0]
# low_idx = np.round(0.6 / fs * waveform.shape[0]).astype('int')
# high_idx = np.round(4 / fs * waveform.shape[0]).astype('int')
# waveform[:low_idx] = 0
# waveform[high_idx:] = 0
bin1 = round(5/60/fs*N)
bin2 = round(10/60/fs*N)
f1 = gt_hr / 60
f2 = f1 * 2
bc1 = round(f1*N/fs)
bc2 = round(f2*N/fs)
window = signal.windows.hann(N)
win_waveform = waveform * window
waveform_f = np.abs(fft(win_waveform))**2
total_power = np.sum(waveform_f)
signal_power1 = 2*np.sum(waveform_f[bc1-bin1:bc1+bin1])
signal_power2 = 2*np.sum(waveform_f[bc2-bin2:bc2+bin2])
signal_power = signal_power1 + signal_power2
noise_power = total_power - signal_power
snr = 10*np.log10(signal_power/noise_power)
return snr
def es(series, alpha):
"""given a series and alpha, return series of expoentially smoothed points"""
results = np.zeros_like(series)
# first value remains the same as series,
# as there is no history to learn from
results[0] = series[0]
for t in range(1, series.shape[0]):
results[t] = alpha * series[t] + (1 - alpha) * results[t - 1]
return results