Beispiel #1
0
int main (int argc, char **argv)
{
	int 	t, T; 
	HMM  	hmm;
	int	*O;	/* observation sequence O[1..T] */
	double **alpha;
	double *scale;
	double 	proba, logproba; 
	FILE	*fp;

	if (argc != 3) {
		printf("Usage error \n");
		printf("Usage: testfor <model.hmm> <obs.seq> \n");
		exit (1);
	}
	
	fp = fopen(argv[1], "r");
	if (fp == NULL) {
		fprintf(stderr, "Error: File %s not found\n", argv[1]);
		exit (1);
	}
	ReadHMM(fp, &hmm);
	fclose(fp);

	fp = fopen(argv[2], "r");
	if (fp == NULL) {
		fprintf(stderr, "Error: File %s not found\n", argv[2]);
		exit (1);
	}
	ReadSequence(fp, &T, &O);
	fclose(fp);


	alpha = dmatrix(1, T, 1, hmm.N);
	scale = dvector(1, T);

	printf("------------------------------------\n");
	printf("Forward without scaling \n");
	Forward(&hmm, T, O, alpha, &proba); 
	fprintf(stdout, "log prob(O| model) = %E\n", log(proba));

	printf("------------------------------------\n");
	printf("Forward with scaling \n");

	ForwardWithScale(&hmm, T, O, alpha, scale, &logproba); 

	fprintf(stdout, "log prob(O| model) = %E\n", logproba);
	printf("------------------------------------\n");
	printf("The two log probabilites should identical \n");
	printf("(within numerical precision). When observation\n");
	printf("sequence is very large, use scaling. \n");
	
	free_ivector(O, 1, T);
	free_dmatrix(alpha, 1, T, 1, hmm.N);
	free_dvector(scale, 1, T);
	FreeHMM(&hmm);
}
Beispiel #2
0
int main (int argc, char **argv)
{
	HMM  	hmm; 	/* the HMM */
	int  	T; 	/* length of observation sequence */
	int	*O;	/* the observation sequence O[1..T]*/
	int	*q; 	/* the state sequence q[1..T] */
	FILE 	*fp,*fp1;
	char    *inputfile,*outputfile;
	int	iflg = 0,oflg = 0,rflg=0, tflg=0,nflg =0, errflg = 0;
	int	randomize=0,seed=1;	/* random number seed */
	int	n,c,i;
	extern char *optarg;
	extern int optind, opterr, optopt;
	while ((c= getopt(argc, argv, "h:i:o:r:t:n:")) != EOF)
	  switch (c) {
	  case 'h': 
	    Usage(argv[0]);
	    exit(1);
	    break;
	  case 'i':
	    iflg++;
	    inputfile = argv[optind-1];
	    break;   
	  case 'o':
	    oflg++;  
	    outputfile  = argv[optind-1];
	    break;   
	  case 'r':
	    rflg++;  
	    sscanf(optarg, "%d", &randomize);
	    break;   
	  case 't':  
	    tflg++;  
	    sscanf(optarg, "%d", &T);
	    break;   
	  case 'n':  
	    nflg++;  
	    sscanf(optarg, "%d", &n);
	    break;   
	  case '?':
	    errflg++;
	  }
	if (errflg || !tflg || !iflg || !oflg||!nflg) {
	  Usage(argv[0]);
	  exit(1);
	}
	/* read HMM file */
	fp = fopen(inputfile,"r");		
	if (fp == NULL) {
	  fprintf(stderr, "Error: File %s not found \n", argv[optind]);
	  exit (1);
	}
	ReadHMM(fp, &hmm);
	fclose(fp);
	fp1 = fopen(outputfile,"w");
	int maxdeviation = (int) T/10;
	int randnum = 0;
	for(i=0;i<n;i++){
	  int T1 = T;
	  seed = (unsigned)time( NULL )+i;
	  if(randomize) {
	    srand(seed);
	    unsigned long r = rand(); r <<= 15; r += rand();
	    int randnum = (r%(2*maxdeviation))-maxdeviation;	    
	    T1 = T + randnum;
	  }
	  O = ivector(1,T1); /* alloc space for observation sequence O */
	  q = ivector(1,T1); /* alloc space for state sequence q */
	  GenSequenceArray(&hmm, seed, T1, O, q);
	  PrintSequence1(fp1, T1, O);
	  free_ivector(O, 1, T1);
	  free_ivector(q, 1, T1);
	}
	fclose(fp1);
	FreeHMM(&hmm);
	return 1;
}
Beispiel #3
0
int main (int argc, char **argv)
{
  HMM  	hmm; 	/* the HMM */
  int  	T = 100; 	/* length of observation sequence */
  int	*O;	/* the observation sequence O[1..T]*/
  int	*q; 	/* the state sequence q[1..T] */
  FILE 	*fp;	/* HMM parameters in this file */
  int	sflg=0, tflg=0, errflg = 0;
  int	seed;	/* random number seed */
  int	c;	
  extern char *optarg;
  extern int optind, opterr, optopt;

  while ((c= getopt(argc, argv, "S:T:")) != EOF)
    switch (c) {
    case 'S':
      /* set random number generator seed */
      if (sflg)
	errflg++;
      else {	
	sflg++;
	sscanf(optarg, "%d", &seed);
      }
      break;
    case 'T':
      /* set sequence length */
      if (tflg)
	errflg++;
      else {	
	tflg++;
	sscanf(optarg, "%d", &T);
      }
      break;
    case '?':
      errflg++;
    }

  if ((argc - optind) != 1) errflg++; /* number or arguments not
					 okay */

  if (errflg || !tflg ) {
    Usage(argv[0]);
    exit(1);
  }


  /* read HMM file */
  fp = fopen(argv[optind],"r");		
  if (fp == NULL) {
    fprintf(stderr, "Error: File %s not found \n", argv[optind]);
    exit (1);
  }
  ReadHMM(fp, &hmm);
  fclose(fp);


  /* length of observation sequence, T */

  O = ivector(1,T); /* alloc space for observation sequence O */
  q = ivector(1,T); /* alloc space for state sequence q */

  if (!sflg) seed = hmmgetseed();

  fprintf(stderr, "RandomSeed: %d\n", seed);
  GenSequenceArray(&hmm, seed, T, O, q);

  PrintSequence(stdout, T, O);
  free_ivector(O, 1, T);
  free_ivector(q, 1, T);
  FreeHMM(&hmm);
}