-
Notifications
You must be signed in to change notification settings - Fork 0
/
zespolqrs.cpp
271 lines (249 loc) · 7.2 KB
/
zespolqrs.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <iostream>
#include <wfdb/wfdb.h>
#include <wfdb/ecgcodes.h>
#include <vector>
class qrs_analyzer
{
public:
qrs_analyzer(const std::string& f_name) : file_name(), number_of_signals(0), slope_criterion_min(0),
annotation_info(), result_annotation(), slope_criterion_max(0), slope_criterion(0),
slope_number(0), number_of_160ms_intervals(0), max_time(0), number_of_200ms_intervals(0),
number_of_2s_intervals(0), filter(0), time(0), qtime(0), sign(0), maxslope(0)
{
file_name = f_name;
get_number_of_signals();
fill_signal_info();
fill_result_annotation();
}
~qrs_analyzer()
{
delete[] info;
delete[] samples;
}
void execute()
{
set_samples_frequency();
get_annotation();
getsc_min_max();
get_sample_intervals();
init_samples();
qrs_detection_algorithm();
}
private:
void get_number_of_signals()
{
number_of_signals = isigopen(const_cast<char*>(file_name.c_str()), NULL, 0);
if(number_of_signals < 2)
{
exit(1);
}
}
void fill_signal_info()
{
info = new WFDB_Siginfo[number_of_signals];
samples = new WFDB_Sample[number_of_signals];
if(isigopen(const_cast<char*>(file_name.c_str()), info, number_of_signals)
!= number_of_signals)
{
exit(1);
}
}
void fill_result_annotation()
{
result_annotation.subtyp = 0;
result_annotation.chan = 0;
result_annotation.num = 0;
result_annotation.aux = NULL;
}
void set_samples_frequency()
{
if(sampfreq((char*)NULL) < 240. || sampfreq((char*)NULL) > 260.)
{
setifreq(250.);
}
}
void get_annotation()
{
annotation_info.name = "qrs";
annotation_info.stat = WFDB_WRITE;
if(annopen(const_cast<char*>(file_name.c_str()), &annotation_info, 1) < 0)
{
exit(1);
}
}
void getsc_min_max()
{
slope_criterion_min = muvadu(0, 1000);
slope_criterion = slope_criterion_max = 10 * slope_criterion_min;
}
void get_sample_intervals()
{
number_of_160ms_intervals = strtim("0.16");
number_of_200ms_intervals = strtim("0.2");
number_of_2s_intervals = strtim("2");
}
void init_samples()
{
getvec(samples);
sample1 = sample2 = sample3 = sample4 = sample5 = sample6 = sample7 = sample8
= sample9 = sample10 = samples[0];
}
void count_filter()
{
filter = (sample1 = samples[0]) + 4*sample2 + 6*sample3 + 4*sample4 + sample5
-sample6 - 4*sample7 - 6*sample8 - 4*sample9 - sample10;
}
void adjust_threshold()
{
if(time % number_of_2s_intervals == 0)
{
if(slope_number == 0)
{
slope_criterion -= slope_criterion >> 4;
if(slope_criterion < slope_criterion_min)
{
slope_criterion = slope_criterion_min;
}
}
else if(slope_number >= 5)
{
slope_criterion += slope_criterion >> 4;
if(slope_criterion > slope_criterion_max)
{
slope_criterion = slope_criterion_max;
}
}
}
}
void check_slope_found()
{
if(slope_number == 0 && abs(filter) > slope_criterion)
{
slope_number = 1;
max_time = number_of_160ms_intervals;
sign = (filter>0) ? 1:-1;
qtime = time;
}
}
void check_qrs_complex()
{
if(slope_number != 0)
{
if(filter*sign < -slope_criterion)
{
sign = -sign;
max_time = (++slope_number > 4) ? number_of_200ms_intervals : number_of_160ms_intervals;
}
else if(filter * sign > slope_criterion && abs(filter) > maxslope)
{
maxslope = abs(filter);
}
if(max_time-- < 0)
{
if(slope_number >= 2 && slope_number <= 4)
{
slope_criterion += ((maxslope>>2) - slope_criterion) >> 3;
if(slope_criterion < slope_criterion_min)
{
slope_criterion = slope_criterion_min;
}
else if(slope_criterion > slope_criterion_max)
{
slope_criterion = slope_criterion_max;
}
result_annotation.time = strtim("i") - (time-qtime) -4;
result_annotation.anntyp = NORMAL;
(void)putann(0, &result_annotation);
time = 0;
}
else if(slope_number >=5)
{
result_annotation.time = strtim("i") - (time-qtime) - 4;
result_annotation.anntyp = ARFCT;
(void)putann(0, &result_annotation);
}
slope_number = 0;
}
}
}
void update_samples()
{
sample10 = sample9;
sample9 = sample8;
sample8 = sample7;
sample7 = sample6;
sample6 = sample5;
sample5 = sample4;
sample4 = sample3;
sample3 = sample2;
sample2 = sample1;
time++;
}
void qrs_detection_algorithm()
{
do
{
count_filter();
adjust_threshold();
check_slope_found();
check_qrs_complex();
update_samples();
}
while(getvec(samples) > 0);
}
private:
std::string file_name;
size_t number_of_signals;
WFDB_Sample* samples;
WFDB_Siginfo* info;
WFDB_Anninfo annotation_info;
WFDB_Annotation result_annotation;
int slope_criterion_min;
int slope_criterion_max;
int slope_criterion;
int slope_number;
int number_of_160ms_intervals;
int number_of_200ms_intervals;
int number_of_2s_intervals;
int max_time;
int sample1, sample2, sample3, sample4, sample5, sample6,
sample7, sample8, sample9, sample10;
int filter;
int time;
int qtime;
int sign;
int maxslope;
};
std::string read_file_name()
{
std::string file_name;
std::cout << "Provide file name with record:\n";
std::cin >> file_name;
return file_name;
}
void print_annotation(const std::string& ann_name, const std::string& record_name)
{
WFDB_Anninfo a;
WFDB_Annotation annotation;
a.name = const_cast<char*>(ann_name.c_str());
a.stat = WFDB_READ;
(void)sampfreq(const_cast<char*>(record_name.c_str()));
if(annopen(const_cast<char*>(record_name.c_str()), &a, 1) < 0)
{
exit(1);
}
std::cout << "Wykryte zespoly QRS:" << std::endl;
while(getann(0, &annotation) == 0)
{
std::cout << "Czas: " << timstr(-(annotation.time)) << " ";
std::cout << "Number probki: " << annotation.time << std::endl;
}
}
int main()
{
auto name = read_file_name();
qrs_analyzer analyzer(name);
analyzer.execute();
print_annotation("qrs", name);
wfdbquit();
}