-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpO2Calculators.py
More file actions
128 lines (99 loc) · 5.41 KB
/
SpO2Calculators.py
File metadata and controls
128 lines (99 loc) · 5.41 KB
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
"""
This file is a module for project1
"""
class AcDcCalculator:
def __init__(self, threshold_percentage) -> None:
self.threshold_percentage = threshold_percentage
self.peak_indexes = []
self.valley_indexes = []
self.total_indexes = []
self.peaks = []
self.valleys = []
self.looking_for_peaks = True
self.greatest_peak = 0
self.lowest_valley = 90000
self.peak_threshold = 0
self.valley_threshold = 0
self.true_peak = 0
self.true_peak_index = 0
self.true_valley = 90000
def peak_valley_detection(self, sample, sample_index):
if len(self.valleys) != 0 and len(self.valley_indexes) != 0:
if self.looking_for_peaks:
if sample > self.greatest_peak:
self.greatest_peak = sample
self.index_interest = sample_index
if self.greatest_peak - self.valleys[-1] < 1000:
self.valley_threshold = self.threshold_percentage * (self.greatest_peak - self.valleys[-1])
elif sample < self.greatest_peak - self.peak_threshold:
#print(str(self.index_interest) + "," + str(self.greatest_peak) + '\n')
self.true_peak = self.greatest_peak
self.true_peak_index = self.index_interest
#self.peaks.append(self.greatest_peak)
'''print("threshold for new valley: ", self.valley_threshold, "value needs to go below: ", self.peaks[-1] - self.valley_threshold)
print("greatest peak at: ", self.index_interest)
print("declared at sample: ", sample_index)'''
self.greatest_peak = 0
self.looking_for_peaks = False
self.peak_indexes.append(self.index_interest)
self.total_indexes.append(self.index_interest)
# print("valleys: ", self.valley_indexes)
# print("peaks: ", self.true_peak_index)
return 1
else:
if sample < self.lowest_valley:
#print("new lowest valley: ", sample, sample_index)
self.lowest_valley = sample
self.index_interest = sample_index
if self.true_peak - self.lowest_valley < 1000:
self.peak_threshold = self.threshold_percentage * (self.true_peak - self.lowest_valley)
elif sample > self.lowest_valley + self.valley_threshold:
#print(str(self.index_interest) + "," + str(self.lowest_valley) + '\n')
self.valleys.append(self.lowest_valley)
#self.threshold = self.threshold_percentage * (self.peaks[-1] - self.valleys[-1])
'''print("threshold for new peak: ", self.peak_threshold, "value needs to exceed: ", self.valleys[-1] + self.peak_threshold)
print("lowest valley at: ", self.index_interest)
print("declared at sample: ", sample_index)'''
self.looking_for_peaks = True
self.valley_indexes.append(self.index_interest)
self.total_indexes.append(self.index_interest)
self.true_valley = self.lowest_valley
self.lowest_valley = 90000
if len(self.valley_indexes) > 2:
self.valley_indexes.pop(0)
self.valleys.pop(0)
# print("valleys: ", self.valley_indexes)
# print("peaks: ", self.true_peak_index)
return 2
#print(self.valley_indexes)
else:
self.valleys.append(sample)
self.valley_indexes.append(sample_index)
def report_peek_valleys(self):
print("peaks: ", self.peaks)
print("valleys: ", self.valleys)
def calculate_ratio(self):
if len(self.valley_indexes) >= 2 and len(self.valleys) >= 2:
if self.valley_indexes[0] < self.true_peak_index and self.valley_indexes[1] > self.true_peak_index:
slope = (self.valleys[1] - self.valleys[0])/(self.valley_indexes[1] - self.valley_indexes[0])
cross_y = slope*(self.valley_indexes[0] - self.true_peak_index) - self.valleys[0]
cross_y = cross_y * -1
dc = cross_y
ac = self.true_peak - cross_y
return float(ac/dc)
def reset(self):
self.peak_indexes = []
self.valley_indexes = []
self.total_indexes = []
self.peaks = []
self.valleys = []
self.looking_for_peaks = True
self.greatest_peak = 0
self.lowest_valley = 90000
self.peak_threshold = 0
self.valley_threshold = 0
self.true_peak = 0
self.true_peak_index = 0
def calculate_SpO2(ir_ratio, red_ratio):
r = red_ratio/ir_ratio
return 104 - 17*r