/* file: makepv.c	G. Moody	12 January 1992
			Last revised:	21 July 1999
-------------------------------------------------------------------------------
makepv: Generate pattern vectors on the standard output
Copyright (C) 1999 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 (george@mit.edu) or postal mail
(MIT Room E25-505A, Cambridge, MA 02139 USA).  For updates to this software,
please visit PhysioNet (http://physionet.org/).
_______________________________________________________________________________

This program is based on example 9 (a signal averager) from the WFDB Database
Programmer's Guide.  Please note that it is an *example* only, intended to be
used for study.  You should determine a suitable method for defining a pattern
vector, and modify this program appropriately.
*/

#include <stdio.h>
#include <wfdb/wfdb.h>
#include <wfdb/ecgmap.h>
#include "defs.h"

#define DT0	(-PVDIM/2)	/* interval from fiducial mark to first sample
				   needed in each pattern vector */
#define DT1	(PVDIM+DT0-1)	/* interval from fiducial mark to last sample
				   needed in each pattern vector */

struct patternvector pattern_vector;

main(int argc, char **argv)
{
  int btype, i, j, nsig;
  WFDB_Sample v[WFDB_MAXSIG], vb[WFDB_MAXSIG];
  WFDB_Time stoptime = 0L;
  WFDB_Anninfo a;
  WFDB_Annotation annot;
  WFDB_Siginfo s[WFDB_MAXSIG];
  void write_pattern_vector(), *calloc();
  
  if (argc < 3) {
    fprintf(stderr,
	    "usage: %s annotator record [beat-type from to]\n",
	    argv[0]);
    exit(1);
  }
  a.name = argv[1]; a.stat = WFDB_READ;
  if ((nsig = wfdbinit(argv[2], &a, 1, s, WFDB_MAXSIG)) < 1) exit(2);
  
  btype = (argc > 3) ? strann(argv[3]) : 0;
  if (argc > 4) iannsettime(strtim(argv[4]));
  if (argc > 5) {
    if ((stoptime = strtim(argv[5])) < 0L)
      stoptime = -stoptime;
    if (s[0].nsamp > 0L && stoptime > s[0].nsamp)
      stoptime = s[0].nsamp;
  }
  else stoptime = s[0].nsamp;
  if (stoptime > 0L) stoptime -= DT1;
  while (getann(0, &annot) == 0 && annot.time < -(DT0+1))
    ;
  do {
    if (!isqrs(annot.anntyp)) continue; 
    if (btype != 0 && annot.anntyp != btype) continue;
    
    /* Go to the sample just before the first one in the pattern
       vector, and use it for the baseline estimate.  You might
       want to improve on this strategy. */
    isigsettime(annot.time + DT0 - 1);
    getvec(vb);
    
    /* Fill the pattern vector, in this case with consecutive baseline-
       corrected samples from signal 0.  Again, you might want to try a
       different idea. */
    for (j = 0; j < PVDIM && getvec(v) > 0; j++)
      pattern_vector.v[j] = v[0] - vb[0];
    
    /* Copy tag fields from the annotation to the pattern vector. */
    pattern_vector.time = annot.time;
    pattern_vector.anntyp = annot.anntyp;
    
    write_pattern_vector();
  } while (getann(0, &annot) == 0 && (stoptime==0L || annot.time < stoptime));
  exit(0);
}

void write_pattern_vector()
{
  int i;
  
  printf("%8ld %s", pattern_vector.time, annstr(pattern_vector.anntyp));
  for (i = 0; i < PVDIM; i++)
    printf(" %5d", pattern_vector.v[i]);
  printf("\n");
}
