Go to the first, previous, next, last section, table of contents.


Example 4: Generating an R-R Interval Histogram

This program reads an annotation file, determines the intervals between beat annotations (assumed to be the R-R intervals), and accumulates a histogram of them.

 1  #include <stdio.h>
 2  #include <ecg/db.h>
 3  #include <ecg/ecgmap.h>
 4
 5  main(argc, argv)
 6  int argc;
 7  char *argv[];
 8  {
 9      int rr, *rrhist, rrmax;
10      long t;
11      DB_Anninfo a;
12      DB_Annotation annot;
13      void *calloc();
14
15      if (argc < 3) {
16          fprintf(stderr, "usage: %s annotator record\n", argv[0]);
17          exit(1);
18      }
19      a.name = argv[1]; a.stat = READ;
20      if (annopen(argv[2], &a, 1) < 0) exit(2);
21      if ((rrmax = (int)(3*sampfreq(argv[2]))) <= 0) exit(3);
22      if ((rrhist = (int *)calloc(rrmax+1, sizeof(int))) == NULL) {
23          fprintf(stderr, "%s: insufficient memory\n", argv[0]);
24          exit(4);
25      }
26      while (getann(0, &annot) == 0 && !isqrs(annot.anntyp))
27          ;
28      t = annot.time;
29      while (getann(0, &annot) == 0)
30          if (isqrs(annot.anntyp)) {
31              if ((rr = annot.time - t) > rrmax) rr = rrmax;
32              rrhist[rr]++;
33              t = annot.time;
34          }
35      for (rr = 1; rr < rrmax; rr++)
36          printf("%4d %s\n", rrhist[rr], mstimstr((long)rr));
37      printf("%4d %s (or longer)\n", rrhist[rr], mstimstr((long)rr));
38      exit(0);
39  }

Notes:

Lines 21--25:
Here we allocate storage for the histogram. The value returned by sampfreq, if positive, specifies the number of sample intervals per second; we will allocate 3 seconds' worth of bins, initialized to zero. See K&R, page 167, for a description of calloc.
Lines 26--28:
This code sets t to the time of the first annotated beat in the record.
Lines 29--34:
Here we read the remainder of the annotations, skipping any non-beat annotations. The difference between the values of annot.time for consecutive beat annotations defines an R-R interval (rr). Each possible value of rr up to rrmax is assigned a bin in rrhist. Intervals longer than 3 seconds (rrmax) are counted in the bin corresponding to rr = rrmax.
Lines 35--37:
The histogram is printed as a two-column table, with the number of intervals in the first column and the length of the interval (with millisecond resolution) in the second column. (What happens if rr starts at 0 rather than 1 in line 35?)


Go to the first, previous, next, last section, table of contents.



George B. Moody (george@hstbme.mit.edu)