Esempio n. 1
0
INTEGER ungetann_(INTEGER *annotator, INTEGER *time, INTEGER *anntyp, INTEGER *subtyp, INTEGER *chan, INTEGER *num, char *aux)
{
    static WFDB_Annotation oann;
    int i, j;

    oann.time = *time;
    oann.anntyp = *anntyp;
    oann.subtyp = *subtyp;
    oann.chan = *chan;
    oann.num = *num;
    oann.aux = aux;
    return (ungetann((WFDB_Annotator)(*annotator), &oann));
}
Esempio n. 2
0
main(int argc, char **argv)
{
    char *oaname = "gqf";
    double HR;
    FILE *config = NULL;
    int a0 = 0, a1 = 0, **count, dn, i, ibest, ichosen = 0, j, *mbuf,
	n, niann = 0, nseg, tdn;
    WFDB_Anninfo *a;
    WFDB_Annotation annot;
    WFDB_Frequency spm;
    WFDB_Time t0, tf;

    pname = prog_name(argv[0]);

    for (i = 1; i < argc; i++) {
	if (*argv[i] == '-') switch (*(argv[i]+1)) {
	  case 'a':	/* input annotator names */
	      for (a0 = a1 = ++i; a1 < argc && *argv[a1] != '-'; a1++, i++)
		;
	    if ((niann = a1 - a0) < 2) {
		(void)fprintf(stderr,
			      "%s: input annotator names must follow -a\n",
			      pname);
		cleanup(1);
	    }
	    break;
	  case 'c':	/* configuration file */
	    if (++i >= argc) {
		(void)fprintf(stderr,
		     "%s: name of configuration file must follow -c\n", pname);
		cleanup(1);
	    }
	    if ((config = fopen(argv[i], "rt")) == NULL) {
		(void)fprintf(stderr,
		     "%s: can't read configuration file %s\n", pname, argv[i]);
		cleanup(1);
	    }
	    break;
	  case 'h':	/* help requested */
	    help();
	    cleanup(0);
	    break;
	  case 'o':	/* output annotator name */
	    if (++i >= argc) {
		(void)fprintf(stderr,
			      "%s: output annotator name must follow -o\n",
			      pname);
		cleanup(1);
	    }
	    oaname = argv[i];
	    break;
	  case 'r':	/* record name */
	    if (++i >= argc) {
		(void)fprintf(stderr, "%s: input record name must follow -r\n",
			      pname);
		cleanup(1);
	    }
	    record = argv[i];
	    break;
	  default:
	    (void)fprintf(stderr, "%s: unrecognized option %s\n", pname,
			  argv[i]);
	    cleanup(1);
	}
	else {
	    (void)fprintf(stderr, "%s: unrecognized argument %s\n", pname,
			  argv[i]);
	    cleanup(1);
	}
    }

    /* Quit unless record name and at least two annotator names specified. */
    if (record == NULL || niann < 2) {
	help();
	cleanup(1);
    }

    /* Set up annotator info. */
    a = gcalloc(sizeof(WFDB_Anninfo), niann+1);
    for (i = a0, j = 0; i < a1; i++, j++) {
	a[j].name = argv[i]; a[j].stat = WFDB_READ;
    }
    a[j].name = oaname; a[j].stat = WFDB_WRITE;

    /* Read a priori physiologic parameters from the configuration file if
       available. They can be adjusted in the configuration file for pediatric,
       fetal, or animal ECGs. */
    if (config) {
	char buf[256], *p;

	/* Read the configuration file a line at a time. */
	while (fgets(buf, sizeof(buf), config)) {
	    /* Skip comments (empty lines and lines beginning with `#'). */
	    if (buf[0] == '#' || buf[0] == '\n') continue;
	    /* Set parameters.  Each `getconf' below is executed once for
	       each non-comment line in the configuration file. */
	    getconf(HR, "%lf");
	}
	fclose(config);
    }

    /* If any a priori parameters were not specified in the configuration file,
       initialize them here (using values chosen for adult human ECGs). */
    if (HR == 0.0) HR = 75;

    spm = 60.0 * sampfreq(record);  /* sample intervals per minute */
    nseg = strtim("e")/spm;	    /* number of complete 1-minute intervals */

    /* Set up buffer for median calculation. */
    mbuf = gcalloc(sizeof(int), niann + 1);
    mbuf[niann] = HR;	/* initial median is HR */

    /* Set up arrays for HR minute-by-minute time series. */
    count = gcalloc(sizeof(int *), niann + 1);
    for (i = 0; i < niann; i++)
	count[i] = gcalloc(sizeof(int), nseg+1);

    /* Pass 1: Load the HR arrays. */
    if (annopen(record, a, niann) < 0) cleanup(2);
    for (i = 0; i < niann; i++) {
	for (tf = spm, j = 0; j < nseg+1; tf += spm, j++) {
	    n = 0;
	    while (getann(i, &annot) == 0 && annot.time < tf)
		if (isqrs(annot.anntyp)) n++;
	    count[i][j] = n;
	}
    }
    wfdbquit();

    /* Pass 2: Select the input annotations to be copied, and copy them. */
    if (annopen(record, a, niann+1) < 0) cleanup(2);
    for (j = 0, t0 = 0, tf = spm; j < nseg+1; j++, t0 = tf, tf += spm) {
	for (i = 0; i < niann; i++)
	    mbuf[i] = count[i][j];
	/* Note that the last element, mbuf[niann], is the previous median. */
	qsort(mbuf, niann+1, sizeof(int), icomp);
	/* If mbuf has an even number of members, the median is the mean of
	   the two central values. */
	if (niann & 1) mbuf[niann] = (mbuf[(niann-1)/2] + mbuf[(niann+1)/2])/2;
	/* Otherwise it's the single central value. */
	else mbuf[niann] = mbuf[niann/2];
	/* Find the input that best matches the prediction (the median). */
	for (i = 0, dn = 9999; i < niann; i++) {
	    tdn = mbuf[niann] - count[i][j];
	    if (tdn < 0) tdn = -tdn;
	    if (tdn < dn) { dn = tdn; ibest = i; }
	}
	/* If the best match is not the previously chosen input, avoid switching
	   the input unless it's more than 2 beats further from the median. */
	if (ibest != ichosen) {
	    tdn = mbuf[niann] - count[ichosen][j];
	    if (tdn < 0) tdn = -tdn;
	    if (tdn < dn + 3) ibest = ichosen;
	}
	ichosen = ibest;
	/* Copy the chosen annotations for this one-minute segment. */
	n = 0;
	while (getann(ichosen, &annot) == 0 && n <= count[ichosen][j]) {
	    if (annot.time >= t0 && isqrs(annot.anntyp)) {
		putann(0, &annot);
		n++;
	    }
	}
	ungetann(ichosen, &annot);
    }
    cleanup(0);
}
Esempio n. 3
0
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 = '*';
}
Esempio n. 4
0
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 = '*';
}