-
Notifications
You must be signed in to change notification settings - Fork 143
/
signal_processing.py
110 lines (68 loc) · 2.73 KB
/
signal_processing.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
import cv2
import numpy as np
import time
from scipy import signal
class Signal_processing():
def __init__(self):
self.a = 1
def extract_color(self, ROIs):
'''
extract average value of green color from ROIs
'''
#r = np.mean(ROI[:,:,0])
g = []
for ROI in ROIs:
g.append(np.mean(ROI[:,:,1]))
#b = np.mean(ROI[:,:,2])
#return r, g, b
output_val = np.mean(g)
return output_val
def normalization(self, data_buffer):
'''
normalize the input data buffer
'''
#normalized_data = (data_buffer - np.mean(data_buffer))/np.std(data_buffer)
normalized_data = data_buffer/np.linalg.norm(data_buffer)
return normalized_data
def signal_detrending(self, data_buffer):
'''
remove overall trending
'''
detrended_data = signal.detrend(data_buffer)
return detrended_data
def interpolation(self, data_buffer, times):
'''
interpolation data buffer to make the signal become more periodic (advoid spectral leakage)
'''
L = len(data_buffer)
even_times = np.linspace(times[0], times[-1], L)
interp = np.interp(even_times, times, data_buffer)
interpolated_data = np.hamming(L) * interp
return interpolated_data
def fft(self, data_buffer, fps):
'''
'''
L = len(data_buffer)
freqs = float(fps) / L * np.arange(L / 2 + 1)
freqs_in_minute = 60. * freqs
raw_fft = np.fft.rfft(data_buffer*30)
fft = np.abs(raw_fft)**2
interest_idx = np.where((freqs_in_minute > 50) & (freqs_in_minute < 180))[0]
print(freqs_in_minute)
interest_idx_sub = interest_idx[:-1].copy() #advoid the indexing error
freqs_of_interest = freqs_in_minute[interest_idx_sub]
fft_of_interest = fft[interest_idx_sub]
# pruned = fft[interest_idx]
# pfreq = freqs_in_minute[interest_idx]
# freqs_of_interest = pfreq
# fft_of_interest = pruned
return fft_of_interest, freqs_of_interest
def butter_bandpass_filter(self, data_buffer, lowcut, highcut, fs, order=5):
'''
'''
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = signal.butter(order, [low, high], btype='band')
filtered_data = signal.lfilter(b, a, data_buffer)
return filtered_data