-
Notifications
You must be signed in to change notification settings - Fork 7
/
heart_rate_detection.py
171 lines (150 loc) · 5.64 KB
/
heart_rate_detection.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'''
##########################################################################
t_min< number_samples/(total_time*frequency_object)<t_max
1.Provide t_min and t_max to run the program.
2.close the t_min and t_max to time period fastest the algorithm.
Observations-
3.Need higher sample rate
4.need samples with greater change during time each interval
Assuming total time =10 sec and total samples =1000
t_min< 100/frequency <t_max
########################################################################'''
"should be more variable with respect to time/change the limits on max & min "
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
t_min=88
t_max=130
sample_rate =100
with open('05._0.9Hz.txt') as f:
inp = []
for line in f:
line = line.split() # to deal with blank
if line: # lines (ie skip them)
line = [float(i) for i in line]
inp.append(line)
for i in range(0,len(inp)):
del inp[i][0]
inp = np.asarray(inp) #converting the input list to array
#plt.plot(inp)
#plt.show()
"---------------------------taking 2nd derivative ---------------------------"
def der(inp):
d_plus1 = np.roll(inp,-1)
d_plus2 = np.roll(inp,-2)
d_plus3 = np.roll(inp,-3)
d_minus1 = np.roll(inp,1)
d_minus2 = np.roll(inp,2)
d_minus3 = np.roll(inp,3)
dd_inp =4*inp
dd_inp = (dd_inp+d_plus1+d_minus1-2*(d_plus2+d_minus2)-(d_plus3+d_minus3))/(16*t_s/samples*t_s/samples)
return dd_inp
"-------------------Linear Warping by cubic spline interpolation--------------"
def warp(u,index):
x =np.arange(len(u))
tck = interpolate.splrep(x, u)
x1 =np.arange(index)
return interpolate.splev(x1, tck)
"------------------segmented variance calculation ---------------------------"
def var_seg(seg,morph):
warped = warp(morph,len(seg))
var1 =np.subtract(seg,warped)
return (np.linalg.norm(var1)*np.linalg.norm(var1))
"-----------------Update segmentation ---------------------------------------"
def segment(inp,u):
s=[[0]*1]*(len(inp)+1)
d=np.zeros(len(inp)+1)
temp2=np.full(10*t_max-10*t_min+1,2e21)
for i in range(1,len(inp)+1):
temp=np.full(t_max-t_min+1,2e21)
if t_min<=i<=t_max:
d[i] = d[0]+var_seg(inp[0:i-1],u)
if 2*t_min<=i<=2*t_max:
if i-t_min<=t_max:
for tau in range(t_min,i-t_min+1):
temp[tau - t_min] = d[tau] + var_seg(inp[tau:i-1],u)
d[i]=np.amin(temp)
s[i]=np.append(s[i],int(np.argmin(temp)+t_min))
temp=np.full(t_max-t_min+1,2e21)
else:
for tau in range(i-t_max,t_max+1):
temp[tau-i+t_max] = d[tau] + var_seg(inp[tau:i-1],u)
d[i]=np.amin(temp)
s[i]=np.append(s[i],int(np.argmin(temp)+i-t_max))
temp=np.full(t_max-t_min+1,2e21)
for itr in range(3,11):
if itr*t_min<= i <=itr*t_max:
for tau in range(t_min,t_max+1):
if (itr-1)*t_min<=i-tau<=(itr-1)*t_max:
temp[tau-t_min]= d[i-tau] + var_seg(inp[i-tau:i-1],u)
d[i]=np.amin(temp)
s[i]=np.append(s[i-(np.argmin(temp)+t_min)],int(i-(np.argmin(temp)+t_min)))
print itr,i,s[i]
temp=np.full(t_max-t_min+1,2e21)
if i==len(inp) and itr*t_min< len(inp) < itr*t_max:
temp2 =np.full(10*t_max-10*t_min+1,2e21)
for last in range((itr-1)*t_max,len(inp)-1):
temp2[last-itr*t_min] = d[last] + var_seg(inp[last:i-1],u)
d[i]=np.amin(temp2)
s[len(inp)]=np.append(s[np.argmin(temp2)+itr*t_min],np.argmin(temp2)+itr*t_min)
print itr
return s[i]
break
"----------------update template -------------------------------------------"
def template(inp,seg):
prev_cost = 2e21
cost = 0
morph=[]
for m in range(t_min,t_max+1):
sums =np.zeros(m)
for i in range(1,len(seg)):
sums=sums+abs(seg[i]-seg[i-1])*warp(inp[seg[i-1]:seg[i]-1],m)
sums=sums/len(inp)
cost = cost_fn(inp,sums,seg)
if cost<prev_cost:
prev_cost =cost
morph = sums
return morph
"----------------variance for min cost-----------------------------"
def cost_fn(inp,u,s):
sums=0
for i in range(1,len(s)):
sums=sums+var_seg(inp[s[i-1]:s[i]-1],u)
return sums
"---------------iterations -------------------------------------------------"
def main(inp):
u = np.zeros(t_max)
u=inp[0:t_max-1]
s = []
cost=np.zeros(15)
prev_cost =2e21
for i in range(0,1):
s =segment(inp,u)
# print s
u =template(inp,s)
#print u, len(u)
cost[i+1]= cost_fn(inp,u,s)
print cost[i+1],len(u)
plt.plot(inp)
#for t in range(0,len(s)):
# plt.axvline(s[t],color='r',linestyle='--')
plt.plot(u)
plt.show()
if i>2 and (cost[i+1] == cost[i] or cost[i]==cost[i-2]):
freq =0.000
for j in range(1,len(s)):
freq =freq + (s[j]-s[j-1])
freq =freq/(len(s)-1)
freq = sample_rate/freq
break
elif abs(cost[i+1] - cost[i]+1)/(cost[i]+cost[i+1]+1) < 0.5:
freq =0.000
for j in range(1,len(s)):
freq =freq + (s[j]-s[j-1])
freq =freq/(len(s)-1)
freq = sample_rate/freq
break
#plt.plot(u)
#plt.show()
print freq,'Hz'
main(inp)