-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_functions.py
More file actions
70 lines (60 loc) · 1.97 KB
/
my_functions.py
File metadata and controls
70 lines (60 loc) · 1.97 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
import numpy
import math
import mdp
import matplotlib.pyplot as plt
threadshold = 13
def normalize(x):
'''x is a multi-dimensional array of m rows and n cols'''
y = x.transpose()
result = []
N = len(y[0])
for row in y:
mean = float(sum(row))/N
variance = float(sum([i**2 for i in row]))/N - mean**2
result.append([float(i-mean)/math.sqrt(variance) for i in row])
return numpy.array(result).transpose()
def calc_heart_rate(input_data):
'''Input data is a list of 3-item list'''
data = normalize(numpy.array(input_data))
#try:
# data_ica = mdp.fastica(data, white_parm={'svd': True})
#except mdp.NodeException, e:
# return None
#data_fft_before = data_ica.transpose()
data_fft_before = data.transpose()
data_fft = numpy.fft.fft(data_fft_before)
return data_fft
def extract_frequency(complex_list):
global fs
amplitudes = [i.__abs__() for i in complex_list]
return amplitudes
#max = 0.0
#index = -1
#for i in range(1, len(amplitudes) + 1):
#if max < amplitudes[i - 1]:
#max = amplitudes[i - 1]
#index = i
#return fs * index / len(amplitudes)
def plot_diagrams(data, fs, last_f):
'''data will be a fft_ed data from calc heart rate'''
x = map(extract_frequency, data)
xs = range(len(data[0]))
N = len(xs)
xs = [i*fs*60/float(N) for i in xs]
i1 = int(math.ceil(40*float(N)/(fs*60)))
i2 = int(math.floor(240*float(N)/(fs*60)))
y = x[1][i1:i2+1]
xs = xs[i1:i2+1]
heart_rate_index = y.index(max(y))
heart_rate = xs[heart_rate_index]
if last_f == -1:
return heart_rate
while abs(heart_rate - last_f) > threadshold:
print 'estimating'
if len(y)==0:
return None
y.pop(heart_rate_index)
xs.pop(heart_rate_index)
heart_rate_index = y.index(max(y))
heart_rate = xs[heart_rate_index]
return heart_rate