-
Notifications
You must be signed in to change notification settings - Fork 2
/
hrv.py
executable file
·54 lines (42 loc) · 1.18 KB
/
hrv.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
#!/usr/bin/python
import datetime, sys
import matplotlib.pyplot as plt
if len(sys.argv) != 2:
print("gief filename")
sys.exit(-1)
data = open(sys.argv[1]).read()
data = data.split("\n")
data = [line.split(" ") for line in data]
# Strip ''
data = [[item for item in line if item] for line in data]
# Remove blank rows
data = [row for row in data if row]
for row in data:
assert len(row) == 2
# Apply conversion
data = [(datetime.time.fromisoformat(time), float(ibi)) for time, ibi in data]
# Don't allow big jumps
data2 = []
ringbuf = [y for _, y in data[:5]]
def ringbuf_push(ringbuf, num):
ringbuf.pop(0)
ringbuf.append(num)
for x, y in data[5:]:
avg = sum(ringbuf)/5
ringbuf_push(ringbuf, y)
if abs(y - avg) > 60:
continue
data2.append((x, y))
x_data, y_data = zip(*data2)
# Convert x_data to UNIX timestamp
start_time = x_data[0]
def to_micros(time):
return (((time.hour*60 + time.minute)*60 + time.second)*10**6 +
time.microsecond)
x_data = [to_micros(x) for x in x_data]
# Convert y_data to BPM
y_data = [y/1000 for y in y_data]
y_data = [60/y for y in y_data]
plt.plot(x_data, y_data)
#plt.savefig("/tmp/weight.jpg")
plt.show()