-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathSTAnalysis.cpp
388 lines (316 loc) · 10.9 KB
/
STAnalysis.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include "STAnalysis.h"
#include "ECGST.h"
#include "ECGSignal.h"
#include <cstdio>
#include <cstdlib>
#ifdef DEVELOPMENT
#include "wfdb/wfdb.h"
#include "wfdb/ecgcodes.h"
#endif
STAnalysis::STAnalysis() :
analizator(nullptr)
{
setAnalizer();
}
STAnalysis::~STAnalysis()
{
setAnalizer(nullptr);
}
void STAnalysis::runModule(const ECGRs & rpeaks, const ECGWaves& waves, const ECGSignalChannel& signal, const ECGInfo& info, ECGST& output)
{
output = ECGST(); //Erease old data
#ifdef DEVELOPMENT
ECGRs my_rpeaks = read_normal_r_peaks("ecgSignals", info.channel_one.filename); //For tests
int N = my_rpeaks.count(); //For tests
#else
int N = rpeaks.count(); //For real
#endif
for(int i=0; i < N; i++) {
#ifdef DEVELOPMENT
analizator->analyse(i, my_rpeaks, waves, signal, info.channel_one, output); // For tests
#else
analizator->analyse(i, rpeaks, waves, signal, info.channel_one, output); //For real
#endif
}
//printf("ST ANALYSIS END");
}
STAnalysis::SimpleAnalizer::SimpleAnalizer() :
thresh(0.05), start(0), during_episode(false) {}
void STAnalysis::SimpleAnalizer::analyse(const int it, const ECGRs& rpeaks, const ECGWaves& waves, const ECGSignalChannel& signal, const ECGChannelInfo& info, ECGST& output)
{
ECGST::Interval interval;
ECGST::Episode ep;
double invgain = 1.0 / float(info.gain);
interval.rpoint = rpeaks.GetRs()->get(it);
int _60s_in_samples = info.frequecy*60;
int _60ms_in_samples = static_cast<int>(info.frequecy*60.0f/1000.0f);
#ifdef DEVELOPMENT
int _45ms_in_samples = static_cast<int>(info.frequecy*45.0f/1000.0f);
interval.isopoint = interval.rpoint - _45ms_in_samples;
interval.jpoint = interval.rpoint + _45ms_in_samples;
#else
interval.isopoint = waves.GetQRS_onset()->get(it);
interval.jpoint = waves.GetQRS_end()->get(it);
interval.tendpoint = waves.GetT_end()->get(it);
interval.ponsetpoint = waves.GetP_onset()->get(it);
#endif
interval.stpoint = interval.jpoint + _60ms_in_samples;
if ( interval.stpoint <= signal->signal->size) {
interval.offset = (signal->get(interval.isopoint) - signal->get(interval.stpoint))*invgain;
double diff = signal->get(interval.stpoint) - signal->get(interval.jpoint);
double invdist = 1/( ( (double) interval.stpoint ) - ( (double) interval.jpoint ) );
interval.slope = diff*invdist*invgain;
if (interval.normal(thresh))
{
interval.description = std::string("normal");
}
else if (interval.higher(thresh))
{
interval.description = std::string("higher");
}
else if (interval.lower(thresh))
{
interval.description = std::string("lower");
}
output.addInterval(interval);
}
if (during_episode && interval.normal(thresh)) {
if (interval.stpoint - start > _60s_in_samples) {
ep.start = start;
ep.end = interval.stpoint;
output.addEpisode(ep);
}
during_episode = false;
}
if (!during_episode && ! interval.normal()) {
start = interval.jpoint;
during_episode = true;
}
}
void STAnalysis::AbstractAnalizer::setParams(ParametersTypes& p)
{
params = p;
}
void STAnalysis::SimpleAnalizer::setParams(ParametersTypes& p)
{
STAnalysis::AbstractAnalizer::setParams(p);
auto it = params.find("simple_thresh");
if (it != params.end()) {
thresh = it->second;
}
}
STAnalysis::ComplexAnalizer::ComplexAnalizer() :
thresh(0.1), type_thresh(0.15), slope_thresh(0.15), start(0), during_episode(false) {}
std::pair<int, double> STAnalysis::ComplexAnalizer::maxDistanceSample(const OtherSignal& sig, int from, int to)
{
int indexMax = from;
double distMax = 0.0;
int size = to - from;
double first = (sig->get(from)*double(to) - sig->get(to)*double(from))/double(size),
A = (sig->get(from) - sig->get(to))/double(size),
ABsqrt = std::sqrt(A*A+1);
for (int i = 0; i < size; i++)
{
double d = (A * (from + i) + sig->get(from + i) + first)/ABsqrt;
if (d > distMax)
{
distMax = d;
indexMax = from + i;
}
}
return std::pair<int, double>(indexMax, distMax);
}
std::pair< int, int > STAnalysis::ComplexAnalizer::overBelowSamples(const OtherSignal& sig, int from, int to)
{
int over = 0, below = 0;
int size = to - from;
double first = (sig->get(from)*double(to) - sig->get(to)*double(from))/double(size),
A = (sig->get(from) - sig->get(to))/double(size);
for (int i = 0; i < size; i++)
{
double d = A * (from + i) + first;
if (sig->get(from+1) > d) over++;
else below++;
}
return std::pair<int, int>(over, below);
}
void STAnalysis::ComplexAnalizer::analyse(const int it, const ECGRs& rpeaks, const ECGWaves& waves, const ECGSignalChannel& signal, const ECGChannelInfo& info, ECGST& output)
{
ECGST::Interval interval;
ECGST::Episode ep;
double invgain = 1.0f / double(info.gain);
interval.rpoint = rpeaks.GetRs()->get(it);
int _60s_in_samples = info.frequecy*60;
int _20ms_in_samples = static_cast<int>(info.frequecy*20.0f/1000.0f);
#ifdef DEVELOPMENT
int _45ms_in_samples = static_cast<int>(info.frequecy*45.0f/1000.0f);
int _60ms_in_samples = static_cast<int>(info.frequecy*60.0f/1000.0f);
interval.isopoint = interval.rpoint - _45ms_in_samples;
interval.jpoint = interval.rpoint + _45ms_in_samples;
int tend = interval.rpoint + 4*_60ms_in_samples;
#else
interval.isopoint = waves.GetQRS_onset()->get(it);
interval.jpoint = waves.GetQRS_end()->get(it);
interval.tendpoint = waves.GetT_end()->get(it);
interval.ponsetpoint = waves.GetP_onset()->get(it);
int tend = waves.GetT_end()->get(it);
#endif
if ( tend <= signal->signal->size) {
int j20point = interval.jpoint + _20ms_in_samples;
int tpeak = getTPeak(signal, interval.jpoint, tend);
auto max_dist = maxDistanceSample(signal, j20point, tpeak);
interval.stpoint = max_dist.first;
interval.offset = (signal->get(interval.isopoint) - signal->get(interval.stpoint))*invgain;
int tepoint = 0;
bool straight = true;
if (interval.higher(thresh)) {
interval.description = std::string("higher");
tepoint = tpeak;
} else {
if (interval.lower(thresh)) interval.description = std::string("lower");
else interval.description = std::string("normal");
tepoint = interval.stpoint;
max_dist = maxDistanceSample(signal, j20point, interval.stpoint);
}
straight = max_dist.second < type_thresh;
if (tepoint - j20point > 2) {
double diff = signal->get(tepoint) - signal->get(j20point);
double invdist = 1.0f/double(tepoint - j20point);
interval.slope = diff*invdist*invgain;
} else {
interval.slope = 0;
}
if (straight) {
interval.description += " straight";
if (interval.slope > slope_thresh) interval.description += " upward";
else if (interval.slope < -1*slope_thresh) interval.description += " downward";
else interval.description += " horizon";
} else {
interval.description += " curve";
auto over_below = overBelowSamples(signal, interval.jpoint + _20ms_in_samples, tepoint);
double span = tepoint - (interval.jpoint + _20ms_in_samples);
if (double(over_below.first)/span > 0.7) {
interval.description += " convex";
} else if (double(over_below.second)/span > 0.7) {
interval.description += " concave";
}
}
output.addInterval(interval);
}
if (during_episode && interval.normal(thresh)) {
if (interval.stpoint - start > _60s_in_samples) {
ep.start = start;
ep.end = interval.stpoint;
output.addEpisode(ep);
}
during_episode = false;
}
if (!during_episode && ! interval.normal()) {
start = interval.jpoint;
during_episode = true;
}
}
int STAnalysis::ComplexAnalizer::getTPeak(const OtherSignal& sig, int from, int to)
{
int size = sig->signal->size;
double value = 0.0;
gsl_vector * tmp = gsl_vector_alloc(to - from);
for(int i = from; i < to; ++i) {
//printf("%.6f| ", sig->get(i));
if (i+2 < size) {
value = (sig->get(i+2) + 2*sig->get(i+1) - 2*sig->get(i-1) - sig->get(i-2))/8.0f;
} else if (i+1 < size) {
value = (2*sig->get(i+1) - 2*sig->get(i-1) - sig->get(i-2))/8.0f;
} else {
value = (-2*sig->get(i-1) - sig->get(i-2))/8.0f;
}
gsl_vector_set(tmp, i - from, value);
}
auto mxmn = std::minmax_element(tmp->data, tmp->data + tmp->size*tmp->stride);
int minit = (mxmn.first - tmp->data);
int maxit = (mxmn.second - tmp->data);
if ( minit < maxit ) {
return from + minit + (maxit - minit)/2;
} else {
return from + maxit + (minit- maxit)/2;
}
}
void STAnalysis::ComplexAnalizer::setParams(ParametersTypes& p)
{
STAnalysis::AbstractAnalizer::setParams(p);
auto it = params.find("complex_thresh");
if (it != params.end()) {
thresh = it->second;
}
it = params.find("type_thresh");
if (it != params.end()) {
type_thresh = it->second;
}
it = params.find("slope_thresh");
if (it != params.end()) {
slope_thresh = it->second;
}
}
void STAnalysis::setParams(ParametersTypes& p)
{
auto algorithm = p.find("algorithm");
if (algorithm != p.end()) {
setAnalizer(static_cast<AlgorithmType>((int)algorithm->second));
p.erase(algorithm);
}
analizator->setParams(p);
}
#ifdef DEVELOPMENT
ECGRs STAnalysis::read_normal_r_peaks(std::string path, std::string filename)
{
WFDB_anninfo info;
WFDB_Annotation ann;
int err;
ECGRs rs;
IntSignal rpeaks(new WrappedVectorInt());
size_t dot_pos = filename.rfind('.');
std::string file = path + "/" + filename.substr(0, dot_pos);
info.name = const_cast<char*>("atr");
info.stat = WFDB_READ;
if( (err = annopen(const_cast<char*>(file.c_str()), &info, 1)) < 0 ) {
printf("ANNOPEN error %d\n", err);
return rs;
}
int rpeaks_size = 0;
while (getann(0, &ann) == 0) {
if (ann.anntyp == NORMAL) rpeaks_size++;
}
if( (err = annopen(const_cast<char*>(file.c_str()), &info, 1)) < 0 ) {
printf("ANNOPEN error %d\n", err);
return rs;
}
rpeaks->signal = gsl_vector_int_alloc(rpeaks_size);
int it = 0;
while (getann(0, &ann) == 0) {
if (ann.anntyp == NORMAL) {
rpeaks->set(it, ann.time);
it++;
}
}
rs.setRs(rpeaks);
return rs;
}
#endif
void STAnalysis::setAnalizer(STAnalysis::AbstractAnalizer * a)
{
if (analizator) {
delete analizator;
}
analizator = a;
}
void STAnalysis::setAnalizer(STAnalysis::AlgorithmType atype)
{
switch(atype) {
case AlgorithmType::Complex:
setAnalizer(new ComplexAnalizer()); break;
case AlgorithmType::Simple:
setAnalizer(new SimpleAnalizer()); break;
default:
break; //Don't change anything
}
}