-
Notifications
You must be signed in to change notification settings - Fork 1
/
R_PEAKS_Tools.cpp
executable file
·137 lines (113 loc) · 3.05 KB
/
R_PEAKS_Tools.cpp
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
#include "R_PEAKS_Tools.h"
#include <algorithm>
#include <fstream>
#include <iostream>
using namespace std;
void Tools::convolve(const std::vector<float>& signal, const std::vector<float>& kernel, std::vector<float>& result) const
{
size_t signalLen = signal.size();
size_t kernelLen = kernel.size();
size_t kmin = 0;
size_t kmax = 0;
for (size_t i = 0; i < signalLen + kernelLen - 1; ++i)
{
result[i] = 0;
kmin = (i >= kernelLen - 1) ? i - (kernelLen - 1) : 0;
kmax = (i < signalLen - 1) ? i : signalLen - 1;
for (size_t j = kmin; j <= kmax; ++j)
{
result[i] += signal[j] * kernel[i - j];
}
}
}
void Tools::vector_square(std::vector<float>& vec) const
{
for (size_t i = 0; i < vec.size(); ++i)
{
vec[i] = pow(vec[i], 2);
}
}
void Tools::adjust(std::vector<unsigned int>& peaks, const std::vector<float>& ecgData, float samplingFrequency) const
{
size_t m = ceil(0.05*samplingFrequency);
size_t i = 0;
if(peaks.size() == 0) return;
if(peaks[i] < m)
{
if(peaks[i] + m > ecgData.size())
{
auto pos = std::max_element(ecgData.begin(), ecgData.end());
peaks[i] = pos - ecgData.begin();
}
else
{
auto pos = std::max_element(ecgData.begin(), ecgData.begin() + peaks[i]);
peaks[i] = pos - ecgData.begin();
}
}
else
{
if(peaks[i] + m > ecgData.size())
{
auto pos = std::max_element(ecgData.begin() + (peaks[i] - m), ecgData.end());
peaks[i] = peaks[i] - m + (pos - (ecgData.begin() + (peaks[i] - m)));
}
else
{
auto pos = std::max_element(ecgData.begin() + (peaks[i] - m), ecgData.begin() + peaks[i] + m);
peaks[i] = peaks[i] - m + (pos - (ecgData.begin() + (peaks[i] - m)));
}
}
for(i = 1; i < peaks.size() - 1; ++i)
{
auto pos = std::max_element(ecgData.begin() + (peaks[i] - m), ecgData.begin() + peaks[i] + m);
peaks[i] = peaks[i] - m + (pos - (ecgData.begin() + (peaks[i] - m)));
}
i = peaks.size() - 1;
if(peaks[i] + m > ecgData.size())
{
auto pos = std::max_element(ecgData.begin() + (peaks[i] - m), ecgData.end());
peaks[i] = peaks[i] - m + (pos - (ecgData.begin() + (peaks[i] - m)));
}
else
{
auto pos = std::max_element(ecgData.begin() + (peaks[i] - m), ecgData.begin() + peaks[i] + m);
peaks[i] = peaks[i] - m + (pos - (ecgData.begin() + (peaks[i] - m)));
}
}
void Tools::createTimeVec(std::vector<float>& time, float samplingFrequency, size_t size) const
{
time.reserve(size);
for(float i = 0; i < size - 1; ++i)
{
time.push_back(i/samplingFrequency);
}
}
void Tools::writeVectorToFile(std::vector<unsigned int> vec, char * path , bool newFile) const
{
fstream file;
if(newFile)
file.open(path, ios::out);
else
file.open(path, ios::out | ios_base::app);
for (size_t i = 0; i < vec.size(); ++i)
{
file << vec[i] << ",";
}
file << endl;
file.close();
}
void Tools::writeVectorToFile(std::vector<float> vec, char * path, bool newFile) const
{
fstream file;
if(newFile)
file.open(path, ios::out);
else
file.open(path, ios::out | ios_base::app);
for (size_t i = 0; i < vec.size(); ++i)
{
file << vec[i] << ",";
}
file << endl;
file.close();
}