-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalak.py
138 lines (115 loc) · 3.3 KB
/
malak.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
import serial
import time
import pygame
from pygame.locals import *
import os
import subprocess
import math
import random
import winsound
savefilename = input("Save to .log file with base name: ")
savefilename = savefilename + ".log"
print("Saving data to " + savefilename)
print('\n')
file2write = open(savefilename, 'w')
process = subprocess.Popen(['python','malak_sample.py'], stdout=subprocess.PIPE)
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption('Malak')
screen.fill((155,155,55))
myFont = pygame.font.SysFont("Times New Roman", 18)
Fs = 200
T = 4
nSamples = math.ceil(Fs * T)
HR_min = 40
HR_max = 140
IBI_min = 60 / HR_max
IBI_max = 60 / HR_min
IBI_min_samples = math.ceil(IBI_min * Fs)
signal = [0 for i in range(nSamples)]
newcalc_nSamples = math.ceil(0.2 * Fs) #
collecting = 1
iSample = 0
while collecting == 1:
val = process.stdout.readline()
val = val.decode()
val = val.strip()
file2write.write(val)
if (val == ""):
continue
val_list = val.split()
PPG_val = int(val_list[0])
light_val = int(val_list[1])
# Update signal
for i in range(len(signal) - 1):
signal[i] = signal[i + 1]
signal[len(signal) - 1] = PPG_val + 0.1 * random.random()
iSample = (iSample + 1) % newcalc_nSamples
if iSample != 0:
continue
# Detect peaks
peakv = [0 for i in range(nSamples)]
IBI_samples = []
for i in range(len(signal) - 1):
h = math.ceil(IBI_min_samples)
a = max(0, i - h)
b = min(len(peakv) - 1, i + h)
i_is_peak = 1
for j in range(a, b):
if i == j:
continue
if (signal[i] <= signal[j]):
i_is_peak = 0
peakv[i] = i_is_peak
if (i_is_peak == 1):
IBI_samples.append(i)
# print(IBI_samples)
IBI_sum = 0
IBI_N = 0
for i in range(len(IBI_samples) - 1):
this_IBI_samples = IBI_samples[i + 1] - IBI_samples[i]
this_IBI = this_IBI_samples / Fs
# print(str(this_IBI) + " [" + str(IBI_min) + ", " + str(IBI_max) + "]")
if (this_IBI < IBI_min or this_IBI > IBI_max):
continue
IBI_sum = IBI_sum + this_IBI
IBI_N = IBI_N + 1
# Calculate HR
if IBI_N > 0:
IBI_mean = IBI_sum / IBI_N
else:
IBI_mean = 1
HR = 60 / IBI_mean
# Visualization
screen.fill((55,55,55))
valDisplay = myFont.render("HR: " + str(HR), 1, (0, 255, 0))
screen.blit(valDisplay, (10, 50))
# Signal and peakdetection feedback
for i in range(len(signal) - 1):
dx = 1
x1 = 10 + i * dx
x2 = 10 + (i + 1) * dx
y1 = 300 - signal[i] / 5
y2 = 300 - signal[i + 1] / 5
pygame.draw.line(screen, (200, 0, 70), (x1, y1), (x2, y2), 1)
y1 = 300 - peakv[i] * 100
y2 = 300 - peakv[i + 1] * 100
pygame.draw.line(screen, (30, 140, 40), (x1, y1), (x2, y2), 3)
# HR representation
HR_normed = (HR - HR_min) / (HR_max - HR_min)
t = (375 - 100) * HR_normed
pygame.draw.rect(screen, (HR_normed * 200, 100, 100), (50, 375, 10, -t))
# pygame.display.flip()
# Light sensor
t = (375 - 100) * light_val / 1000
pygame.draw.rect(screen, (200, 200, 200), (525, 375, 10, -t))
pygame.display.flip()
# HR audio representation
f = int(2000 + 3000 * HR_normed)
winsound.Beep(f, 50)
for event in pygame.event.get():
if (event.type == pygame.QUIT):
print("Ending measurement...")
collecting = 0
pygame.display.quit()
file2write.close()