Beispiel #1
0
void InitDWin(PStream * pst)
{
   double *dcalloc(int, int);
   int i, j;
   int fsize, leng;
   double x, a0, a1, a2;
   FILE *fp;

   /* memory allocation */
   if ((pst->dw.width = (int **) calloc(pst->dw.num, sizeof(int *))) == NULL) {
      fprintf(stderr, "%s : Cannot allocate memory!\n", cmnd);
      exit(1);
   }
   for (i = 0; i < pst->dw.num; i++)
      if ((pst->dw.width[i] = (int *) calloc(2, sizeof(int))) == NULL) {
         fprintf(stderr, "%s : Cannot allocate memory!\n", cmnd);
         exit(1);
      }
   if ((pst->dw.coef =
        (double **) calloc(pst->dw.num, sizeof(double *))) == NULL) {
      fprintf(stderr, "%s : Cannot allocate memory!\n", cmnd);
      exit(1);
   }

   /* window for static parameter */
   pst->dw.width[0][WLEFT] = pst->dw.width[0][WRIGHT] = 0;
   pst->dw.coef[0] = dcalloc(1, 0);
   pst->dw.coef[0][0] = 1;

   /* set delta coefficients */
   if (pst->dw.calccoef == 0) {
      for (i = 1; i < pst->dw.num; i++) {
         if (pst->dw.fn[i][0] == ' ') {
            fsize = str2darray(pst->dw.fn[i], &(pst->dw.coef[i]));
         } else {
            /* read from file */
            fp = getfp(pst->dw.fn[i], "rb");

            /* check the number of coefficients */
            fseek(fp, 0L, 2);
            fsize = ftell(fp) / sizeof(real);
            fseek(fp, 0L, 0);

            /* read coefficients */
            pst->dw.coef[i] = dcalloc(fsize, 0);
            freadf(pst->dw.coef[i], sizeof(**(pst->dw.coef)), fsize, fp);
         }

         /* set pointer */
         leng = fsize / 2;
         pst->dw.coef[i] += leng;
         pst->dw.width[i][WLEFT] = -leng;
         pst->dw.width[i][WRIGHT] = leng;
         if (fsize % 2 == 0)
            pst->dw.width[i][WRIGHT]--;
      }
   } else if (pst->dw.calccoef == 1) {
      for (i = 1; i < pst->dw.num; i++) {
         leng = atoi(pst->dw.fn[i]);
         if (leng < 1) {
            fprintf(stderr,
                    "%s : Width for regression coefficient shuould be more than 1!\n",
                    cmnd);
            exit(1);
         }
         pst->dw.width[i][WLEFT] = -leng;
         pst->dw.width[i][WRIGHT] = leng;
         pst->dw.coef[i] = dcalloc(leng * 2 + 1, 0);
         pst->dw.coef[i] += leng;
      }

      leng = atoi(pst->dw.fn[1]);
      for (a1 = 0, j = -leng; j <= leng; a1 += j * j, j++);
      for (j = -leng; j <= leng; j++)
         pst->dw.coef[1][j] = (double) j / (double) a1;

      if (pst->dw.num > 2) {
         leng = atoi(pst->dw.fn[2]);
         for (a0 = a1 = a2 = 0, j = -leng; j <= leng;
              a0++, a1 += j * j, a2 += j * j * j * j, j++);
         for (j = -leng; j <= leng; j++)
            pst->dw.coef[2][j] =
                ((double) (a0 * j * j - a1)) / ((double) (a2 * a0 - a1 * a1)) /
                2;
      }
   }

   pst->dw.maxw[WLEFT] = pst->dw.maxw[WRIGHT] = 0;
   for (i = 0; i < pst->dw.num; i++) {
      if (pst->dw.maxw[WLEFT] > pst->dw.width[i][WLEFT])
         pst->dw.maxw[WLEFT] = pst->dw.width[i][WLEFT];
      if (pst->dw.maxw[WRIGHT] < pst->dw.width[i][WRIGHT])
         pst->dw.maxw[WRIGHT] = pst->dw.width[i][WRIGHT];
   }

   return;
}
Beispiel #2
0
int main(int argc, char *argv[])
{
   FILE *fp = stdin, *fpc;
   char *coef = NULL;
   double *x = NULL, *dx = NULL, **dw_coef = NULL, *y = NULL;
   int i, j, l, d, t, tj, ispipe, fsize, leng = LENG, total = T;
   int dw_num = 1, **dw_width = NULL, dw_calccoef = -1, dw_coeflen = 1,
       dw_leng = 1;
   char **dw_fn = (char **) calloc(sizeof(char *), argc);
   int non_magic_num, win_size_forward[2], win_size_backward[2];
   float_list *top, *cur, *prev, *tmpf;

   if ((cmnd = strrchr(argv[0], '/')) == NULL)
      cmnd = argv[0];
   else
      cmnd++;

   while (--argc) {
      if (**++argv == '-') {
         switch (*(*argv + 1)) {
         case 'd':
            if (dw_calccoef == 1 || dw_calccoef == 2) {
               fprintf(stderr,
                       "%s : Options '-r' and '-d' should not be defined simultaneously!\n",
                       cmnd);
               return (1);
            }
            dw_calccoef = 0;
            if (isfloat(*++argv)) {
               dw_coeflen = 0;
               for (i = 0; (i < argc - 1) && isfloat(argv[i]); i++) {
                  dw_coeflen += strlen(argv[i]) + 1;
               }
               dw_coeflen += 1;
               coef = dw_fn[dw_num] = getmem(dw_coeflen, sizeof(char));
               for (j = 0; j < i; j++) {
                  sprintf(coef, " %s", *argv);
                  coef += strlen(*argv) + 1;
                  if (j < i - 1) {
                     argv++;
                     argc--;
                  }
               }
            } else {
               dw_fn[dw_num] = *argv;
            }
            dw_num++;
            --argc;
            break;
         case 'r':
            if (dw_calccoef == 0 || dw_calccoef == 2) {
               fprintf(stderr,
                       "%s : Options '-r' and '-d' should not be defined simultaneously!\n",
                       cmnd);
               return (1);
            }
            dw_calccoef = 1;
            dw_coeflen = atoi(*++argv);
            --argc;
            if ((dw_coeflen != 1) && (dw_coeflen != 2)) {
               fprintf(stderr,
                       "%s : Number of delta parameter should be 1 or 2!\n",
                       cmnd);
               return (1);
            }
            if (argc <= 1) {
               fprintf(stderr,
                       "%s : Window size for delta parameter required!\n",
                       cmnd);
               return (1);
            }
            dw_fn[dw_num] = *++argv;
            dw_num++;
            --argc;
            if (dw_coeflen == 2) {
               if (argc <= 1) {
                  fprintf(stderr,
                          "%s : Window size for delta-delta parameter required!\n",
                          cmnd);
                  return (1);
               }
               dw_fn[dw_num] = *++argv;
               dw_num++;
               --argc;
            }
            break;
         case 'm':
            leng = atoi(*++argv) + 1;
            --argc;
            break;
         case 'l':
            leng = atoi(*++argv);
            --argc;
            break;
         case 'R':
            if (dw_calccoef == 0 || dw_calccoef == 1) {
               fprintf(stderr,
                       "%s : Options '-r', '-d' and '-R' should not be defined simultaneously!\n",
                       cmnd);
               return (1);
            }
            dw_calccoef = 2;
            dw_num = atoi(*++argv) + 1;
            --argc;
            if ((dw_num != 2) && (dw_num != 3)) {
               fprintf(stderr,
                       "%s : Number of delta parameter should be 1 or 2!\n",
                       cmnd);
               return (1);
            }
            if (argc <= 1) {
               fprintf(stderr,
                       "%s : Window size for delta-delta parameter required!\n",
                       cmnd);
               return (1);
            }

            sscanf(*++argv, "%d", &win_size_forward[0]);
            --argc;
            sscanf(*++argv, "%d", &win_size_backward[0]);
            --argc;
            if (dw_num > 2) {
               sscanf(*++argv, "%d", &win_size_forward[1]);
               --argc;
               sscanf(*++argv, "%d", &win_size_backward[1]);
               --argc;
            }
            break;
         case 'M':
            sscanf(*++argv, "%lf", &magic);
            MAGIC = TR;
            --argc;
            break;
         case 'h':
            usage(0);
         default:
            fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1));
            usage(1);
         }
      } else
         fp = getfp(*argv, "rb");
   }

   /* parse window files */
   /* memory allocation */
   if ((dw_width = (int **) calloc(dw_num, sizeof(int *))) == NULL) {
      fprintf(stderr, "%s : Cannot allocate memory!\n", cmnd);
      exit(1);
   }
   for (i = 0; i < dw_num; i++)
      if ((dw_width[i] = (int *) calloc(2, sizeof(int))) == NULL) {
         fprintf(stderr, "%s : Cannot allocate memory!\n", cmnd);
         exit(1);
      }
   if ((dw_coef = (double **) calloc(dw_num, sizeof(double *))) == NULL) {
      fprintf(stderr, "%s : Cannot allocate memory!\n", cmnd);
      exit(1);
   }

   /* window for static parameter */
   dw_width[0][0] = dw_width[0][1] = 0;
   dw_coef[0] = dgetmem(1);
   dw_coef[0][0] = 1;

   /* set delta coefficients */
   if (dw_calccoef == 0) {
      for (i = 1; i < dw_num; i++) {
         if (dw_fn[i][0] == ' ') {
            fsize = str2darray(dw_fn[i], &(dw_coef[i]));
         } else {
            /* read from file */
            fpc = getfp(dw_fn[i], "rb");

            /* check the number of coefficients */
            fseek(fpc, 0L, 2);
            fsize = ftell(fpc) / sizeof(real);
            fseek(fpc, 0L, 0);

            /* read coefficients */
            dw_coef[i] = dgetmem(fsize);
            freadf(dw_coef[i], sizeof(**(dw_coef)), fsize, fpc);
         }

         /* set pointer */
         dw_leng = fsize / 2;
         dw_coef[i] += dw_leng;
         dw_width[i][0] = -dw_leng;
         dw_width[i][1] = dw_leng;
         if (fsize % 2 == 0)
            dw_width[i][1]--;
      }
   } else if (dw_calccoef == 1) {
      int a0, a1, a2;
      for (i = 1; i < dw_num; i++) {
         dw_leng = atoi(dw_fn[i]);
         if (dw_leng < 1) {
            fprintf(stderr,
                    "%s : Width for regression coefficient shuould be more than 1!\n",
                    cmnd);
            exit(1);
         }
         dw_width[i][0] = -dw_leng;
         dw_width[i][1] = dw_leng;
         dw_coef[i] = dgetmem(dw_leng * 2 + 1);
         dw_coef[i] += dw_leng;
      }

      dw_leng = atoi(dw_fn[1]);
      for (a1 = 0, j = -dw_leng; j <= dw_leng; a1 += j * j, j++);
      for (j = -dw_leng; j <= dw_leng; j++)
         dw_coef[1][j] = (double) j / (double) a1;

      if (dw_num > 2) {
         dw_leng = atoi(dw_fn[2]);
         for (a0 = a1 = a2 = 0, j = -dw_leng; j <= dw_leng;
              a0++, a1 += j * j, a2 += j * j * j * j, j++);
         for (j = -dw_leng; j <= dw_leng; j++)
            dw_coef[2][j] =
                2 * ((double) (a0 * j * j - a1)) /
                ((double) (a2 * a0 - a1 * a1));
      }
   }

   /* -- Count number of input vectors and read -- */
   x = dgetmem(leng);
   top = prev = (float_list *) malloc(sizeof(float_list));
   top->f = (float *) malloc(sizeof(float) * leng);
   total = 0;
   prev->next = NULL;
   while (freadf(x, sizeof(*x), leng, fp) == leng) {
      cur = (float_list *) malloc(sizeof(float_list));
      cur->f = (float *) malloc(sizeof(float) * leng);
      for (i = 0; i < leng; i++) {
         cur->f[i] = (float) x[i];
      }
      total++;
      prev->next = cur;
      cur->next = NULL;
      prev = cur;
   }
   free(x);
   x = dgetmem(leng * total);
   dx = dgetmem(dw_num * leng * total);
   fillz(dx, sizeof(*x), dw_num * leng * total);
   for (i = 0, tmpf = top->next; tmpf != NULL; i++, tmpf = tmpf->next) {
      for (j = 0; j < leng; j++) {
         x[i * leng + j] = tmpf->f[j];
      }
   }

   if (dw_calccoef == 0 || dw_calccoef == 1) {
      /* calculate delta and delta-delta */
      for (t = 0; t < total; t++) {
         for (d = 0; d < dw_num; d++) {
            for (j = dw_width[d][0]; j <= dw_width[d][1]; j++) {
               tj = t + j;
               if (tj < 0)
                  tj = 0;
               if (!(tj < total))
                  tj = total - 1;
               for (l = 0; l < leng; l++)
                  dx[dw_num * leng * t + leng * d + l] +=
                      dw_coef[d][j] * x[leng * tj + l];
            }
         }
      }

      /* output static, delta, delta-delta */
      fwritef(dx, sizeof(*dx), dw_num * total * leng, stdout);

   } else if (dw_calccoef == 2) {
      int *position = (int *) malloc(sizeof(int) * total);

      /* skip magic number */
      if (MAGIC == TR) {
         for (t = 0, non_magic_num = 0; t < total; t++) {
            for (l = 0; l < leng; l++) {
               if (x[leng * t + l] == magic) {
                  break;
               }
            }
            if (l == leng) {
               /* remember position of non-magic number */
               position[non_magic_num] = t;
               non_magic_num++;
            }
         }
      } else {
         for (t = 0; t < total; t++) {
            position[t] = t;
         }
         non_magic_num = total;
      }

      /* calculate delta and delta-delta */
      GetCoefficient(x, dx, dw_num, position, total, non_magic_num, leng,
                     win_size_forward, win_size_backward);

      /* output static, delta, delta-delta */
      fwritef(dx, sizeof(*dx), dw_num * total * leng, stdout);
   }

   return (0);
}
Beispiel #3
0
Datei: vc.c Projekt: EQ4/SPTK
int main(int argc, char **argv)
{
   size_t i, source_vlen = DEF_L, target_vlen = 0, len_total = 0, num_mix =
       DEF_M, total_frame = 0;
   char *coef = NULL, **dw_fn = (char **) getmem(argc, sizeof(*(dw_fn)));
   int j, k, dw_num = 1, dw_calccoef = -1, dw_coeflen = 1, win_max_width = 0;
   double floor = FLOOR;
   double *source = NULL, *target = NULL, *gv_mean = NULL, *gv_vari = NULL;
   FILE *fp = stdin, *fgmm = NULL, *fgv = NULL;
   Boolean full = TR;
   GMM gmm;
   DELTAWINDOW window;

   memset(dw_fn, 0, argc * sizeof(*dw_fn));

   if ((cmnd = strrchr(argv[0], '/')) == NULL) {
      cmnd = argv[0];
   } else {
      cmnd++;
   }
   while (--argc) {
      if (**++argv == '-') {
         switch (*(*argv + 1)) {
         case 'l':
            source_vlen = atoi(*++argv);
            --argc;
            break;
         case 'n':
            source_vlen = atoi(*++argv) + 1;
            --argc;
            break;
         case 'L':
            target_vlen = atoi(*++argv);
            --argc;
            break;
         case 'N':
            target_vlen = atoi(*++argv) + 1;
            --argc;
            break;
         case 'm':
            num_mix = atoi(*++argv);
            --argc;
            break;
         case 'd':
            if (dw_calccoef == 1 || dw_calccoef == 2) {
               fprintf(stderr,
                       "%s : Options '-r' and '-d' should not be defined simultaneously!\n",
                       cmnd);
               usage(EXIT_FAILURE);
            }
            dw_calccoef = 0;
            if (isfloat(*++argv)) {
               dw_coeflen = 0;
               for (k = 0; (k < argc - 1) && isfloat(argv[k]); k++) {
                  dw_coeflen += strlen(argv[k]) + 1;
               }
               dw_coeflen += 1;
               coef = dw_fn[dw_num] = getmem(dw_coeflen, sizeof(*coef));
               for (j = 0; j < k; j++) {
                  sprintf(coef, " %s", *argv);
                  coef += strlen(*argv) + 1;
                  if (j < k - 1) {
                     argv++;
                     argc--;
                  }
               }
            } else {
               dw_fn[dw_num] = getmem(strlen(*argv) + 1, sizeof(**dw_fn));
               strncpy(dw_fn[dw_num], *argv, strlen(*argv) + 1);
            }
            dw_num++;
            --argc;
            break;
         case 'r':
            if (dw_calccoef == 0 || dw_calccoef == 2) {
               fprintf(stderr,
                       "%s : Options '-r' and '-d' should not be defined simultaneously!\n",
                       cmnd);
               usage(EXIT_FAILURE);
            }
            dw_calccoef = 1;
            dw_coeflen = atoi(*++argv);
            --argc;
            if ((dw_coeflen != 1) && (dw_coeflen != 2)) {
               fprintf(stderr,
                       "%s : Number of delta parameter should be 1 or 2!\n",
                       cmnd);
               usage(EXIT_FAILURE);
            }
            if (argc <= 1) {
               fprintf(stderr,
                       "%s : Window size for delta parameter required!\n",
                       cmnd);
               usage(EXIT_FAILURE);
            }
            dw_fn[dw_num] = getmem(strlen(*++argv) + 1, sizeof(**dw_fn));
            strncpy(dw_fn[dw_num], *argv, strlen(*argv) + 1);
            dw_num++;
            --argc;
            if (dw_coeflen == 2) {
               if (argc <= 1) {
                  fprintf(stderr,
                          "%s : Window size for delta-delta parameter required!\n",
                          cmnd);
                  usage(EXIT_FAILURE);
               }
               dw_fn[dw_num] = getmem(strlen(*++argv) + 1, sizeof(**dw_fn));
               strncpy(dw_fn[dw_num], *argv, strlen(*argv) + 1);
               dw_num++;
               --argc;
            }
            break;
         case 'g':
            fgv = getfp(*++argv, "rb");
            --argc;
            break;
         case 'e':
            floor = atof(*++argv);
            if (floor < 0.0 || isdigit(**argv) == 0) {
               fprintf(stderr,
                       "%s : '-e' option must be specified with positive value.\n",
                       cmnd);
               usage(1);
            }
            --argc;
            break;
         case 'h':
            usage(EXIT_SUCCESS);
         default:
            fprintf(stderr, "%s: Illegal option %s.\n", cmnd, *argv);
            usage(EXIT_FAILURE);
         }
      } else if (fgmm == NULL) {
         fgmm = getfp(*argv, "rb");
      } else {
         fp = getfp(*argv, "rb");
      }
   }

   if (fgmm == NULL) {
      fprintf(stderr, "%s: GMM file must be specified!\n", cmnd);
      usage(EXIT_FAILURE);
   }

   /* set dimensionarity of joint vector */
   if (target_vlen == 0) {
      target_vlen = source_vlen;
   }
   len_total = (source_vlen + target_vlen) * dw_num;

   /* read sequence of source feature vectors */
   source = read_input(fp, source_vlen, &total_frame);
   fclose(fp);
   target = dgetmem(target_vlen * total_frame);

   /* load GMM parameters */
   alloc_GMM(&gmm, num_mix, len_total, full);
   load_GMM(&gmm, fgmm);
   prepareCovInv_GMM(&gmm);
   prepareGconst_GMM(&gmm);
   fclose(fgmm);

   /* flooring for diagonal component of covariance */
   if (floor != 0.0) {
      for (i = 0; i < num_mix; i++) {
         for (j = 0; j < (int) len_total; j++) {
            gmm.gauss[i].cov[j][j] += floor;
         }
      }
   }

   /* load GV parameters */
   if (fgv != NULL) {
      gv_mean = dgetmem(target_vlen);
      gv_vari = dgetmem(target_vlen);
      freadf(gv_mean, sizeof(*gv_mean), target_vlen, fgv);
      freadf(gv_vari, sizeof(*gv_vari), target_vlen, fgv);
      fclose(fgv);
   }

   /* set window parameters */
   window.win_size = dw_num;
   window.win_l_width =
       (int *) getmem(window.win_size, sizeof(*(window.win_l_width)));
   window.win_r_width =
       (int *) getmem(window.win_size, sizeof(*(window.win_r_width)));
   window.win_coefficient =
       (double **) getmem(window.win_size, sizeof(*(window.win_coefficient)));
   window.win_l_width[0] = 0;
   window.win_r_width[0] = 0;
   window.win_coefficient[0] = dgetmem(1);
   window.win_coefficient[0][0] = 1.0;
   if (dw_calccoef == 0) {
      int fsize, dw_leng;
      FILE *fpc = NULL;
      for (i = 1; i < window.win_size; i++) {
         if (dw_fn[i][0] == ' ') {
            fsize = str2darray(dw_fn[i], &(window.win_coefficient[i]));
         } else {
            /* read from file */
            fpc = getfp(dw_fn[i], "rb");

            /* check the number of coefficients */
            fseek(fpc, 0L, SEEK_END);
            fsize = ftell(fpc) / sizeof(float);
            fseek(fpc, 0L, SEEK_SET);
            if (fsize % 2 == 0) {
               fprintf(stderr,
                       "%s : number of delta coefficients must be odd!\n",
                       cmnd);
               usage(EXIT_FAILURE);
            }

            /* read coefficients */
            window.win_coefficient[i] = dgetmem(fsize);
            freadf(window.win_coefficient[i],
                   sizeof(*(window.win_coefficient[i])), fsize, fpc);
         }

         /* set pointer */
         dw_leng = fsize / 2;
         window.win_coefficient[i] += dw_leng;
         window.win_l_width[i] = -dw_leng;
         window.win_r_width[i] = dw_leng;
      }
      fclose(fpc);
   } else if (dw_calccoef == 1) {
      int a0, a1, a2, dw_leng;
      for (i = 1; i < window.win_size; i++) {
         dw_leng = atoi(dw_fn[i]);
         if (dw_leng < 1) {
            fprintf(stderr,
                    "%s : Width for regression coefficient shuould be more than 1!\n",
                    cmnd);
            usage(EXIT_FAILURE);
         }
         window.win_l_width[i] = -dw_leng;
         window.win_r_width[i] = dw_leng;
         window.win_coefficient[i] = dgetmem(dw_leng * 2 + 1);
         window.win_coefficient[i] += dw_leng;
      }
      dw_leng = atoi(dw_fn[1]);
      for (a1 = 0, j = -dw_leng; j <= dw_leng; a1 += j * j, j++);
      for (j = -dw_leng; j <= dw_leng; j++) {
         window.win_coefficient[1][j] = (double) j / (double) a1;
      }

      if (window.win_size > 2) {
         dw_leng = atoi(dw_fn[2]);
         for (a0 = a1 = a2 = 0, j = -dw_leng; j <= dw_leng;
              a0++, a1 += j * j, a2 += j * j * j * j, j++);
         for (j = -dw_leng; j <= dw_leng; j++) {
            window.win_coefficient[2][j]
                = 2 * ((double) (a0 * j * j - a1)) /
                ((double) (a2 * a0 - a1 * a1));
         }
      }
   }
   win_max_width = window.win_r_width[0];       /* width of static window is 0 */
   for (i = 1; i < window.win_size; i++) {
      if (win_max_width < window.win_r_width[i]) {
         win_max_width = window.win_r_width[i];
      }
      if (win_max_width < -window.win_l_width[i]) {
         win_max_width = -window.win_l_width[i];
      }
   }
   window.win_max_width = win_max_width;

   /* perform conversion */
   vc(&gmm, &window, total_frame, source_vlen, target_vlen, gv_mean, gv_vari,
      source, target);

   /* output sequence of converted target static feature vectors */
   fwritef(target, sizeof(*target), target_vlen * total_frame, stdout);

   /* release memory */
   free(source);
   free(target);
   free(gv_mean);
   free(gv_vari);
   free_GMM(&gmm);
   for (i = 0; i < window.win_size; i++) {
      if (dw_fn[i]) {
         free(dw_fn[i]);
      }
      free(window.win_coefficient[i] + window.win_l_width[i]);
   }
   free(dw_fn);
   free(window.win_l_width);
   free(window.win_r_width);
   free(window.win_coefficient);

   return (0);
}