Ejemplo n.º 1
0
/* HTS_Model_load_pdf: load pdfs */
static void HTS_Model_load_pdf(HTS_Model * model, FILE * fp, int ntree,
                               HTS_Boolean msd_flag)
{
   int i, j, k, l, m;
   float temp;
   int ssize;

   /* check */
   if (fp == NULL)
      HTS_error(1, "HTS_Model_load_pdf: File for pdfs is not specified.\n");

   /* load pdf */
   model->ntree = ntree;
   /* read MSD flag */
   HTS_fread_big_endian(&i, sizeof(int), 1, fp);
   if ((i != 0 || msd_flag != FALSE) && (i != 1 || msd_flag != TRUE))
      HTS_error(1, "HTS_Model_load_pdf: Failed to load header of pdfs.\n");
   /* read stream size */
   HTS_fread_big_endian(&ssize, sizeof(int), 1, fp);
   if (ssize < 1)
      HTS_error(1, "HTS_Model_load_pdf: Failed to load header of pdfs.\n");
   /* read vector size */
   HTS_fread_big_endian(&model->vector_length, sizeof(int), 1, fp);
   if (model->vector_length < 0)
      HTS_error(1,
                "HTS_Model_load_pdf: # of HMM states %d should be positive.\n",
                model->vector_length);
   model->npdf = (int *) HTS_calloc(ntree, sizeof(int));
   model->npdf -= 2;
   /* read the number of pdfs */
   HTS_fread_big_endian(&model->npdf[2], sizeof(int), ntree, fp);
   for (i = 2; i <= ntree + 1; i++)
      if (model->npdf[i] < 0)
         HTS_error(1,
                   "HTS_Model_load_pdf: # of pdfs at %d-th state should be positive.\n",
                   i);
   model->pdf = (double ***) HTS_calloc(ntree, sizeof(double **));
   model->pdf -= 2;
   /* read means and variances */
   if (msd_flag) {              /* for MSD */
      for (j = 2; j <= ntree + 1; j++) {
         model->pdf[j] = (double **)
             HTS_calloc(model->npdf[j], sizeof(double *));
         model->pdf[j]--;
         for (k = 1; k <= model->npdf[j]; k++) {
            model->pdf[j][k] = (double *)
                HTS_calloc(2 * model->vector_length + 1, sizeof(double));
            for (l = 0; l < ssize; l++) {
               for (m = 0; m < model->vector_length / ssize; m++) {
                  HTS_fread_big_endian(&temp, sizeof(float), 1, fp);
                  model->pdf[j][k][l * model->vector_length / ssize + m] =
                      (double) temp;
                  HTS_fread_big_endian(&temp, sizeof(float), 1, fp);
                  model->pdf[j][k][l * model->vector_length / ssize + m +
                                   model->vector_length] = (double) temp;
               }
               HTS_fread_big_endian(&temp, sizeof(float), 1, fp);
               if (l == 0) {
                  if (temp < 0.0 || temp > 1.0)
                     HTS_error(1,
                               "HTS_Model_load_pdf: MSD weight should be within 0.0 to 1.0.\n");
                  model->pdf[j][k][2 * model->vector_length] = (double) temp;
               }
               HTS_fread_big_endian(&temp, sizeof(float), 1, fp);
            }
         }
      }
   } else {                     /* for non MSD */
      for (j = 2; j <= ntree + 1; j++) {
         model->pdf[j] =
             (double **) HTS_calloc(model->npdf[j], sizeof(double *));
         model->pdf[j]--;
         for (k = 1; k <= model->npdf[j]; k++) {
            model->pdf[j][k] =
                (double *) HTS_calloc(2 * model->vector_length, sizeof(double));
            for (l = 0; l < model->vector_length; l++) {
               HTS_fread_big_endian(&temp, sizeof(float), 1, fp);
               model->pdf[j][k][l] = (double) temp;
               HTS_fread_big_endian(&temp, sizeof(float), 1, fp);
               model->pdf[j][k][l + model->vector_length] = (double) temp;
            }
         }
      }
   }
}
Ejemplo n.º 2
0
/* HTS_Model_load_pdf: load pdfs */
static HTS_Boolean HTS_Model_load_pdf(HTS_Model * model, HTS_File * fp, int ntree, HTS_Boolean * msd_flag)
{
   int i, j, k, l, m;
   float temp;
   int ssize;
   HTS_Boolean result = TRUE;

   /* check */
   if (model == NULL || fp == NULL || ntree <= 0) {
      HTS_error(1, "HTS_Model_load_pdf: File for pdfs is not specified.\n");
      return FALSE;
   }

   /* load pdf */
   model->ntree = ntree;
   /* read MSD flag */
   HTS_fread_big_endian(&i, sizeof(int), 1, fp);
   *msd_flag = i;
   if ((*msd_flag != FALSE) && (*msd_flag != TRUE)) {
      HTS_error(1, "HTS_Model_load_pdf: Failed to load header of pdfs.\n");
      HTS_Model_initialize(model);
      return FALSE;
   }
   /* read stream size */
   HTS_fread_big_endian(&ssize, sizeof(int), 1, fp);
   if (ssize < 1) {
      HTS_error(1, "HTS_Model_load_pdf: Failed to load header of pdfs.\n");
      HTS_Model_initialize(model);
      return FALSE;
   }
   /* read vector size */
   HTS_fread_big_endian(&model->vector_length, sizeof(int), 1, fp);
   if (model->vector_length <= 0) {
      HTS_error(1, "HTS_Model_load_pdf: # of HMM states %d should be positive.\n", model->vector_length);
      HTS_Model_initialize(model);
      return FALSE;
   }
   model->npdf = (int *) HTS_calloc(ntree, sizeof(int));
   model->npdf -= 2;
   /* read the number of pdfs */
   if (HTS_fread_big_endian(&model->npdf[2], sizeof(int), ntree, fp) != ntree)
      result = FALSE;
   for (i = 2; i <= ntree + 1; i++)
      if (model->npdf[i] <= 0) {
         HTS_error(1, "HTS_Model_load_pdf: # of pdfs at %d-th state should be positive.\n", i);
         result = FALSE;
         break;
      }
   if (result == FALSE) {
      model->npdf += 2;
      free(model->npdf);
      HTS_Model_initialize(model);
      return FALSE;
   }
   model->pdf = (double ***) HTS_calloc(ntree, sizeof(double **));
   model->pdf -= 2;
   /* read means and variances */
   if (*msd_flag) {              /* for MSD */
      for (j = 2; j <= ntree + 1; j++) {
         model->pdf[j] = (double **) HTS_calloc(model->npdf[j], sizeof(double *));
         model->pdf[j]--;
         for (k = 1; k <= model->npdf[j]; k++) {
            model->pdf[j][k] = (double *) HTS_calloc(2 * model->vector_length + 1, sizeof(double));
            for (l = 0; l < ssize; l++) {
               for (m = 0; m < model->vector_length / ssize; m++) {
                  if (HTS_fread_big_endian(&temp, sizeof(float), 1, fp) != 1)
                     result = FALSE;
                  model->pdf[j][k][l * model->vector_length / ssize + m] = (double) temp;
                  if (HTS_fread_big_endian(&temp, sizeof(float), 1, fp) != 1)
                     result = FALSE;
                  model->pdf[j][k][l * model->vector_length / ssize + m + model->vector_length] = (double) temp;
               }
               if (HTS_fread_big_endian(&temp, sizeof(float), 1, fp) != 1)
                  result = FALSE;
               if (l == 0) {
                  if (temp < 0.0 || temp > 1.0) {
                     HTS_error(1, "HTS_Model_load_pdf: MSD weight should be within 0.0 to 1.0.\n");
                     result = FALSE;
                  }
                  model->pdf[j][k][2 * model->vector_length] = (double) temp;
               }
               if (HTS_fread_big_endian(&temp, sizeof(float), 1, fp) != 1)
                  result = FALSE;
            }
         }
      }
   } else {                     /* for non MSD */
      for (j = 2; j <= ntree + 1; j++) {
         model->pdf[j] = (double **) HTS_calloc(model->npdf[j], sizeof(double *));
         model->pdf[j]--;
         for (k = 1; k <= model->npdf[j]; k++) {
            model->pdf[j][k] = (double *) HTS_calloc(2 * model->vector_length, sizeof(double));
            for (l = 0; l < model->vector_length; l++) {
               if (HTS_fread_big_endian(&temp, sizeof(float), 1, fp) != 1)
                  result = FALSE;
               model->pdf[j][k][l] = (double) temp;
               if (HTS_fread_big_endian(&temp, sizeof(float), 1, fp) != 1)
                  result = FALSE;
               model->pdf[j][k][l + model->vector_length] = (double) temp;
            }
         }
      }
   }
   if (result == FALSE) {
      HTS_Model_clear(model);
      return FALSE;
   }
   return TRUE;
}