File: ecg_statistics.c

package info (click to toggle)
edfbrowser 1.67+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 3,312 kB
  • sloc: cpp: 62,150; ansic: 8,810; makefile: 12
file content (138 lines) | stat: -rw-r--r-- 3,132 bytes parent folder | download
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
/*
***************************************************************************
*
* Author: Teunis van Beelen
*
* Copyright (C) 2018 - 2019 Teunis van Beelen
*
* Email: teuniz@gmail.com
*
***************************************************************************
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
***************************************************************************
*/




#include "ecg_statistics.h"


int ecg_get_hr_statistics(double *beat_interval_list, int beat_cnt, struct ecg_hr_statistics_struct *ecg_stats)
{
  int i,
      NN20=0,
      NN50=0;

  double d_tmp,
         average_bpm=0,
         average_rr=0,
         sdnn_bpm=0,
         sdnn_rr=0,
         rmssd_rr=0,
         *buf_bpm=NULL;

  if(beat_interval_list == NULL)  return -1;

  if(ecg_stats == NULL)  return -2;

  if(beat_cnt < 3)  return -3;

  buf_bpm = (double *)malloc(sizeof(double) * beat_cnt);
  if(buf_bpm == NULL)
  {
    return -4;
  }

  for(i=0; i<beat_cnt; i++)
  {
    if(beat_interval_list[i] < 1e-15)
    {
      free(buf_bpm);
      buf_bpm = NULL;
      return -5;
    }

    buf_bpm[i] = 60.0 / beat_interval_list[i];

    average_bpm += buf_bpm[i];
    average_rr += beat_interval_list[i];

    if(i < (beat_cnt - 1))
    {
      d_tmp = (beat_interval_list[i] - beat_interval_list[i + 1]) * 1000.0;

      rmssd_rr += (d_tmp * d_tmp);

      if(((beat_interval_list[i] - beat_interval_list[i + 1]) > 0.02 ) || ((beat_interval_list[i + 1] - beat_interval_list[i]) > 0.02 ))
      {
        NN20++;
      }

      if(((beat_interval_list[i] - beat_interval_list[i + 1]) > 0.05 ) || ((beat_interval_list[i + 1] - beat_interval_list[i]) > 0.05 ))
      {
        NN50++;
      }
    }
  }

  average_bpm /= beat_cnt;
  average_rr /= beat_cnt;
  rmssd_rr /= beat_cnt;
  rmssd_rr = sqrt(rmssd_rr);

  for(i=0; i<beat_cnt; i++)
  {
    sdnn_bpm += (buf_bpm[i] - average_bpm) * (buf_bpm[i] - average_bpm);
    sdnn_rr += (beat_interval_list[i] - average_rr) * (beat_interval_list[i] - average_rr);
  }

  sdnn_bpm = sqrt(sdnn_bpm / beat_cnt);
  sdnn_rr = sqrt(sdnn_rr / beat_cnt);

  ecg_stats->beat_cnt = beat_cnt;
  ecg_stats->mean_rr = average_rr * 1000.0;
  ecg_stats->sdnn_rr = sdnn_rr * 1000.0;
  ecg_stats->rmssd_rr = rmssd_rr;
  ecg_stats->mean_hr = average_bpm;
  ecg_stats->sdnn_hr = sdnn_bpm;
  ecg_stats->NN20 = NN20;
  ecg_stats->NN50 = NN50;
  ecg_stats->pNN20 = (NN20 * 100.0) / (beat_cnt - 1);
  ecg_stats->pNN50 = (NN50 * 100.0) / (beat_cnt - 1);

  free(buf_bpm);
  buf_bpm = NULL;

  return 0;
}