forked from BIDS-collaborative/EKG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DSP.py
63 lines (52 loc) · 1.47 KB
/
DSP.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
import numpy as np
def clean(raw, start_time):
return list(filter(lambda x: x[1] > start_time, map(lambda x: [int(x.split(",")[0]),int(x.split(",")[1][:-1]), ''], raw)))
def avg_of_mid(raw, bot, top):
mid = sorted(raw, key=lambda x: x[0])
print(mid)
midran = range(int(len(mid)*bot), int(len(mid)*top))
add = 0
for i in midran:
add = add + mid[i][0]
avg = round(add/len(midran),2)
return avg
def add_avg(avg, data):
for i in range(len(data)):
data[i].append(avg)
return data
def label_R(data):
f = open('process_r.txt', 'w')
st = ''
for i in range(len(data)):
if data[i][0] > 600 and data[i][0] > data[i-1][0] and data[i][0] > data[i+1][0]:
data[i][2] = 'R'
st = st + str(data[i][0]) + ',' + str(data[i][1]) + ',' + str(data[i][2]) + ',' + str(data[i][3]) + "\n"
f.write(st)
return data
def calc_R_rate(data):
begin = 0
end = 0
time = []
for i in range(len(data)):
if data[i][2] == 'R':
end = data[i][1]
time.append((end - begin))
begin = data[i][1]
time.pop(0)
rate = []
for i in time:
rate.append(round(61440/i))
print(time)
print(rate)
return(time)
def main():
f = open('subject_9.txt', 'r')
l = f.readlines()
raw = clean(l, 29366)
f.close()
avg = avg_of_mid(raw, 0.25, 0.75)
a = add_avg(avg, raw)
k = label_R(a)
calc_R_rate(k)
if __name__ == '__main__':
main()