-
Notifications
You must be signed in to change notification settings - Fork 0
/
BXB.CPP
1605 lines (1508 loc) · 43.9 KB
/
BXB.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* file: bxb.c G. Moody 14 December 1987
Revised: 7 November 2001
Revised: 5/13/2002 -- Patrick Hamilton
-------------------------------------------------------------------------------
bxb: ANSI/AAMI-standard beat-by-beat annotation file comparator
Copyright (C) 2001 George B. Moody
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 2 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, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA.
You may contact the author by e-mail ([email protected]) or postal mail
(MIT Room E25-505A, Cambridge, MA 02139 USA). For updates to this software,
please visit PhysioNet (http://www.physionet.org/).
_______________________________________________________________________________
This program implements the beat-by-beat comparison algorithms described in
AAMI/ANSI EC38:1998, the American National Standard for ambulatory ECGs, and
in AAMI EC57:1998, the American National Standard for Testing and Reporting
Performance Results of Cardiac Rhythm and ST Segment Measurement Algorithms.
These standards are available from AAMI, 1110 N Glebe Road, Suite 220,
Arlington, VA 22201 USA (http://www.aami.org/).
The -f, -O, -t, and -w options modify the comparison algorithm used by bxb in
ways not permitted by EC38:1998 or EC57:1998. These options are provided for
the use of developers, who may find them useful for obtaining a more detailed
understanding of algorithm errors.
This version of bxb.cpp has been modified to run without command line input,
batch comparing .ate annotations to .atr annotations for MIT/BIH database
or AHA database files. The test maticies are stored in "testrpt.txt". The
Record #, QRS TP, QRS FN, QRS FP, PVC TP, PVC FN, and PVC FP are stored in
the file "adstat.txt".
*/
#define MITDB // Comment out to batch run AHA data instead of MIT/BIH data.
#ifdef MITDB
#define ECG_DB_PATH "C:\\MITDB\\" // MIT/BIH database directory.
#define REC_COUNT 48
int Records[REC_COUNT] = {100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 111, 112,
113, 114, 115, 116, 117, 118, 119, 121, 122, 123, 124, 200,
201, 202, 203, 205, 207, 208, 209, 210, 212, 213, 214, 215,
217, 219, 220, 221, 222, 223, 228, 230, 231, 232, 233, 234};
#else
#define ECG_DB_PATH "C:\\AHADAT~1\\" // AHA database directory
#define REC_COUNT 69
int Records[REC_COUNT] = {1201, 1202, 1203, 1204, 1205, 1206, 1207, 1208, 1209, 1210,
2201, 2203, 2204, 2205, 2206, 2207, 2208, 2209, 2210,
3201, 3202, 3203, 3204, 3205, 3206, 3207, 3208, 3209, 3210,
4201, 4202, 4203, 4204, 4205, 4206, 4207, 4208, 4209, 4210,
5201, 5202, 5203, 5204, 5205, 5206, 5207, 5208, 5209, 5210,
6201, 6202, 6203, 6204, 6205, 6206, 6207, 6208, 6209, 6210,
7201, 7202, 7203, 7204, 7205, 7206, 7207, 7208, 7209, 7210};
#endif
#include <stdio.h>
#include <stdlib.h> // For exit.
#include <math.h> /* for declaration of sqrt() */
#ifndef BSD
#include <string.h>
#else
#include <strings.h>
#endif
#ifndef __STDC__
extern void exit();
#endif
#include "wfdb.h"
#define map1
#define map2
#define ammap
#define mamap
#define annpos
#include "ecgmap.h"
#define abs(A) ((A) >= 0 ? (A) : -(A))
// Local Prototypes.
void NewInit(void);
int rpann(long t);
int tpann(long t);
void pair(int ref, int test);
void print_results(int fflag);
int amap(int a);
char *prog_name(char *s);
void pstat(char *s, char *f, long a, long b);
void sstat(char *s, char *f, long a, long b);
void NewPrintResults(void);
// Global Variables
char *pname; /* name by which this program was invoked */
int A, Aprime; /* types of the current & next reference annotations */
int a, aprime; /* types of the current & next test annotations */
int fflag = 3; /* report format (0: none; 1: compressed; 2: line;
3: standard; 4: compressed with SVEB; 5: line
with SVEB; 6: standard with SVEB) */
int match_dt = 0; /* match window duration in samples */
int Oflag = 0; /* if non-zero, produce an extended annotation file */
long shut_down; /* duration of test annotator's shutdown */
long start; /* time of the beginning of the test period */
long end_time; /* end of the test period (-1: end of reference annot
file; 0: end of either annot file) */
long huge_time = 0x7FFFFFFF; /* largest possible time */
long T, Tprime; /* times of the current & next reference annotations */
long t, tprime; /* times of the current & next test annotations */
char record[20]; /* record name */
int main()
{
void genxcmp(), getref(), gettest(), init();
int recNum;
/* Read and interpret command-line arguments. */
// init(argc, argv);
for (recNum = 0; recNum < REC_COUNT; ++recNum)
{
sprintf(record, "%d", Records[recNum]);
printf("%s\n", record);
NewInit();
/* Set A and T to the type and time of the first reference annotation after
the end of the learning period. */
do
{
getref();
} while (T < start);
/* Set aprime and tprime to the type and time of the first test annotation
after the end of the learning period, and a and t to the type and time
of the last test annotation in the learning period. */
do
{
gettest();
} while (tprime < start);
/* If an extended output annotation file was requested, produce it and
exit. */
if (Oflag)
{
genxcmp();
wfdbquit(); //**
exit(0);
}
/* If t matches the first reference annotation, count it and get the next
annotation from each file. (Since T >= start and t < start, T-t must be
positive.) */
if (T - t < abs(T - tprime) && T - t <= match_dt)
{
if (A != 0 || a != 0) /* false only if start = 0 */
pair(A, a);
getref();
gettest();
}
/* If there is a test annotation within an interval equal to the match
window following the beginning of the test period, and there is no
match, go on to the next test annotation without counting the first
one. */
else
{
gettest();
if (t - start <= match_dt && abs(T - tprime) < abs(T - t))
gettest();
}
/* Peform the comparison. Each time through the loop below, a beat label
pair is identified and counted (or else a non-beat annotation is
discarded), and an annotation is read from each file from which an
annotation was paired or discarded. Note that only one of the four
numbered actions is performed on each iteration.
The complex loop termination condition is dependent on end_time, which
is not changed during execution of the loop. There are three ways the
loop termination condition can be satisfied:
- If the length of the comparison is known, either because it was
specified using the `-t' option or because the header file specifies
the record length, the loop ends when both T and t are greater than
end_time. This is the usual case.
- If the length of the comparison is unknown (end_time = -1), the loop
ends when EOF is reached in the reference annotation file (T =
huge_time).
- If the option `-t 0' was specified (end_time = 0), the loop ends when
EOF is first reached in either annotation file (T or t = huge_time).
*/
while ((end_time > 0L && (T <= end_time || t <= end_time)) ||
(end_time == -1L && T != huge_time) ||
(end_time == 0L && T != huge_time && t != huge_time))
{
if (t < T)
{ /* test annotation is earliest */
/* (1) If t is within the match window, and is a better match than
the next test annotation, pair it. */
if (T - t <= match_dt && T - t < abs(T - tprime))
{
pair(A, a);
getref();
gettest();
}
/* (2) There is no match to the test annotation, so pair it with a
pseudo-beat annotation and get the next one. */
else
{
pair(rpann(t), a);
gettest();
}
}
else
{ /* reference annotation is earliest */
/* (3) If T is within the match window, and is a better match than
the next reference annotation, pair it. */
if (t - T <= match_dt && t - T < abs(t - Tprime))
{
pair(A, a);
gettest();
getref();
}
/* (4) There is no match to the reference annotation, so pair it
with a pseudo-beat annotation and get the next one. */
else
{
pair(A, tpann(T));
getref();
}
}
}
shut_down /= strtim("1"); /* convert from samples to seconds */
/* Generate output. */
print_results(fflag);
NewPrintResults();
wfdbquit(); /* close input files */
}
return 0;
// exit(0); /*NOTREACHED*/
}
/* getref() and gettest() read the next beat annotations from their respective
files. */
struct WFDB_anninfo an[3];
unsigned int oflag = 0; /* if non-zero, produce an output annotation file */
long RR; /* reference RR interval, if non-zero */
long sdonref = -1L; /* start of reference shutdown */
long sdoffref = -1L; /* end of reference shutdown */
long vfonref = -1L; /* start of reference VF */
long vfoffref = -1L; /* end of reference VF */
long psdonref = -1L; /* start of previous reference shutdown */
long psdoffref = -1L; /* end of previous reference shutdown */
long pvfonref = -1L; /* start of previous reference VF */
long pvfoffref = -1L; /* end of previous reference VF */
struct WFDB_ann ref_annot;
void getref() /* get next reference beat annotation */
{
static long TT; /* time of previous reference beat annotation */
static struct WFDB_ann annot;
TT = T;
T = Tprime;
A = Aprime;
/* T-TT is not a valid RR interval if T is the time of the first beat,
if TT is the time of the last beat, or if a period of VF or shutdown
occurs between TT and T. */
if (TT == 0L || T == huge_time ||
(TT <= vfonref && vfonref < T) ||
(TT <= sdonref && sdonref < T) ||
(TT <= pvfonref && pvfonref < T) ||
(TT <= psdonref && psdonref < T))
RR = 0L;
else
RR = T - TT;
if (oflag)
ref_annot = annot;
/* Read reference annotations until a beat annotation is read, or EOF.
If an expanded output annotation file is required, all annotations
are treated as if they were beat annotations. */
while (getann(0, &annot) == 0)
{
if (isqrs(annot.anntyp) || Oflag)
{ /* beat annotation */
Tprime = annot.time;
Aprime = amap(annot.anntyp);
return;
}
/* Shutdown occurs when neither signal is readable; the beginning of
shutdown is indicated by a NOISE annotation in which bits 4 and 5
of the subtyp field are set, and the end of shutdown is indicated
by a NOISE annotation with any value of `subtyp' for which at least
one of bits 4 and 5 is zero. In AHA DB reference annotation files,
shutdown is indicated by a single shutdown annotation placed roughly
in the middle of the shutdown interval; in this case, shutdown is
assumed to begin match_dt samples after the previous beat annotation
or VFOFF annotation, and is assumed to end match_dt samples before
the next annotation.
*/
else if (annot.anntyp == NOISE)
{
if ((annot.subtyp & 0x30) == 0x30)
{
psdonref = sdonref;
psdoffref = sdoffref;
sdonref = annot.time;
/* Read next annotation, which should mark end of shutdown. */
if (getann(0, &annot) < 0)
{ /* EOF before end of shutdown */
Tprime = sdoffref = huge_time;
Aprime = '*';
return;
}
if (annot.anntyp == NOISE &&
(annot.subtyp & 0x30) != 0x30)
sdoffref = annot.time;
else
{
if (vfoffref > T)
sdonref = vfoffref + match_dt;
else
sdonref = T + match_dt;
sdoffref = annot.time - match_dt;
if (sdonref > sdoffref)
sdonref = sdoffref;
(void)ungetann(0, &annot);
}
}
}
/* The beginning of ventricular fibrillation is indicated by a VFON
annotation, and its end by a VFOFF annotation; any annotations
between VFON and VFOFF are read and ignored. */
else if (annot.anntyp == VFON)
{
pvfonref = vfonref;
pvfoffref = vfoffref;
vfonref = annot.time;
/* Read additional annotations, until end of VF or EOF. */
do
{
if (getann(0, &annot) < 0)
{ /* EOF before end of VF */
Tprime = huge_time;
Aprime = '*';
return;
}
} while (annot.anntyp != VFOFF);
vfoffref = annot.time;
}
}
/* When this statement is reached, there are no more annotations in the
reference annotation file. */
Tprime = huge_time;
Aprime = '*';
}
long rr; /* test RR interval, if non-zero */
long sdontest = -1L; /* start of test shutdown */
long sdofftest = -1L; /* end of test shutdown */
long vfontest = -1L; /* start of test VF */
long vfofftest = -1L; /* end of test VF */
long psdontest = -1L; /* start of previous test shutdown */
long psdofftest = -1L; /* end of previous test shutdown */
long pvfontest = -1L; /* start of previous test VF */
long pvfofftest = -1L; /* end of previous test VF */
struct WFDB_ann test_annot;
void gettest() /* get next test annotation */
{
static long tt; /* time of previous test beat annotation */
static struct WFDB_ann annot;
tt = t;
t = tprime;
a = aprime;
/* See comments on the similar code in getref(), above. */
if (tt == 0L || t == huge_time ||
(tt <= vfontest && vfontest < t) ||
(tt <= sdontest && sdontest < t) ||
(tt <= pvfontest && pvfontest < t) ||
(tt <= psdontest && psdontest < t))
rr = 0L;
else
rr = t - tt;
if (oflag)
test_annot = annot;
while (getann(1, &annot) == 0)
{
if (isqrs(annot.anntyp) || Oflag)
{
tprime = annot.time;
aprime = amap(annot.anntyp);
return;
}
if (annot.anntyp == NOISE)
{
if ((annot.subtyp & 0x30) == 0x30)
{
psdontest = sdontest;
psdofftest = sdofftest;
sdontest = annot.time;
if (getann(1, &annot) < 0)
{
tprime = huge_time;
aprime = '*';
if (end_time > 0L)
shut_down += end_time - sdontest;
else
{
(void)fprintf(stderr,
"%s: unterminated shutdown starting at %s in record %s, annotator %s\n",
pname, timstr(sdontest), record, an[1].name);
(void)fprintf(stderr,
" (not included in shutdown duration measurement)\n");
}
return;
}
if (annot.anntyp == NOISE &&
(annot.subtyp & 0x30) != 0x30)
sdofftest = annot.time;
else
{
if (vfofftest > t)
sdontest = vfofftest + match_dt;
else
sdontest = t + match_dt;
sdofftest = annot.time - match_dt;
if (sdontest > sdofftest)
sdontest = sdofftest;
(void)ungetann(0, &annot);
}
/* update shutdown duration tally */
shut_down += sdofftest - sdontest;
}
}
else if (annot.anntyp == VFON)
{
pvfontest = vfontest;
pvfofftest = vfofftest;
vfontest = annot.time;
do
{
if (getann(1, &annot) < 0)
{
tprime = huge_time;
aprime = '*';
return;
}
} while (annot.anntyp != VFOFF);
vfofftest = annot.time;
}
}
tprime = huge_time;
aprime = '*';
}
/* Functions rpann() and tpann() return the appropriate pseudo-beat label
for the time specified by their argument. They should be called only
with time arguments which match the times of the current test or reference
beat labels, since they depend on getref() and gettest() to locate the two
most recent VF and shutdown periods and have no information about earlier
or later VF or shutdown periods. */
int rpann(long t)
{
if ((vfonref != -1L && vfonref <= t && (t <= vfoffref || vfoffref == -1L)) ||
(pvfonref != -1L && pvfonref <= t && t <= pvfoffref))
return ('*'); /* test beat labels during reference-marked VF are
not to be counted; since `*' is not recognized by
pair(), returning `*' accomplishes this */
else if ((sdonref != -1L && sdonref <= t && (t <= sdoffref || sdoffref == -1L)) ||
(psdonref != -1L && psdonref <= t && t <= psdoffref))
return ('X'); /* test beat labels during reference-marked shutdown
are paired with X pseudo-beat labels */
else
return ('O'); /* all other extra test beat labels are paired with
O pseudo-beat labels */
}
int tpann(long t)
{
/* no special treatment for reference beat labels during test-marked VF */
if ((sdontest != -1L && sdontest <= t && (t <= sdofftest || sdofftest == -1L)) ||
(psdontest != -1L && psdontest <= t && t <= psdoffref))
return ('X'); /* reference beat labels during test-marked shutdown
are paired with X pseudo-beat labels */
else
return ('O'); /* all other extra reference beat labels are paired
with O pseudo-beat labels */
}
/* Define counters for the elements of the confusion matrix. Static variables
have initial values of zero. */
static long Nn, Ns, Nv, Nf, Nq, No, Nx,
Sn, Ss, Sv, Sf, Sq, So, Sx,
Vn, Vs, Vv, Vf, Vq, Vo, Vx,
Fn, Fs, Fv, Ff, Fq, Fo, Fx,
Qn, Qs, Qv, Qf, Qq, Qo, Qx,
On, Os, Ov, Of, Oq,
Xn, Xs, Xv, Xf, Xq;
int verbose = 0; /* if non-zero, describe all mismatches */
long nrre = 0; /* number of RR errors tallied in ssrre */
double ssrre = 0.; /* sum of squares of RR errors */
void pair(int ref, int test) /* count a beat label pair */
{
switch (ref)
{
case 'N':
switch (test)
{
case 'N':
Nn++;
break;
case 'S':
Ns++;
break;
case 'V':
Nv++;
break;
case 'F':
Nf++;
break;
case 'Q':
Nq++;
break;
case 'O':
No++;
break;
case 'X':
Nx++;
break;
}
break;
case 'S':
switch (test)
{
case 'N':
Sn++;
break;
case 'S':
Ss++;
break;
case 'V':
Sv++;
break;
case 'F':
Sf++;
break;
case 'Q':
Sq++;
break;
case 'O':
So++;
break;
case 'X':
Sx++;
break;
}
break;
case 'V':
switch (test)
{
case 'N':
Vn++;
break;
case 'S':
Vs++;
break;
case 'V':
Vv++;
break;
case 'F':
Vf++;
break;
case 'Q':
Vq++;
break;
case 'O':
Vo++;
break;
case 'X':
Vx++;
break;
}
break;
case 'F':
switch (test)
{
case 'N':
Fn++;
break;
case 'S':
Fs++;
break;
case 'V':
Fv++;
break;
case 'F':
Ff++;
break;
case 'Q':
Fq++;
break;
case 'O':
Fo++;
break;
case 'X':
Fx++;
break;
}
break;
case 'Q':
switch (test)
{
case 'N':
Qn++;
break;
case 'S':
Qs++;
break;
case 'V':
Qv++;
break;
case 'F':
Qf++;
break;
case 'Q':
Qq++;
break;
case 'O':
Qo++;
break;
case 'X':
Qx++;
break;
}
break;
case 'O':
switch (test)
{
case 'N':
On++;
break;
case 'S':
Os++;
break;
case 'V':
Ov++;
break;
case 'F':
Of++;
break;
case 'Q':
Oq++;
break;
}
break;
case 'X':
switch (test)
{
case 'N':
Xn++;
break;
case 'S':
Xs++;
break;
case 'V':
Xv++;
break;
case 'F':
Xf++;
break;
case 'Q':
Xq++;
break;
}
break;
}
/* Compute the RR interval error and update the sum of squared errors. */
if (RR > 0L && rr > 0L)
{
double rre = RR - rr;
ssrre += rre * rre;
nrre++;
}
if (oflag)
{
if (ref == test)
(void)putann(0, &test_annot);
else
{
struct WFDB_ann out_annot;
char auxp[3];
auxp[0] = 2;
auxp[1] = ref;
auxp[2] = test - 'A' + 'a';
if (test == 'O' || test == 'X')
out_annot.time = T;
else
out_annot.time = t;
out_annot.anntyp = NOTE;
out_annot.subtyp = out_annot.chan = out_annot.num = 0;
out_annot.aux = auxp;
(void)putann(0, &out_annot);
}
}
if (verbose && ref != test)
{
if (ref == 'O' || ref == 'X')
(void)fprintf(stderr, "%c(%ld)/%c(%ld)\n", ref, t, test, t);
else if (test == 'O' || test == 'X')
(void)fprintf(stderr, "%c(%ld)/%c(%ld)\n", ref, T, test, T);
else
(void)fprintf(stderr, "%c(%ld)/%c(%ld)\n", ref, T, test, t);
}
}
int amap(int a) /* map MIT annotation code into AAMI test label */
{
switch (a)
{
case NORMAL:
case LBBB:
case RBBB:
case BBB:
return ('N');
case NPC:
case APC:
case SVPB:
case ABERR:
case NESC:
case AESC:
case SVESC:
return (fflag > 3 ? 'S' : 'N');
case PVC:
case RONT:
case VESC:
return ('V');
case FUSION:
return ('F');
case UNKNOWN:
return ('Q');
/* The AAMI RP excludes records containing paced beats from its reporting
requirements. To permit this program to be used with such records,
beats which are either paced (type PACE) or fusions of paced and normal
beats (type PFUS) are treated in the same way as unknown beats. */
case PACE:
case PFUS:
return ('Q');
/* LEARN annotations should appear only in the `test' annotation file, and
only during the learning period; if they appear elsewhere, they are
treated in the same way as unknown beats. */
case LEARN:
return ('Q');
/* Other annotations (including NOISE and VFON/VFOFF) are treated as non-beat
annotations. */
default:
return ('O');
}
}
FILE *ofile, *sfile; /* files for beat-by-beat and shutdown reports */
/* `pstat' prints a statistic described by s, defined as the quotient of a and
b expressed in percentage units. Undefined values are indicated by `-'. */
void pstat(char *s, char *f, long a, long b)
{
if (fflag == 1 || fflag == 3 || fflag == 4 || fflag == 6)
{
(void)fprintf(ofile, "%s: ", s);
if (b <= 0)
(void)fprintf(ofile, " - ");
else
{
(void)fprintf(ofile, f, (100. * a) / b);
(void)fprintf(ofile, "%%");
}
(void)fprintf(ofile, " (%ld/%ld)\n", a, b);
}
else if (b <= 0)
(void)fprintf(ofile, " -");
else
{
(void)fprintf(ofile, " ");
(void)fprintf(ofile, f, (100. * a) / b);
}
}
/* `sstat' prints a statistic as for `pstat', but the output goes to sfile. */
void sstat(char *s, char *f, long a, long b)
{
if (fflag == 1 || fflag == 3 || fflag == 4 || fflag == 6)
{
(void)fprintf(sfile, "%s: ", s);
if (b <= 0)
(void)fprintf(sfile, " - ");
else
{
(void)fprintf(sfile, f, (100. * a) / b);
(void)fprintf(sfile, "%%");
}
(void)fprintf(sfile, " (%ld/%ld)\n", a, b);
}
else if (b <= 0)
(void)fprintf(sfile, " -");
else
{
(void)fprintf(sfile, " ");
(void)fprintf(sfile, f, (100. * a) / b);
}
}
char *ofname = "-", *sfname; /* filenames for reports */
/* Read and interpret command-line arguments. */
void init(int argc, char *argv[])
{
int i;
void help();
pname = prog_name(argv[0]);
for (i = 1; i < argc; i++)
{
if (*argv[i] == '-')
switch (*(argv[i] + 1))
{
case 'a': /* annotator names follow */
if (++i >= argc - 1)
{
(void)fprintf(stderr,
"%s: reference and test annotator names must follow -a\n",
pname);
exit(0);
}
an[0].name = argv[i];
an[1].name = argv[++i];
break;
case 'c': /* condensed output */
if (++i >= argc)
{
(void)fprintf(stderr, "%s: output file name must follow -c\n",
pname);
exit(0);
}
ofname = argv[i];
fflag = 1;
break;
case 'C': /* condensed output with SVEB statistics */
if (++i >= argc)
{
(void)fprintf(stderr, "%s: output file name must follow -C\n",
pname);
exit(0);
}
ofname = argv[i];
fflag = 4;
break;
case 'f': /* start time follows */
if (++i >= argc)
{
(void)fprintf(stderr, "%s: start time must follow -f\n", pname);
exit(0);
}
start = i; /* save arg index, convert to samples later, when
record has been opened and sampling frequency is
known */
break;
case 'h': /* print usage summary */
help();
exit(0);
break;
case 'l': /* line-format output */
if (++i >= argc - 1)
{
(void)fprintf(stderr,
"%s: two output file names must follow -l\n",
pname);
exit(0);
}
ofname = argv[i];
sfname = argv[++i];
fflag = 2;
break;
case 'L': /* line-format output, with SVEB statistics */
if (++i >= argc - 1)
{
(void)fprintf(stderr,
"%s: two output file names must follow -L\n",
pname);
exit(0);
}
ofname = argv[i];
sfname = argv[++i];
fflag = 5;
break;
case 'o': /* generate output annotation file */
oflag = 1;
break;
case 'O': /* generate expanded output annotation file */
oflag = 1;
Oflag = 1;
fflag = 0;
break;
case 'r': /* record name follows */
if (++i >= argc)
{
(void)fprintf(stderr,
"%s: record name must follow -r\n", pname);
exit(0);
}
// record = argv[i];
break;
case 's': /* standard-format output */
if (++i >= argc)
{
(void)fprintf(stderr, "%s: output file name must follow -s\n",
pname);
exit(0);
}
ofname = argv[i];
fflag = 3;
break;
case 'S': /* standard-format output, with SVEB statistics */
if (++i >= argc)
{
(void)fprintf(stderr, "%s: output file name must follow -S\n",
pname);
exit(0);
}
ofname = argv[i];
fflag = 6;
break;
case 't': /* end time follows */
if (++i >= argc)
{
(void)fprintf(stderr, "%s: end time must follow -t\n", pname);
exit(0);
}
end_time = i;
break;
case 'v': /* verbose mode */
verbose = 1;
break;
case 'w': /* match window follows */
if (++i >= argc)
{
(void)fprintf(stderr,
"%s: match window must follow -w\n", pname);
exit(0);
}
match_dt = i;
break;
default:
(void)fprintf(stderr,
"%s: unrecognized option %s\n", pname, argv[i]);
exit(0);
}
else
{
(void)fprintf(stderr,
"%s: unrecognized argument %s\n", pname, argv[i]);
exit(0);
}
}
if (!record || !an[0].name)