Exemple #1
0
void prep_times()
{
    char *p;

    if ((p = get_param("t0")) == NULL) p = "0";
    if ((t0 = strtim(p)) < 0L) t0 = -t0;
    if ((p = get_param("dt")) == NULL) p = "1";
	
    /* dt is the amount of data to be retrieved.  On input, dt is in seconds,
       but the next block of code converts it to sample intervals.  There are
       several special cases:

       * If dt is 0 or negative, no samples are retrieved, but all annotations
       are retrieved.

       * If dt is positive but less than 1 sampling interval, it is set to 1
       sampling interval.  This occurs for records with very low sampling rates,
       such as once per minute.

       * Otherwise, if dt is longer than 2 minutes and longer than 120000 sample
       intervals, it is reduced to 2 minutes, to limit the load on the server
       from a single request.
    */
    dt = atoi(p);
    if (dt <= 0) dt = 0;
    else {
	dt *= ffreq;
	if (dt < 1) dt = 1;
	else if (dt > 120*ffreq && dt > 120000) dt = 120*ffreq;
    }
    tf = t0 + dt;
}
Exemple #2
0
void NewInit(void)
	{
	char testAnnName[20], rec ;

	// Initialize counts.
	Nn = Ns = Nv = Nf = Nq = No = Nx = 0 ;
	Sn = Ss = Sv = Sf = Sq = So = Sx =  0 ;
	Vn = Vs = Vv = Vf = Vq = Vo = Vx =  0 ;
	Fn = Fs = Fv = Ff = Fq = Fo = Fx =  0 ;
	Qn = Qs = Qv = Qf = Qq = Qo = Qx =  0 ;
	On = Os = Ov = Of = Oq =  0 ;
	Xn = Xs = Xv = Xf = Xq = 0 ;

	T = Tprime = 0 ;
	t = tprime = 0 ;


	setwfdb(ECG_DB_PATH) ;

 //	printf("Enter record: ") ;
 //	gets(record) ;

	sampfreq(record) ;
	an[0].name = "atruth" ;
	an[1].name = "atest" ;
	an[0].stat = an[1].stat = WFDB_READ ;
	if(annopen(record,an,2) < 0)
		{
		printf("Couldn't open annotation files.\n") ;
		exit(0) ;
		}

	fflag = 3 ;

	ofname = "testrpt.txt" ;	// Name of file for report.
	match_dt = (int) strtim(".15") ;
	start = strtim("5:0") ;
	end_time = -1L ;
	shut_down = (int) strtim(".5") ;
	}
Exemple #3
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);
}
Exemple #4
0
void print_results(int fflag)
	{
	long QTP, QFN, QFP, STP, SFN, SFP, VTP, VFN, VTN, VFP;

    /* Open output files.  If line-format output was selected, write column
       headings only if the files must be created from scratch. */
	if (strcmp(ofname, "-"))
		{
		if ((ofile = fopen(ofname, "r")) == NULL)
			{
			if ((ofile = fopen(ofname, "w")) == NULL)
				{
				(void)fprintf(stderr, "%s: can't create %s\n", pname, ofname);
				exit(0);
				}
			if (fflag == 2)
				{
				(void)fprintf(ofile,
			      "Record Nn' Vn' Fn' On'  Nv   Vv  Fv' Ov' No'");
				(void)fprintf(ofile,
			      " Vo' Fo'  Q Se   Q +P   V Se   V +P  V FPR\n");
				}
			else if (fflag == 5)
				{
				(void)fprintf(ofile,
			      "Record Nn' Sn' Vn' Fn' On'  Ns  Ss  Vs  Fs'");
				(void)fprintf(ofile,
			      " Os' Nv  Sv   Vv  Fv' Ov' No' So' Vo' Fo'");
				(void)fprintf(ofile,
					"  Q Se   Q +P   V Se   V +P   S Se   S +P RR err\n");
				}
			}
		else
			{
			(void)fclose(ofile);
			if ((ofile = fopen(ofname, "a")) == NULL)
				{
				(void)fprintf(stderr, "%s: can't modify %s\n", pname, ofname);
				exit(0);
				}
			}
		}
	else ofile = stdout;
	if (fflag == 2 || fflag == 5)
		{
		if (strcmp(sfname, "-"))
			{
			if ((sfile = fopen(sfname, "r")) == NULL)
				{
				if ((sfile = fopen(sfname, "w")) == NULL)
					{
					(void)fprintf(stderr,
						"%s: can't create %s\n", pname, sfname);
					exit(0);
					}
				if (fflag == 2) {
		    (void)fprintf(sfile,
			    "Record Nx   Vx   Fx   Qx  %% beats  %% N    ");
		    (void)fprintf(sfile, "%% V    %% F   Total Shutdown\n");
		    (void)fprintf(sfile,
			    "                           missed missed ");
		    (void)fprintf(sfile, "missed missed      Time\n");
		}
		else {
		    (void)fprintf(sfile,
			  "Record Nx   Sx   Vx   Fx   Qx  %% beats  %% N    ");
		    (void)fprintf(sfile,
				  "%% S    %% V    %% F   Total Shutdown\n");
		    (void)fprintf(sfile,
			    "                                missed missed ");
		    (void)fprintf(sfile, "missed missed missed      Time\n");
		}
	    }
	    else {
		(void)fclose(sfile);
		if ((sfile = fopen(sfname, "a")) == NULL) {
		    (void)fprintf(stderr,
				  "%s: can't modify %s\n", pname, sfname);
		    exit(0);
		}
	    }
	}
	else sfile = stdout;
    }
    else sfile = stdout;

    if (fflag == 1 || fflag == 3 || fflag == 4 || fflag == 6) {
	(void)fprintf(ofile, "Beat-by-beat comparison results for record %s\n",
		      record);
	(void)fprintf(ofile, "Reference annotator: %s\n", an[0].name);
	(void)fprintf(ofile, "     Test annotator: %s\n\n", an[1].name);
    }

    switch (fflag) {
      case 1:	/* print condensed format summary tables */
	(void)fprintf(ofile, "         Algorithm\n");
	(void)fprintf(ofile, "      n+f+q    v  o+x\n");
	(void)fprintf(ofile, "     ________________\n");
	(void)fprintf(ofile, "  N  | %4ld %4ld %4ld\n",
		      Nn+Ns+Nf+Nq + Sn+Ss+Sf+Sq, Nv + Sv, No+Nx + So+Sx);
	(void)fprintf(ofile, "  V  | %4ld %4ld %4ld\n",
		      Vn+Vs+Vf+Vq, Vv, Vo+Vx);
	(void)fprintf(ofile, " F+Q | %4ld %4ld %4ld\n",
		      Fn+Fs+Ff+Fq + Qn+Qs+Qf+Qq, Fv + Qv, Fo+Fx + Qo+Qx);
	(void)fprintf(ofile, " O+X | %4ld %4ld\n\n",
		      On+Os+Of+Oq + Xn+Xs+Xf+Xq, Ov+Xv);
	break;
      case 2:	/* print line-format output */
	(void)fprintf(ofile,
	      "%4s %5ld %3ld %3ld %3ld %3ld %4ld %3ld %3ld %3ld %3ld %3ld",
		      record,
		      Nn+Ns+Nf+Nq + Sn+Ss+Sf+Sq,
		      Vn+Vs+Vf+Vq,
		      Fn+Fs+Ff+Fq + Qn+Qs+Qf+Qq,
		      On+Os+Of+Oq + Xn+Xs+Xf+Xq,
		      Nv+Sv, Vv, Fv+Qv, Ov+Xv,
		      No+Nx+So+Sx, Vo+Vx, Fo+Fx+Qo+Qx);
	(void)fprintf(sfile, "%4s %4ld %4ld %4ld %4ld  ",
		      record, Nx+Sx, Vx, Fx, Qx);
	break;
      case 3:	/* print standard format summary tables */
	(void)fprintf(ofile, "               Algorithm\n");
	(void)fprintf(ofile, "        n    v    f    q    o    x\n");
	(void)fprintf(ofile, "   _______________________________\n");
	(void)fprintf(ofile, " N | %4ld %4ld %4ld %4ld %4ld %4ld\n",
		      Nn+Ns+Sn+Ss, Nv+Sv, Nf+Sf, Nq+Sq, No+So, Nx+Sx);
	(void)fprintf(ofile, " V | %4ld %4ld %4ld %4ld %4ld %4ld\n",
		      Vn+Vs, Vv, Vf, Vq, Vo, Vx);
	(void)fprintf(ofile, " F | %4ld %4ld %4ld %4ld %4ld %4ld\n",
		      Fn+Fs, Fv, Ff, Fq, Fo, Fx);
	(void)fprintf(ofile, " Q | %4ld %4ld %4ld %4ld %4ld %4ld\n",
		      Qn+Qs, Qv, Qf, Qq, Qo, Qx);
	(void)fprintf(ofile, " O | %4ld %4ld %4ld %4ld\n",
		      On+Os, Ov, Of, Oq);
	(void)fprintf(ofile, " X | %4ld %4ld %4ld %4ld\n\n",
		      Xn+Xs, Xv, Xf, Xq);
	break;
      case 4:	/* print condensed format summary tables, with SVEBs */
	(void)fprintf(ofile, "         Algorithm\n");
	(void)fprintf(ofile, "      n+f+q    s    v  o+x\n");
	(void)fprintf(ofile, "     _____________________\n");
	(void)fprintf(ofile, "  N  | %4ld %4ld %4ld %4ld\n",
		      Nn+Nf+Nq, Ns, Nv, No+Nx);
	(void)fprintf(ofile, "  S  | %4ld %4ld %4ld %4ld\n",
		      Sn+Sf+Sq, Ss, Sv, So+Sx);
	(void)fprintf(ofile, "  V  | %4ld %4ld %4ld %4ld\n",
		      Vn+Vf+Vq, Vs, Vv, Vo+Vx);
	(void)fprintf(ofile, " F+Q | %4ld %4ld %4ld %4ld\n",
		      Fn+Ff+Fq+Qn+Qf+Qq, Fs+Qs, Fv+Qv, Fo+Fx+Qo+Qx);
	(void)fprintf(ofile, " O+X | %4ld %4ld %4ld\n\n",
		      On+Of+Oq+Xn+Xf+Xq, Os+Xs, Ov+Xv);
	break;
      case 5:	/* print line-format output, with SVEBs */
	(void)fprintf(ofile,
		      "%4s %5ld %3ld %3ld %3ld %3ld %3ld %3ld %3ld %3ld %3ld",
		      record,
		      Nn+Nf+Nq,
		      Sn+Sf+Sq,
		      Vn+Vf+Vq,
		      Fn+Ff+Fq + Qn+Qf+Qq,
		      On+Of+Oq + Xn+Xf+Xq,
		      Ns, Ss, Vs, Fs+Qs, Os+Xs);
	(void)fprintf(ofile,
		      " %3ld %3ld %4ld %3ld %3ld %3ld %3ld %3ld %3ld",
		      Nv, Sv, Vv, Fv+Qv, Ov+Xv,
		      No+Nx, So+Sx, Vo+Vx, Fo+Fx+Qo+Qx);
	(void)fprintf(sfile,
		      "%4s %4ld %4ld %4ld %4ld %4ld  ",
		      record, Nx, Sx, Vx, Fx, Qx);
	break;
      case 6:	/* print standard format summary tables, with SVEBs */
      default:
	(void)fprintf(ofile, "               Algorithm\n");
	(void)fprintf(ofile, "        n    s    v    f    q    o    x\n");
	(void)fprintf(ofile, "   ____________________________________\n");
	(void)fprintf(ofile, " N | %4ld %4ld %4ld %4ld %4ld %4ld %4ld\n",
		      Nn, Ns, Nv, Nf, Nq, No, Nx);
	(void)fprintf(ofile, " S | %4ld %4ld %4ld %4ld %4ld %4ld %4ld\n",
		      Sn, Ss, Sv, Sf, Sq, So, Sx);
	(void)fprintf(ofile, " V | %4ld %4ld %4ld %4ld %4ld %4ld %4ld\n",
		      Vn, Vs, Vv, Vf, Vq, Vo, Vx);
	(void)fprintf(ofile, " F | %4ld %4ld %4ld %4ld %4ld %4ld %4ld\n",
		      Fn, Fs, Fv, Ff, Fq, Fo, Fx);
	(void)fprintf(ofile, " Q | %4ld %4ld %4ld %4ld %4ld %4ld %4ld\n",
		      Qn, Qs, Qv, Qf, Qq, Qo, Qx);
	(void)fprintf(ofile, " O | %4ld %4ld %4ld %4ld %4ld\n",
		      On, Os, Ov, Of, Oq);
	(void)fprintf(ofile, " X | %4ld %4ld %4ld %4ld %4ld\n\n",
		      Xn, Xs, Xv, Xf, Xq);
	break;
    }
	 QTP = Nn+Ns+Nv+Nf+Nq + Sn+Ss+Sv+Sf+Sq + Vn+Vs+Vv+Vf+Vq + Fn+Fs+Fv+Ff+Fq +
	Qn+Qs+Qv+Qf+Qq;
	 QFN = No+Nx + So+Sx + Vo+Vx + Fo+Fx + Qo+Qx;
	 QFP = On+Os+Ov+Of+Oq + Xn+Xs+Xv+Xf+Xq;
	 VTP = Vv;
	 VFN = Vn + Vs + Vf + Vq + Vo + Vx;
	 VTN = Nn+Ns+Nf+Nq + Sn+Ss+Sf+Sq + Fn+Fs+Ff+Fq + Qn+Qs+Qf+Qq + On+Os+Of+Oq +
	Xn+Xs+Xf+Xq;
	 VFP = Nv + Sv + Ov + Xv;
    STP = Ss;
    SFN = Sn + Sv + Sf + Sq + So + Sx;
    SFP = Ns + Vs + Fs + Os + Xs;
	 pstat("           QRS sensitivity", "%6.2f", QTP, QTP + QFN);
    pstat(" QRS positive predictivity", "%6.2f", QTP, QTP + QFP);
    pstat("           VEB sensitivity", "%6.2f", VTP, VTP + VFN);
    pstat(" VEB positive predictivity", "%6.2f", VTP, VTP + VFP);
    if (fflag < 4)
	pstat("   VEB false positive rate", "%6.3f", VFP, VTN + VFP);
    else {
	pstat("          SVEB sensitivity", "%6.2f", STP, STP + SFN);
	pstat("SVEB positive predictivity", "%6.2f", STP, STP + SFP);
    }
    if (fflag == 4 || fflag == 6) {
	(void)fprintf(ofile, "     RMS RR interval error: ");
	if (nrre)
	    (void)fprintf(ofile, "%6.2f ms",
			  sqrt(ssrre/nrre)*1000./strtim("1"));
	else
	    (void)fprintf(ofile, "     -");
    }
    else if (fflag == 5) {
	if (nrre)
	    (void)fprintf(ofile, " %6.2f", sqrt(ssrre/nrre)*1000./strtim("1"));
	else
	    (void)fprintf(ofile, "     -");
    }
    (void)fprintf(ofile, "\n");
    sstat("\n  Beats missed in shutdown", "%6.2f", Nx+Vx+Fx+Qx, QTP + QFN);
    sstat("      N missed in shutdown", "%6.2f", Nx, Nn+Ns+Nv+Nf+Nq+No+Nx);
    if (fflag >= 4)
	sstat("      S missed in shutdown", "%6.2f", Sx, Sn+Ss+Sv+Sf+Sq+So+Sx);
    sstat("      V missed in shutdown", "%6.2f", Vx, Vn+Vs+Vv+Vf+Vq+Vo+Vx);
    sstat("      F missed in shutdown", "%6.2f", Fx, Fn+Fs+Fv+Ff+Fq+Fo+Fx);
    if (fflag == 1 || fflag == 3 || fflag == 4 || fflag == 6)
	(void)fprintf(sfile, "       Total shutdown time: ");
	 (void)fprintf(sfile, "%5ld seconds\n", shut_down);

	if(ofile != NULL)
		fclose(ofile) ;
	}
Exemple #5
0
/* 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) {
	help();
	exit(0);
    }

    if (start != 0L || end_time != 0L || match_dt != 0)
	(void)fprintf(stderr,"%s: (warning) nonstandard comparison selected\n",
		pname);

	 if (sampfreq(record) <= 0) {
	(void)fprintf(stderr,
		      "%s: (warning) %g Hz sampling frequency assumed\n",
		pname, WFDB_DEFFREQ);
	(void)setsampfreq(WFDB_DEFFREQ);
    }

    /* Set the match window and the times of the start and end of the test
       period.  Initialize the shutdown tally to 1/2 second so that it will be
       properly rounded to the nearest second at the end. */
    if (match_dt)
	match_dt = (int)strtim(argv[match_dt]);
    else
	match_dt = (int)strtim(".15");		/* 150 milliseconds */
    if (start)
	start = strtim(argv[(int)start]);
    else
	start = strtim("5:0");			/* 5 minutes */
	 if (end_time)
	end_time = strtim(argv[(int)end_time]);
    else if ((end_time = strtim("e")) == 0L)
	end_time = -1L;		/* record length unavailable -- go to end of
				   reference annotation file */
	 if (end_time > 0L && end_time < start) {
	(void)fprintf(stderr, "%s: improper interval specified\n", pname);
	exit(0);
    }
    shut_down = strtim(".5");	/* 1/2 second */

	 an[0].stat = an[1].stat = WFDB_READ;
    if (oflag) {
	an[2].name = "bxb";
	an[2].stat = WFDB_WRITE;
    }
    if (annopen(record, an, 2 + oflag) < 0) exit(0);
}
Exemple #6
0
MAINTYPE 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 */
		}
	 exit(0);	/*NOTREACHED*/
	}
Exemple #7
0
main(int argc, char **argv)
{ 
    char *p;
    char *record = NULL;	     /* input record name */
    float sps;			     /* sampling frequency, in Hz (SR) */
    float samplingInterval;          /* sampling interval, in milliseconds */
    int i, max, min, minutes = 0, onset, timer, vflag = 0;
    int dflag = 0;		     /* if non-zero, dump raw and filtered
					samples only;  do not run detector */
    int jflag = 0;		     /* if non-zero, annotate J-points */
    int Rflag = 0;		     /* if non-zero, resample at 120 or 150 Hz
				      */
    int EyeClosing;                  /* eye-closing period, related to SR */
    int ExpectPeriod;                /* if no QRS is detected over this period,
					the threshold is automatically reduced
					to a minimum value;  the threshold is
					restored upon a detection */
    double Ta, T0;		     /* high and low detection thresholds */
    WFDB_Anninfo a;
    WFDB_Annotation annot;
    WFDB_Gain gain;
    WFDB_Siginfo *s;
    WFDB_Time from = 0L, next_minute, spm, t, tj, tpq, to = 0L, tt, t1;
    static int gvmode = WFDB_GVPAD | WFDB_LOWRES;
    char *prog_name();
    void help();

    pname = prog_name(argv[0]);

    for (i = 1; i < argc; i++) {
	if (*argv[i] == '-') switch (*(argv[i]+1)) {
	  case 'd':	/* dump filter data */
	    dflag = 1;
	    break;
	  case 'f':	/* starting time */
	    if (++i >= argc) {
		(void)fprintf(stderr, "%s: time must follow -f\n", pname);
		exit(1);
	    }
	    from = i;
	    break;
	  case 'h':	/* help requested */
	    help();
	    exit(0);
	    break;
	  case 'H':	/* operate in WFDB_HIGHRES mode */
	    gvmode = WFDB_GVPAD | WFDB_HIGHRES;
	    break;
	  case 'j':	/* annotate J-points (ends of QRS complexes) */
	    jflag = 1;
	    break;
	  case 'm':	/* threshold */
	    if (++i >= argc || (Tm = atoi(argv[i])) <= 0) {
		(void)fprintf(stderr, "%s: threshold ( > 0) must follow -m\n",
			      pname);
		exit(1);
	    }
	    break;
	  case 'p':	/* specify power line (mains) frequency */
	    if (++i >= argc || (PWFreq = atoi(argv[i])) <= 0) {
		(void)fprintf(stderr,
			    "%s: power line frequency ( > 0) must follow -p\n",
			      pname);
		exit(1);
	    }
	    break;
	  case 'r':	/* record name */
	    if (++i >= argc) {
		(void)fprintf(stderr, "%s: input record name must follow -r\n",
			      pname);
		exit(1);
	    }
	    record = argv[i];
	    break;
	  case 'R':	/* resample */
	    Rflag = 1;
	    break;
	  case 's':	/* signal */
	    if (++i >= argc) {
		(void)fprintf(stderr,
			      "%s: signal number or name must follow -s\n",
			      pname);
		exit(1);
	    }
	    sig = i;	/* remember the argument until the record is open */
	    break;
	  case 't':	/* end time */
	    if (++i >= argc) {
		(void)fprintf(stderr, "%s: time must follow -t\n", pname);
		exit(1);
	    }
	    to = i;
	    break;
	  case 'v':	/* verbose mode */
	    vflag = 1;
	    break;
	  default:
	    (void)fprintf(stderr, "%s: unrecognized option %s\n", pname,
			  argv[i]);
	    exit(1);
	}
	else {
	    (void)fprintf(stderr, "%s: unrecognized argument %s\n", pname,
			  argv[i]);
	    exit(1);
	}
    }
    if (record == NULL) {
	help();
	exit(1);
    }

    if (gvmode == 0 && (p = getenv("WFDBGVMODE")))
	gvmode = atoi(p);
    setgvmode(gvmode|WFDB_GVPAD);

    if ((nsig = isigopen(record, NULL, 0)) < 1) exit(2);
    if ((s = (WFDB_Siginfo *)malloc(nsig * sizeof(WFDB_Siginfo))) == NULL) {
	(void)fprintf(stderr, "%s: insufficient memory\n", pname);
	exit(2);
    }
    if ((nsig = isigopen(record, s, nsig)) < 1) exit(2);
    sps = sampfreq((char *)NULL);
    if (sps < PWFreq) {
	(void)fprintf(stderr, "%s: sampling frequency (%g Hz) is too low%s",
		      pname, sps,
		      (gvmode & WFDB_HIGHRES) ? "\n" : ", try -H option\n");
	exit(3);
    }
    if (gvmode & WFDB_HIGHRES)
	setafreq(sampfreq((char *)NULL));
    a.name = "wqrs"; a.stat = WFDB_WRITE;
    if (annopen(record, &a, 1) < 0) exit(2);
    if (sig >= 0) sig = findsig(argv[sig]);
    if (sig < 0 || sig >= nsig) sig = 0;
    if ((gain = s[sig].gain) == 0.0) gain = WFDB_DEFGAIN;
    if (Rflag) {
    	if (PWFreq == 60.0) setifreq(sps = 120.);
    	else setifreq(sps = 150.);
    }
    if (from > 0L) {
	if ((from = strtim(argv[from])) < 0L)
	from = -from;
    }
    if (to > 0L) {
	if ((to = strtim(argv[to])) < 0L)
	    to = -to;
    }
    else
	to = strtim("e");

    annot.subtyp = annot.num = 0;
    annot.chan = sig;
    annot.aux = NULL;
    Tm = muvadu((unsigned)sig, Tm);
    samplingInterval = 1000.0/sps;
    lfsc = 1.25*gain*gain/sps;	/* length function scale constant */
    spm = 60 * sps;
    next_minute = from + spm;
    LPn = sps/PWFreq; 		/* The LP filter will have a notch at the
				    power line (mains) frequency */
    if (LPn > 8)  LPn = 8;	/* avoid filtering too agressively */
    LP2n = 2 * LPn;
    EyeClosing = sps * EYE_CLS; /* set eye-closing period */
    ExpectPeriod = sps * NDP;	/* maximum expected RR interval */
    LTwindow = sps * MaxQRSw;   /* length transform window size */

    (void)sample(sig, 0L);
    if (dflag) {
	for (t = from; t < to || (to == 0L && sample_valid()); t++)
	    printf("%6d\t%6d\n", sample(sig, t), ltsamp(t));
	exit(0);
    }

    if (vflag) {
	printf("\n------------------------------------------------------\n");
	printf("Record Name:             %s\n", record);
	printf("Total Signals:           %d  (", nsig);
	for (i = 0; i < nsig - 1; i++)
	    printf("%d, ", i);
	printf("%d)\n", nsig - 1);
	printf("Sampling Frequency:      %.1f Hz\n", sps);
	printf("Sampling Interval:       %.3f ms\n", samplingInterval);
	printf("Signal channel used for detection:    %d\n", sig);
	printf("Eye-closing period:      %d samples (%.0f ms)\n",
	       EyeClosing, EyeClosing*samplingInterval);
	printf("Minimum threshold:       %d A/D units (%d microvolts)\n",
	       Tm, adumuv(sig, Tm));
	printf("Power line frequency:    %d Hz\n", PWFreq);
	printf("\n------------------------------------------------------\n\n");
	printf("Processing:\n");
    }

    /* Average the first 8 seconds of the length-transformed samples
       to determine the initial thresholds Ta and T0. The number of samples
       in the average is limited to half of the ltsamp buffer if the sampling
       frequency exceeds about 2 KHz. */
    if ((t1 = strtim("8")) > BUFLN*0.9)
	t1 = BUFLN/2;
    t1 += from;
    for (T0 = 0, t = from; t < t1 && sample_valid(); t++)
	T0 += ltsamp(t);
    T0 /= t1 - from;
    Ta = 3 * T0;

    /* Main loop */
    for (t = from; t < to || (to == 0L && sample_valid()); t++) {
	static int learning = 1, T1;
	
	if (learning) {
	    if (t > t1) {
		learning = 0;
		T1 = T0;
		t = from;	/* start over */
	    }
	    else
		T1 = 2*T0;
	}
	
	/* Compare a length-transformed sample against T1. */
	if (ltsamp(t) > T1) {	/* found a possible QRS near t */
	    timer = 0; /* used for counting the time after previous QRS */
	    max = min = ltsamp(t);
	    for (tt = t+1; tt < t + EyeClosing/2; tt++)
		if (ltsamp(tt) > max) max = ltsamp(tt);
	    for (tt = t-1; tt > t - EyeClosing/2; tt--)
		if (ltsamp(tt) < min) min = ltsamp(tt);
	    if (max > min+10) { /* There is a QRS near tt */
		/* Find the QRS onset (PQ junction) */
		onset = max/100 + 2;
		tpq = t - 5;
		for (tt = t; tt > t - EyeClosing/2; tt--) {
		    if (ltsamp(tt)   - ltsamp(tt-1) < onset &&
			ltsamp(tt-1) - ltsamp(tt-2) < onset &&
			ltsamp(tt-2) - ltsamp(tt-3) < onset &&
			ltsamp(tt-3) - ltsamp(tt-4) < onset) {
			tpq = tt - LP2n;	/* account for phase shift */
			break;
		    }
		}

		if (!learning) {
		    /* Check that we haven't reached the end of the record. */
		    (void)sample(sig, tpq);
		    if (sample_valid() == 0) break;
		    /* Record an annotation at the QRS onset */
		    annot.time = tpq;
		    annot.anntyp = NORMAL;
		    if (putann(0, &annot) < 0) { /* write the annotation */
			wfdbquit();	/* close files if an error occurred */
			exit(1);
		    }
		    if (jflag) {
			/* Find the end of the QRS */
			for (tt = t, tj = t + 5; tt < t + EyeClosing/2; tt++) {
			    if (ltsamp(tt) > max - (max/10)) {
				tj = tt;
				break;
			    }
			}
			(void)sample(sig, tj);
			if (sample_valid() == 0) break;
			/* Record an annotation at the J-point */
			annot.time = tj;
			annot.anntyp = JPT;
			if (putann(0, &annot) < 0) {
			    wfdbquit();
			    exit(1);
			}
		    }
		}

		/* Adjust thresholds */
		Ta += (max - Ta)/10;
		T1 = Ta / 3;


		/* Lock out further detections during the eye-closing period */
		t += EyeClosing;
	    }
	}
	else if (!learning) {
	    /* Once past the learning period, decrease threshold if no QRS
	       was detected recently. */
	    if (++timer > ExpectPeriod && Ta > Tm) {
		Ta--;
		T1 = Ta / 3;
	    }      
	}

	/* Keep track of progress by printing a dot for each minute analyzed */
	if (t >= next_minute) {
	    next_minute += spm;
	    (void)fprintf(stderr, ".");
	    (void)fflush(stderr);
	    if (++minutes >= 60) {
		(void)fprintf(stderr, " %s\n", timstr(t));
		minutes = 0;
	    }
	}
    }
    if (minutes) (void)fprintf(stderr, " %s\n", timstr(t));

    (void)free(lbuf);
    (void)free(ebuf);
    wfdbquit();		        /* close WFDB files */
    fprintf(stderr, "\n");
    if (vflag) {
	printf("\n\nDone! \n\nResulting annotation file:  %s.wqrs\n\n\n",
	       record);
    }
    exit(0);
}
Exemple #8
0
INTEGER strtim_(char *string)
{
    return (strtim(fcstring(string)));
}
Exemple #9
0
int main(int argc, char **argv)
{ 
    char *record = NULL;	     /* input record name */
    float sps;			     /* sampling frequency, in Hz (SR) */
    float samplingInterval;          /* sampling interval, in milliseconds */
    int i, max, min, minutes = 0, onset, timer, vflag = 0;
    int dflag = 0;		     /* if non-zero, dump raw and filtered
					samples only;  do not run detector */
    int Rflag = 0;		     /* if non-zero, resample at 125 Hz  */
    int EyeClosing;                  /* eye-closing period, related to SR */
    int ExpectPeriod;                /* if no ABP pulse is detected over this period,
					the threshold is automatically reduced
					to a minimum value;  the threshold is
					restored upon a detection */
    int Ta, T0;			     /* high and low detection thresholds */
    WFDB_Anninfo a;
    WFDB_Annotation annot;
    WFDB_Siginfo *s;
    WFDB_Time from = 0L, next_minute, spm, t, tpq, to = 0L, tt, t1;
    char *p, *prog_name();
    static int gvmode = 0;
    void help();

    pname = prog_name(argv[0]);

    for (i = 1; i < argc; i++) {
	if (*argv[i] == '-') switch (*(argv[i]+1)) {
	  case 'd':	/* dump filter data */
	    dflag = 1;
	    break;
	  case 'f':	/* starting time */
	    if (++i >= argc) {
		(void)fprintf(stderr, "%s: time must follow -f\n", pname);
		exit(1);
	    }
	    from = i;
	    break;
	  case 'h':	/* help requested */
	    help();
	    exit(0);
	    break;
	  case 'H':	/* operate in WFDB_HIGHRES mode */
	    gvmode = WFDB_HIGHRES;
	    break;
	  case 'm':	/* threshold */
	    if (++i >= argc || (Tm = atoi(argv[i])) <= 0) {
		(void)fprintf(stderr, "%s: threshold ( > 0) must follow -m\n",
			      pname);
		exit(1);
	    }
	    break;
	  case 'r':	/* record name */
	    if (++i >= argc) {
		(void)fprintf(stderr, "%s: input record name must follow -r\n",
			      pname);
		exit(1);
	    }
	    record = argv[i];
	    break;
	  case 'R':	/* resample */
	    Rflag = 1;
	    break;
	  case 's':	/* signal */
	    if (++i >= argc) {
		(void)fprintf(stderr,
			      "%s: signal number or name must follow -s\n",
			      pname);
		exit(1);
	    }
	    sig = i;	/* remember argument until record is open */
	    break;
	  case 't':	/* end time */
	    if (++i >= argc) {
		(void)fprintf(stderr, "%s: time must follow -t\n", pname);
		exit(1);
	    }
	    to = i;
	    break;
	  case 'v':	/* verbose mode */
	    vflag = 1;
	    break;
	  default:
	    (void)fprintf(stderr, "%s: unrecognized option %s\n", pname,
			  argv[i]);
	    exit(1);
	}
	else {
	    (void)fprintf(stderr, "%s: unrecognized argument %s\n", pname,
			  argv[i]);
	    exit(1);
	}
    }
    if (record == NULL) {
	help();
	exit(1);
    }

    if (gvmode == 0 && (p = getenv("WFDBGVMODE")))
	gvmode = atoi(p);
    setgvmode(gvmode|WFDB_GVPAD);

    if ((nsig = isigopen(record, NULL, 0)) < 1) exit(2);
    if ((s = (WFDB_Siginfo *)malloc(nsig * sizeof(WFDB_Siginfo))) == NULL) {
	(void)fprintf(stderr, "%s: insufficient memory\n", pname);
	exit(2);
    }
    a.name = "wabp"; a.stat = WFDB_WRITE;
    if ((nsig = wfdbinit(record, &a, 1, s, nsig)) < 1) exit(2);
    if (sig >= 0) sig = findsig(argv[sig]);
    if (sig < 0 || sig >= nsig) {
	/* Identify the lowest-numbered ABP, ART, or BP signal */
	for (i = 0; i < nsig; i++)
	    if (strcmp(trim_whitespace(s[i].desc), "ABP") == 0 ||
		strcmp(s[i].desc, "ART") == 0 ||
		strcmp(s[i].desc, "BP") == 0)
		break;
	if (i == nsig) {
	    fprintf(stderr, "%s: no ABP signal specified; use -s option\n\n",
		    pname);
	    help();
	    exit(3);
	}
	sig = i;
    }
    
    if (vflag)
	fprintf(stderr, "%s: analyzing signal %d (%s)\n",
		pname, sig, s[sig].desc);
    sps = sampfreq((char *)NULL);
    if (Rflag)
    	setifreq(sps = 125.); 
    if (from > 0L) {
	if ((from = strtim(argv[from])) < 0L)
	    from = -from;
    }
    if (to > 0L) {
	if ((to = strtim(argv[to])) < 0L)
	    to = -to;
    }

    annot.subtyp = annot.num = 0;
    annot.chan = sig;
    annot.aux = NULL;
    Tm = physadu((unsigned)sig, Tm);
    samplingInterval = 1000.0/sps;
    spm = 60 * sps;
    next_minute = from + spm;
    EyeClosing = sps * EYE_CLS;   /* set eye-closing period */
    ExpectPeriod = sps * NDP;	  /* maximum expected RR interval */
    SLPwindow = sps * SLPW;       /* slope window size */

    if (vflag) {
	printf("\n------------------------------------------------------\n");
	printf("Record Name:             %s\n", record);
	printf("Total Signals:           %d  (", nsig);
	for (i = 0; i < nsig - 1; i++)
	    printf("%d, ", i);
	printf("%d)\n", nsig - 1);
	printf("Sampling Frequency:      %.1f Hz\n", sps);
	printf("Sampling Interval:       %.3f ms\n", samplingInterval);
	printf("Signal channel used for detection:    %d\n", sig);
	printf("Eye-closing period:      %d samples (%.0f ms)\n",
	       EyeClosing, EyeClosing*samplingInterval);
	printf("Minimum threshold:       %d\n", Tm);
	printf("\n------------------------------------------------------\n\n");
	printf("Processing:\n");
    }

    (void)sample(sig, 0L);
    if (dflag) {
	for (t = from; (to == 0L || t < to) && sample_valid(); t++)
	    printf("%6d\t%6d\n", sample(sig, t), slpsamp(t));
	exit(0);
    }

    /* Average the first 8 seconds of the slope  samples
       to determine the initial thresholds Ta and T0 */
    t1 = from + strtim("8");
    for (T0 = 0, t = from; t < t1 && sample_valid(); t++)
	T0 += slpsamp(t);
    T0 /= t1 - from;
    Ta = 3 * T0;

    /* Main loop */
    for (t = from; (to == 0L || t < to) && sample_valid(); t++) {
	static int learning = 1, T1;
	
	if (learning) {
	    if (t > from + LPERIOD) {
		learning = 0;
		T1 = T0;
		t = from;	/* start over */
	    }
	    else T1 = 2*T0;
	}
	
	if (slpsamp(t) > T1) {   /* found a possible ABP pulse near t */ 
	    timer = 0; 
            /* used for counting the time after previous ABP pulse */
	    max = min = slpsamp(t);
	    for (tt = t+1; tt < t + EyeClosing/2; tt++)
		if (slpsamp(tt) > max) max = slpsamp(tt);
	    for (tt = t-1; tt > t - EyeClosing/2; tt--)
		if (slpsamp(tt) < min) min = slpsamp(tt);
	    if (max > min+10) { 
		onset = max/100 + 2;
		tpq = t - 5;
		for (tt = t; tt > t - EyeClosing/2; tt--) {
		    if (slpsamp(tt) - slpsamp(tt-1) < onset) {
		      tpq = tt;  
			break;
		    }
		}

		if (!learning) {
		    /* Check that we haven't reached the end of the record. */
		    (void)sample(sig, tpq);
		    if (sample_valid() == 0) break;
		    /* Record an annotation at the ABP pulse onset */
		    annot.time = tpq;
		    annot.anntyp = NORMAL;
		    if (putann(0, &annot) < 0) { /* write the annotation */
			wfdbquit();	/* close files if an error occurred */
			exit(1);
		    }
		}

		/* Adjust thresholds */
		Ta += (max - Ta)/10;
		T1 = Ta / 3;

		/* Lock out further detections during the eye-closing period */
		t += EyeClosing;
	    }
	}
	else if (!learning) {
	    /* Once past the learning period, decrease threshold if no pulse
	       was detected recently. */
	    if (++timer > ExpectPeriod && Ta > Tm) {
		Ta--;
		T1 = Ta / 3;
	    }
	}

	/* Keep track of progress by printing a dot for each minute analyzed */
	if (t >= next_minute) {
	    next_minute += spm;
	    (void)fprintf(stderr, ".");
	    (void)fflush(stderr);
	    if (++minutes >= 60) {
		(void)fprintf(stderr, "\n");
		minutes = 0;
	    }
	}
    }

    (void)free(lbuf);
    (void)free(ebuf);
    wfdbquit();		        /* close WFDB files */
    fprintf(stderr, "\n");
    if (vflag) {
	printf("\n\nDone! \n\nResulting annotation file:  %s.wabp\n\n\n",
	       record);
    }
    exit(0);
}
Exemple #10
0
void info(void)
{
    char *info, *p;
    int i;
    WFDB_Time t;

    prep_signals();
    if (nsig < 0) {
	lwfail("The '.hea' file could not be read");
	return;
    }
    printf("{ \"info\":\n");
    printf("  { \"db\": %s,\n", p = strjson(db)); SFREE(p);
    printf("    \"record\": %s,\n", p = strjson(record)); SFREE(p);
    printf("    \"tfreq\": %g,\n", tfreq);
    p = timstr(0);
    if (*p == '[') {
        printf("    \"start\": \"%s\",\n", mstimstr(0L));
	printf("    \"end\": \"%s\",\n", mstimstr(-strtim("e")));
    }
    else {
        printf("    \"start\": null,\n");
	printf("    \"end\": null,\n");
    }
    t = strtim("e");
    if (t > (WFDB_Time)0) {
	p = mstimstr(t);
	while (*p == ' ') p++;
	printf("    \"duration\": \"%s\",\n", p);
    }
    else
	printf("    \"duration\": null,\n");

    if (nsig > 0) {
	printf("    \"signal\": [\n");
	for (i = 0; i < nsig; i++) {
	    printf("      { \"name\": %s,\n", p = strjson(sname[i])); SFREE(p);
	    printf("        \"tps\": %g,\n", tfreq/(ffreq*s[i].spf));
	    if (s[i].units) {
		printf("        \"units\": %s,\n", p = strjson(s[i].units));
		SFREE(p);
	    }
	    else
		printf("        \"units\": null,\n");
	    printf("        \"gain\": %g,\n",
		   s[i].gain ? s[i].gain : WFDB_DEFGAIN);
	    printf("        \"adcres\": %d,\n", s[i].adcres);
	    printf("        \"adczero\": %d,\n", s[i].adczero);
	    printf("        \"baseline\": %d\n", s[i].baseline);
	    printf("      }%s", i < nsig-1 ? ",\n" : "\n    ],\n");
	}
    }
    else
	printf("    \"signal\": null,\n");

    if (info = getinfo(recpath)) {
	printf("    \"note\": [\n      %s", p = strjson(info));
	while (info = getinfo((char *)NULL)) {
	    printf(",\n      %s", p = strjson(info));
	    SFREE(p);
	}
	printf("\n    ]\n");
    }
    else
	printf("    \"note\": null\n");

    printf("  },\n");
    lwpass();
}