-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathHRV1Analyzer.cpp
473 lines (349 loc) · 12.1 KB
/
HRV1Analyzer.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include "HRV1Analyzer.h"
#ifndef DEV // 4 testing
#include <QDebug>
#endif
HRV1Analyzer::HRV1Analyzer() {
signalSize=0;
signalSampling=0;
sizeFftIndex=0;
}
HRV1Analyzer::~HRV1Analyzer() {
delete[] sig;
delete[] sigAbsolute;
}
void HRV1Analyzer::runModule(const ECGInfo &info, const ECGRs & r_peaks_data, ECGHRV1 & hrv1_data) {
this->rPeaksData = r_peaks_data;
this->hrv1Data = &hrv1_data;
this->signalSampling = (int) info.channel_one.frequecy;
prepareSignal();
prepareSigAbsolute();
calculateParameters();
}
/**
* Przeksztalcenie tablicy z numerami probek na tablice zawierajaca zbior interwalow
* pomiedzy kolejnymi zespolami RR
*/
void HRV1Analyzer::prepareSignal() {
#ifdef DEBUG
qDebug() << "Preparing RR signal data.";
#endif
//wlasciwe przygotowanie sygnalu wejsciowego (double *sig)
#ifdef DEV
std::cout << "Preparing RR example signal data.\n";
ExampleSignal es = ExampleSignal();
sig = es.getSignal();
this->signalSize = es.getLength();
signalSampling = 500;
std::cout << "ExampleSignal loaded.\n";
#else
this->signalSize = (unsigned int) rPeaksData.GetRs()->signal->size*1;
sig = new double[this->signalSize];
double scalingValue = (double)(1000.0/signalSampling);
int v1, v2;
for(int i = 0; i < this->signalSize-1; i++) {
v1 = gsl_vector_int_get (rPeaksData.GetRs()->signal, i);
v2 = gsl_vector_int_get (rPeaksData.GetRs()->signal, i+1);
sig[i] = (double) abs(v2-v1) * scalingValue;
}
//po przygotowaniu wielkosc sygnalu RR ulega zmniejszeniu
this->signalSize = this->signalSize -1;
#endif
#ifdef DEBUG
qDebug() << "Preparing RR signal data has been completed.";
#endif
}
void HRV1Analyzer::prepareSigAbsolute() {
sigAbsolute = new double[this->signalSize];
sigAbsolute[0] = sig[0];
for(int i=1; i<this->signalSize; i++) {
sigAbsolute[i] = sig[i] + sigAbsolute[i-1];
#ifdef DEBUG_SIG
qDebug() << "sig: " << sig[i] << " sigA: " << sigAbsolute[i];
#endif
}
}
/**
* Metoda odpowiadajaca za wyliczenie wszystkich parametrow ilosciowych oraz czestotliwosciowych,
* ktore skladaja sie na analize, ktora ma przeprowadzic modul HRV1
*/
void HRV1Analyzer::calculateParameters() {
#ifdef DEBUG
qDebug() << "HRV1 calculateParameters method started";
#endif
double temp=0;
/////////////////////RR_avg
//OK: accuracy (vs Matlab 2010b) 0.00005
this->hrv1Data->RR_avg = mean(sig,0,this->signalSize);
#ifdef DEBUG
qDebug() << "RR_avg:" << this->hrv1Data->RR_avg;
#endif
/////////////////////RR_stddev
//OK: accuracy 0.006
this->hrv1Data->RR_stddev = std(sig,0,this->signalSize);
#ifdef DEBUG
qDebug() << "RR_stddev:" << this->hrv1Data->RR_stddev;
#endif
//////////////////////SDNN
//OK: accuracy 0.00005
temp=0;
for(int i=0; i<this->signalSize; i++) {
temp += (this->hrv1Data->RR_avg-sig[i])
*(this->hrv1Data->RR_avg-sig[i]);
}
this->hrv1Data->SDNN = sqrt( (double)temp/this->signalSize-1 );
#ifdef DEBUG
qDebug() << "SDNN:" << this->hrv1Data->SDNN;
#endif
//////////////////////RMSSD
//OK: accuracy 0.00005
temp=0;
for(int i=0; i<this->signalSize-1; i++) {
temp += (sig[i+1]-sig[i])*(sig[i+1]-sig[i]);
}
this->hrv1Data->RMSSD = sqrt( (double)temp/(this->signalSize-1) );
#ifdef DEBUG
qDebug() << "RMSSD:" << this->hrv1Data->RMSSD;
#endif
//////////////////////NN50
//OK: accuracy 0.0
temp=0;
double thresholdNN50 = 50;
for(int i=0; i<this->signalSize-1; i++) {
if (abs(sig[i+1]-sig[i]) > thresholdNN50)
temp++;
}
this->hrv1Data->NN50 = temp;
#ifdef DEBUG
qDebug() << "NN50:" << this->hrv1Data->NN50;
#endif
/////////////////////pNN50
//OK: accuracy 0.00005
this->hrv1Data->pNN50 = this->hrv1Data->NN50*100/(this->signalSize-1);
#ifdef DEBUG
qDebug() << "pNN50:" << this->hrv1Data->pNN50;
#endif
/////////////////////SDANN & SDANNi
//SDANN: accuracy 0.5
//SDANNi OK: accuracy 0.05
temp=0;
//temp variables
int windowSize = 1000*60*5; /* 60 seconds*5minutes */
long numberOfSteps = std::floor( sigAbsolute[this->signalSize-1]/windowSize );
//zabezpieczenie na wypadek smieci w ostatniej probce
if (numberOfSteps < 0) {
qDebug() << "Dopasowano sygnal!";
this->signalSize = this->signalSize -1;
long numberOfSteps = std::floor( sigAbsolute[this->signalSize-1]/windowSize );
}
double * mRRI = new double[numberOfSteps];
double * stdRR5 = new double[numberOfSteps];
int windowStartTime, windowEndTime, windowStartIndex, windowEndIndex;
//wyliczanie wartosci w 5 minutowym oknie czasowym
for(int step=1;step<=numberOfSteps;step++) {
windowStartTime = (step-1) * windowSize;
windowEndTime = step * windowSize;
windowStartIndex = 0;
windowEndIndex = 0;
for(int i=0; i<this->signalSize; i++) {
if(windowStartTime<=sigAbsolute[i]) {
windowStartIndex=i;
break;
}
}
for(int i=0; i<this->signalSize; i++) {
if(windowEndTime<=sigAbsolute[i]) {
windowEndIndex=i;
break;
}
}
//mRRI i stdRR5 w oknie czasowym
mRRI[step-1] = mean(sig, windowStartIndex, windowEndIndex+1);
stdRR5[step-1] = std(sig, windowStartIndex, windowEndIndex+1);
#ifdef DEBUG_WINDOWS
qDebug() << "#windowStartIndex=" << windowStartIndex << " windowEndIndex=" << windowEndIndex;
qDebug() << "#windowStartTime=" << windowStartTime << " windowEndTime=" << windowEndTime;
qDebug() << "#mRRI=" << mRRI[step-1] << " stdRR5=" << stdRR5[step-1];
#endif
}
//zapis SDANN i SDANNi
this->hrv1Data->SDANN = std(mRRI, 0, numberOfSteps);
this->hrv1Data->SDANN_index = mean(stdRR5, 0, numberOfSteps);
#ifdef DEBUG
qDebug() << "SDANN:" << this->hrv1Data->SDANN;
qDebug() << "SDANNi:" << this->hrv1Data->SDANN_index;
#endif
///////////////////SDSD
//OK: accuracy 0.01
double* tmpSig = new double[this->signalSize-1];
for(int i=0; i<this->signalSize-1; i++) {
tmpSig[i] = sig[i+1] - sig[i];
}
this->hrv1Data->SDSD = std(tmpSig,0,this->signalSize-1);
#ifdef DEBUG
qDebug() << "SDSD:" << this->hrv1Data->SDSD;
#endif
delete[] tmpSig;
////////////////////FFT
double* sigAfterSpline = cubicSpline(sigAbsolute, sig, this->signalSize);
int sigAfterSplineSize = (int)(sigAbsolute[this->signalSize-1]/(FREQUENCY_FFT))-1;
double* fftMagnitude = doFFT(sigAfterSpline, sigAfterSplineSize);
double freqq = (double)signalSampling/sigAfterSplineSize;
//wlasciwa analiza czestotliwosciowa
for(int i = 1; i< sizeFftIndex; i++) {
if(0.003>i*freqq) {
this->hrv1Data->ULF += fftMagnitude[i];
}
else if(0.003<i*freqq && 0.04>=i*freqq) {
this->hrv1Data->VLF += fftMagnitude[i];
}
else if(0.04<i*freqq && 0.15>=i*freqq) {
this->hrv1Data->LF += fftMagnitude[i];
}
else if(0.15<i*freqq && 0.4>=i*freqq) {
this->hrv1Data->HF += fftMagnitude[i];
}
}
this->hrv1Data->TP = this->hrv1Data->ULF + this->hrv1Data->VLF + this->hrv1Data->LF + this->hrv1Data->HF;
this->hrv1Data->LFHF = this->hrv1Data->LF / this->hrv1Data->HF;
#ifdef DEBUG
qDebug() << "ULF:" << this->hrv1Data->ULF;
qDebug() << "VLF:" << this->hrv1Data->VLF;
qDebug() << "LF:" << this->hrv1Data->LF;
qDebug() << "HF:" << this->hrv1Data->HF;
qDebug() << "TP:" << this->hrv1Data->TP;
qDebug() << "LFHF:" << this->hrv1Data->LFHF;
#endif
////////////////////power & frequency plot
#ifndef DEV
this->hrv1Data->freqency = OtherSignal(new WrappedVector);
this->hrv1Data->freqency->signal = gsl_vector_alloc(sizeFftIndex);
this->hrv1Data->power = OtherSignal(new WrappedVector);
this->hrv1Data->power->signal = gsl_vector_alloc(sizeFftIndex);
for(int i=0; i<sizeFftIndex; i++) {
this->hrv1Data->freqency->set(i, i);
this->hrv1Data->power->set(i, fftMagnitude[i]);
}
#endif
//sprzatanie pamieci
delete[] mRRI;
delete[] stdRR5;
delete[] fftMagnitude;
delete[] sigAfterSpline;
}
#ifndef DEV
void HRV1Analyzer::setParams(ParametersTypes ¶meterTypes) { }
#endif
/**
* Funkcja przeprowadzajaca analize fft zinterpolowanego sygnalu
* z wykorzystaniem biblioteki kiss fft
*/
double* HRV1Analyzer::doFFT(double* sigAfterSpline, int size) {
// ustalenie uwagi na interesujacy nas fragment sygnalu
int sizeFftSig = 2000; //nieco ponad 1,6Hz
#ifdef DEBUG
qDebug() << "Method doFFT() started";
#endif
#ifdef DEV
std::cout << "Method doFFT() started\n";
#endif
size = (size>2000)?sizeFftSig:(int)(size*3/4); //nieco ponad 1,6Hz
kiss_fft_cpx * out_cpx = new kiss_fft_cpx[size], * out = new kiss_fft_cpx[size], *cpx_buf;
kiss_fft_cfg fft = kiss_fft_alloc(size, false, NULL,0);
cpx_buf = copycpx(sigAfterSpline,size);
kiss_fft(fft,cpx_buf, out_cpx);
//zwalnianie pamieci
kiss_fft_cleanup();
free(fft);
sizeFftIndex = (size+(size%2))/2;
double * fftMagnitude = new double[sizeFftIndex];
#ifdef DEV
std::cout << "fftMagnitude[] length: " << sizeFftIndex << "\n";
#endif
for(int i=0; i<sizeFftIndex; i++) {
fftMagnitude[i] = 2*(out_cpx[i].r*out_cpx[i].r)/(size*size); //=(abs(out_cpx[i].r)/size)*(abs(out_cpx[i].r)/size)
}
// wziecie tylko polowy wartosci fft (bo jest symetryczne)
fftMagnitude[0] = fftMagnitude[0]/2;
if(size%2==0) {
fftMagnitude[sizeFftIndex-1] = fftMagnitude[sizeFftIndex-1]/2;
}
#ifdef DEBUG_FFT
for(i=0;i<sizeFftIndex;i++) {
qDebug() << "fft#"<< i << ": " << fftMagnitude[i];
}
#endif
delete[] out_cpx;
delete[] out;
delete cpx_buf;
#ifdef DEBUG
qDebug() << "Method doFFT() finished";
#endif
#ifdef DEV
std::cout << "Method doFFT() finished\n";
#endif
return fftMagnitude;
}
/**
* Metoda zamienia tablice double* o dlugosci nframe na strukturze uzywana
* przez biblioteke kiss fft
*/
kiss_fft_cpx* HRV1Analyzer::copycpx(double *mat, int nframe) {
int i;
kiss_fft_cpx *mat2;
mat2=(kiss_fft_cpx*)KISS_FFT_MALLOC(sizeof(kiss_fft_cpx)*nframe);
kiss_fft_scalar zero;
memset(&zero,0,sizeof(zero) );
for(i=0; i<nframe ; i++) {
mat2[i].r = mat[i];
mat2[i].i = zero;
}
return mat2;
}
/**
* Interpolacja funkcji o wektorach x i y.
* Dlugosc wektorow x i y jest taka sama i wynosi nframe
*/
double* HRV1Analyzer::cubicSpline(double* x, double* y, int nframe) {
alglib::spline1dinterpolant s;
alglib::ae_int_t natural_bound_type = 2;
alglib::real_1d_array *rx = new alglib::real_1d_array();
rx->setcontent(nframe, x);
alglib::real_1d_array *ry = new alglib::real_1d_array();
ry->setcontent(nframe, y);
alglib::spline1dbuildcubic(
*const_cast<const alglib::real_1d_array*>(rx),
*const_cast<const alglib::real_1d_array*>(ry),
s);
//wlasciwe interpolowanie funkcji
long sizeAfterSpline = (long)(x[this->signalSize-1]/FREQUENCY_FFT) +1;
double* sigI = new double[sizeAfterSpline];
#ifdef DEBUG
qDebug() << "cubicSpline sizeAfterSpline= " << sizeAfterSpline << "\n";
#endif
for(int i=0; i<sizeAfterSpline; i++) {
sigI[i] = alglib::spline1dcalc(s, i*FREQUENCY_FFT);
}
return sigI;
}
/**
* Oblicza srednia z wartosci przechowywanych w tablicy tab pomiedzy indeksami start i end
*/
double HRV1Analyzer::mean(double *tab, int start, int end) {
double temp=0;
for(int i=start; i<end; i++) {
temp += tab[i];
}
return temp/(end-start);
}
/**
* Oblicza odchylenie standardowe z wartosci przechowywanych w tablicy tab
* pomiedzy indeksami start i end
*/
double HRV1Analyzer::std(double *tab, int start, int end) {
double temp=0;
double avg = mean(tab, start, end);
for(int i=start; i<end; i++) {
temp += std::pow( (tab[i]-avg), 2);
}
return std::sqrt(temp/(end-start));
}