int main(int argc, char **argv) { int m = ORDER, fprd = FPERIOD, iprd = IPERIOD, stage = STAGE, pd = PADEORDER, i, j; Boolean transpose = TRANSPOSE, ngain = NGAIN, inverse = INVERSE; FILE *fp = stdin, *fpc = NULL; double alpha = ALPHA, gamma = -1 / (double) STAGE, x, *c, *inc, *cc, *d; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'm': m = atoi(*++argv); --argc; break; case 'a': alpha = atof(*++argv); --argc; break; case 'c': stage = atoi(*++argv); --argc; break; case 'p': fprd = atoi(*++argv); --argc; break; case 'i': iprd = atoi(*++argv); --argc; break; case 't': transpose = 1 - transpose; break; case 'v': inverse = 1 - inverse; break; case 'k': ngain = 1 - ngain; break; case 'P': pd = atoi(*++argv); --argc; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else if (fpc == NULL) fpc = getfp(*argv, "rb"); else fp = getfp(*argv, "rb"); if (fpc == NULL) { fprintf(stderr, "%s : Cannot open cepstrum file!\n", cmnd); return (1); } if (inverse) { if (stage == 0) { fprintf(stderr, "%s : gamma should not equal to 0 in Inverse MGLSA!\n", cmnd); usage(1); } } if (stage != 0) { /* MGLSA */ gamma = -1 / (double) stage; } else { /* MLSA */ if ((pd < 4) || (pd > 5)) { fprintf(stderr, "%s : Order of Pade approximation should be 4 or 5!\n", cmnd); return (1); } } c = (stage != 0) ? dgetmem(m + m + m + 3 + (m + 1) * stage) /* MGLSA */ : dgetmem(3 * (m + 1) + 3 * (pd + 1) + pd * (m + 2)); /* MLSA */ cc = c + m + 1; inc = cc + m + 1; d = inc + m + 1; if (freadf(c, sizeof(*c), m + 1, fpc) != m + 1) return (1); mc2b(c, c, m, alpha); if (stage != 0) { /* MGLSA */ gnorm(c, c, m, gamma); c[0] = log(c[0]); for (i = 1; i <= m; i++) c[i] *= gamma; } for (;;) { if (freadf(cc, sizeof(*cc), m + 1, fpc) != m + 1) return (0); mc2b(cc, cc, m, alpha); if (stage != 0) { gnorm(cc, cc, m, gamma); cc[0] = log(cc[0]); for (i = 1; i <= m; i++) cc[i] *= gamma; } for (i = 0; i <= m; i++) inc[i] = (cc[i] - c[i]) * iprd / fprd; for (j = fprd, i = (iprd + 1) / 2; j--;) { if (freadf(&x, sizeof(x), 1, fp) != 1) return (0); if (inverse) { /* IMGLSA */ if (!ngain) x /= exp(c[0]); if (transpose) x = imglsadft(x, c, m, alpha, stage, d); else x = imglsadf(x, c, m, alpha, stage, d); } else { if (stage != 0) { /* MGLSA */ if (!ngain) x *= exp(c[0]); if (transpose) x = mglsadft(x, c, m, alpha, stage, d); else x = mglsadf(x, c, m, alpha, stage, d); } else { /* MLSA */ if (!ngain) x *= exp(c[0]); x = mlsadf(x, c, m, alpha, pd, d); } } fwritef(&x, sizeof(x), 1, stdout); if (!--i) { for (i = 0; i <= m; i++) c[i] += inc[i]; i = iprd; } } movem(cc, c, sizeof(*cc), m + 1); } return (0); }
int main(int argc, char **argv) { int m1 = ORDER1, m2 = ORDER2, i; double a1 = ALPHA1, a2 = ALPHA2, g1 = GAMMA1, g2 = GAMMA2, *c1, *c2; Boolean norm1 = NORMFLG1, norm2 = NORMFLG2, mulg1 = MULGFLG1, mulg2 = MULGFLG2; FILE *fp = stdin; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'm': m1 = atoi(*++argv); --argc; break; case 'M': m2 = atoi(*++argv); --argc; break; case 'a': a1 = atof(*++argv); --argc; break; case 'A': a2 = atof(*++argv); --argc; break; case 'g': g1 = atof(*++argv); --argc; break; case 'c': g1 = atoi(*++argv); --argc; if (g1 < 1) fprintf(stderr, "%s : value of c must be c>=1!\n", cmnd); g1 = -1.0 / g1; break; case 'G': g2 = atof(*++argv); --argc; break; case 'C': g2 = atoi(*++argv); --argc; if (g2 < 1) fprintf(stderr, "%s : value of C must be C>=1!\n", cmnd); g2 = -1.0 / g2; break; case 'n': norm1 = 1 - norm1; break; case 'N': norm2 = 1 - norm2; break; case 'u': mulg1 = 1 - mulg1; break; case 'U': mulg2 = 1 - mulg2; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else fp = getfp(*argv, "rb"); c1 = dgetmem(m1 + m2 + 2); c2 = c1 + m1 + 1; if (mulg1 && g1 == 0) { fprintf(stderr, "%s : gamma for input mgc coefficients should not equal to 0 if you specify -u option!\n", cmnd); usage(1); } while (freadf(c1, sizeof(*c1), m1 + 1, fp) == m1 + 1) { if (norm1) ignorm(c1, c1, m1, g1); else if (mulg1) c1[0] = (c1[0] - 1.0) / g1; if (mulg1) for (i = m1; i >= 1; i--) c1[i] /= g1; mgc2mgc(c1, m1, a1, g1, c2, m2, a2, g2); if (norm2) gnorm(c2, c2, m2, g2); else if (mulg2) c1[0] = c1[0] * g2 + 1.0; if (mulg2) for (i = m2; i >= 1; i--) c2[i] *= g2; fwritef(c2, sizeof(*c2), m2 + 1, stdout); } return (0); }
int main(int argc, char **argv) { int m = ORDER, period = PERIOD, stage = STAGE, i, j; FILE *fp = stdin, *fpe = NULL; Boolean ave = AVERAGE, norm = NORM; double lambda = LAMBDA, step = STEP, eps = EPS, *c, *cc, *eg, *ep, *d, *avec, tau = TAU, x, ee, ll, gg, mu, gamma, tt, ttx; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'l': lambda = atof(*++argv); --argc; break; case 't': tau = atof(*++argv); --argc; break; case 'k': step = atof(*++argv); --argc; break; case 'm': m = atoi(*++argv); --argc; break; case 'c': stage = atoi(*++argv); --argc; break; case 'p': period = atoi(*++argv); --argc; break; case 's': ave = 1 - ave; break; case 'n': norm = 1 - norm; break; case 'e': eps = atof(*++argv); --argc; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else fpe = getfp(*argv, "wb"); if (stage == 0) { fprintf(stderr, "%s : gamma should not equal to 0!\n", cmnd); usage(1); } gamma = -1.0 / (double) stage; c = dgetmem(5 * (m + 1) + m * stage); cc = c + m + 1; eg = cc + m + 1; ep = eg + m + 1; avec = ep + m + 1; d = avec + m + 1; j = period; ll = 1.0 - lambda; gg = 1.0; ee = 1.0; step /= (double) m; tt = 2 * (1.0 - tau); while (freadf(&x, sizeof(x), 1, fp) == 1) { eg[m] = d[stage * m - 1]; x = iglsadf1(x, c, m, stage, d); movem(d + (stage - 1) * m, eg, sizeof(*d), m); gg = lambda * gg + ll * eg[0] * eg[0]; gg = (gg < eps) ? eps : gg; mu = step / gg; ttx = tt * x; for (i = 1; i <= m; i++) { ep[i] = tau * ep[i] - ttx * eg[i]; c[i] -= mu * ep[i]; } ee = lambda * ee + ll * x * x; c[0] = sqrt(ee); if (ave) for (i = 0; i <= m; i++) avec[i] += c[i]; if (fpe != NULL) fwritef(&x, sizeof(x), 1, fpe); if (--j == 0) { j = period; if (ave) { for (i = 0; i <= m; i++) avec[i] /= period; if (!norm) ignorm(avec, cc, m, gamma); fwritef(cc, sizeof(*cc), m + 1, stdout); fillz(avec, sizeof(*avec), m + 1); } else if (!norm) { ignorm(c, cc, m, gamma); fwritef(cc, sizeof(*cc), m + 1, stdout); } else fwritef(c, sizeof(*c), m + 1, stdout); } } return (0); }
int main(int argc, char **argv) { int m = ORDER, pd = PADEORDER, fprd = FPERIOD, iprd = IPERIOD, i, j; FILE *fp = stdin, *fpc = NULL; double *c, *inc, *cc, *d, x, a = ALPHA; Boolean bflag = BFLAG, ngain = NGAIN, transpose = TRANSPOSE, inverse = INVERSE; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'm': m = atoi(*++argv); --argc; break; case 'a': a = atof(*++argv); --argc; break; case 'p': fprd = atoi(*++argv); --argc; break; case 'i': iprd = atoi(*++argv); --argc; break; case 'P': pd = atoi(*++argv); --argc; break; case 't': transpose = 1 - transpose; break; case 'v': inverse = 1 - inverse; break; case 'b': bflag = 1 - bflag; break; case 'k': ngain = 1 - ngain; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else if (fpc == NULL) fpc = getfp(*argv, "rb"); else fp = getfp(*argv, "rb"); if ((pd < 4) || (pd > 5)) { fprintf(stderr, "%s : Order of Pade approximation should be 4 or 5!\n", cmnd); return (1); } if (fpc == NULL) { fprintf(stderr, "%s : Cannot open mel cepstrum file!\n", cmnd); return (1); } c = dgetmem(3 * (m + 1) + 3 * (pd + 1) + pd * (m + 2)); cc = c + m + 1; inc = cc + m + 1; d = inc + m + 1; if (freadf(c, sizeof(*c), m + 1, fpc) != m + 1) return (1); if (!bflag) mc2b(c, c, m, a); if (inverse) { if (!ngain) { for (i = 0; i <= m; i++) c[i] *= -1; } else { c[0] = 0; for (i = 1; i <= m; i++) c[i] *= -1; } } for (;;) { if (freadf(cc, sizeof(*cc), m + 1, fpc) != m + 1) return (0); if (!bflag) mc2b(cc, cc, m, a); if (inverse) { if (!ngain) { for (i = 0; i <= m; i++) cc[i] *= -1; } else { cc[0] = 0; for (i = 1; i <= m; i++) cc[i] *= -1; } } for (i = 0; i <= m; i++) inc[i] = (cc[i] - c[i]) * (double) iprd / (double) fprd; for (j = fprd, i = (iprd + 1) / 2; j--;) { if (freadf(&x, sizeof(x), 1, fp) != 1) return (0); if (!ngain) x *= exp(c[0]); if (transpose) x = mlsadft(x, c, m, a, pd, d); else x = mlsadf(x, c, m, a, pd, d); fwritef(&x, sizeof(x), 1, stdout); if (!--i) { for (i = 0; i <= m; i++) c[i] += inc[i]; i = iprd; } } movem(cc, c, sizeof(*cc), m + 1); } return (0); }
int fftr(double *x, double *y, const int m) { int i, j; double *xp, *yp, *xq; double *yq; int mv2, n, tblsize; double xt, yt, *sinp, *cosp; double arg; mv2 = m / 2; /* separate even and odd */ xq = xp = x; yp = y; for (i = mv2; --i >= 0;) { *xp++ = *xq++; *yp++ = *xq++; } if (fft(x, y, mv2) == -1) /* m / 2 point fft */ return (-1); /*********************** * SIN table generation * ***********************/ if ((_sintbl == 0) || (maxfftsize < m)) { tblsize = m - m / 4 + 1; arg = PI / m * 2; if (_sintbl != 0) free(_sintbl); _sintbl = sinp = dgetmem(tblsize); *sinp++ = 0; for (j = 1; j < tblsize; j++) *sinp++ = sin(arg * (double) j); _sintbl[m / 2] = 0; maxfftsize = m; } n = maxfftsize / m; sinp = _sintbl; cosp = _sintbl + maxfftsize / 4; xp = x; yp = y; xq = xp + m; yq = yp + m; *(xp + mv2) = *xp - *yp; *xp = *xp + *yp; *(yp + mv2) = *yp = 0; for (i = mv2, j = mv2 - 2; --i; j -= 2) { ++xp; ++yp; sinp += n; cosp += n; yt = *yp + *(yp + j); xt = *xp - *(xp + j); *(--xq) = (*xp + *(xp + j) + *cosp * yt - *sinp * xt) * 0.5; *(--yq) = (*(yp + j) - *yp + *sinp * yt + *cosp * xt) * 0.5; } xp = x + 1; yp = y + 1; xq = x + m; yq = y + m; for (i = mv2; --i;) { *xp++ = *(--xq); *yp++ = -(*(--yq)); } return (0); }
int main(int argc, char **argv) { int m = ORDER, pd = PADEORDER, fftlen = FFTLENGTH, stable_condition = STABLE1, frame = 0, c = 0; double *mcep, a = ALPHA, r = PADE4_THRESH1, R = 0.0; FILE *fp = stdin; if ((cmnd = strrchr(argv[0], '/')) == NULL) { cmnd = argv[0]; } else { cmnd++; } while (--argc) { if (**++argv == '-') { switch (*(*argv + 1)) { case 'm': m = atoi(*++argv); --argc; break; case 'a': a = atof(*++argv); --argc; break; case 'P': pd = atoi(*++argv); --argc; break; case 'l': fftlen = atoi(*++argv); --argc; break; case 'c': c = atoi(*++argv); if ((c != 0 && c != 1 && c != 2 && c != 3 && c != 4) || isdigit(**argv) == 0) { fprintf(stderr, "%s : '-c' option must be specified with 0, 1, 2, 3 or 4.\n", cmnd); usage(1); } --argc; break; case 'r': stable_condition = atoi(*++argv); if (stable_condition != STABLE1 && stable_condition != STABLE2) { fprintf(stderr, "%s : '-r' option must be specified with %d or %d.\n", cmnd, STABLE1, STABLE2); usage(1); } --argc; break; case 'R': R = atof(*++argv); if (isdigit(**argv) == 0) { fprintf(stderr, "%s : '-R' option must be specified by real number !\n", cmnd); usage(1); } --argc; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else { fp = getfp(*argv, "rb"); } } switch (pd) { case 4: if (stable_condition == STABLE1) r = PADE4_THRESH1; else r = PADE4_THRESH2; break; case 5: if (stable_condition == STABLE1) r = PADE5_THRESH1; else r = PADE5_THRESH2; break; default: fprintf(stderr, "%s : Order of Pade approximation should be 4 or 5!\n", cmnd); usage(1); } if (R != 0.0) r = R; mcep = dgetmem(m + 1); /* check stability of MLSA filter and output */ while (freadf(mcep, sizeof(*mcep), m + 1, fp) == m + 1) { mlsacheck(mcep, m, fftlen, frame, a, r, c); frame++; } return (0); }
int main(int argc, char **argv) { int l = LENG, *cbsize, *index, stage = 0, ss = 0, num, i; char **cbfile; FILE *fp = stdin, *fpcb; double *cb = NULL, *x, *qx, *p; Boolean qflag = QFLAG; cbsize = (int *) calloc(argc / 2, sizeof(*cbsize)); index = (int *) calloc(argc / 2, sizeof(*index)); cbfile = (char **) calloc(argc / 2, sizeof(**cbfile)); if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'l': l = atoi(*++argv); --argc; break; case 'n': l = atoi(*++argv) + 1; --argc; break; case 's': cbsize[stage] = atoi(*++argv); cbfile[stage++] = *++argv; argc -= 2; break; case 'q': qflag = 1 - qflag; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else fp = getfp(*argv, "rb"); for (i = 0, num = 0; i < stage; i++) num += cbsize[i]; cb = dgetmem(num * l); p = cb; for (i = 0; i < stage; i++) { fpcb = getfp(cbfile[i], "rb"); if (freadf(p, sizeof(*p), cbsize[i] * l, fpcb) != cbsize[i] * l) { fprintf(stderr, "%s : Codebook size error of %d stage!\n", cmnd, ss); return (1); } p += cbsize[i] * l; } x = dgetmem(l + l); qx = x + l; if (!qflag) while (freadf(x, sizeof(*x), l, fp) == l) { msvq(x, cb, l, cbsize, stage, index); fwritex(index, sizeof(*index), stage, stdout); } else while (freadf(x, sizeof(*x), l, fp) == l) { msvq(x, cb, l, cbsize, stage, index); imsvq(index, cb, l, cbsize, stage, qx); fwritef(qx, sizeof(*qx), l, stdout); } return (0); }
int main(int argc, char **argv) { int leng = LENG, m = ORDER, itype = ITYPE, otype = OTYPE, gain = GAIN; double alpha = ALPHA, gamma = GAMMA; FILE *fp = stdin; double logk, sampling = SAMPLING; double *x, *lsp; int i, no, loggain = 1; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'a': case 'g': case 'c': if (isdigit(**(argv + 1)) == 0) { /* Check the value is correct */ if (**(argv + 1) != '+' && **(argv + 1) != '-') { fprintf(stderr, "%s : need numerical number !\n", cmnd); usage(1); } else if (isdigit(*(*(argv + 1) + 1)) == 0) { fprintf(stderr, "%s : need numerical number !\n", cmnd); usage(1); } } if ((*(*argv + 1)) == 'a') { alpha = atof(*++argv); } else if ((*(*argv + 1)) == 'g') { gamma = atof(*++argv); if (gamma == 0.0) { fprintf(stderr, "%s : value of g must not be zero!\n", cmnd); } } else { gamma = atof(*++argv); if (gamma < 1) fprintf(stderr, "%s : value of c must be c>=1!\n", cmnd); gamma = -1.0 / gamma; } --argc; break; case 'm': case 's': case 'l': case 'i': case 'q': case 'o': if (isdigit(**(argv + 1)) == 0) { if ((**(argv + 1)) != '+') { fprintf(stderr, "%s : %s option need positive value !\n", cmnd, *argv); usage(1); } else if (isdigit(*(*(argv + 1) + 1)) == 0) { fprintf(stderr, "%s : %s option need positive value !\n", cmnd, *argv); usage(1); } } if ((*(*argv + 1)) == 'm') m = atoi(*++argv); else if ((*(*argv + 1)) == 's') sampling = atof(*++argv); else if ((*(*argv + 1)) == 'l') leng = atoi(*++argv); else if ((*(*argv + 1)) == 'i' || (*(*argv + 1)) == 'q') itype = atoi(*++argv); else if ((*(*argv + 1)) == 'o') otype = atoi(*++argv); --argc; break; case 'L': loggain = 0; break; case 'k': gain = 0; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else fp = getfp(*argv, "rb"); lsp = dgetmem(m + gain); no = leng / 2 + 1; x = dgetmem(no); logk = 20.0 / log(10.0); while (freadf(lsp, sizeof(*lsp), m + gain, fp) == m + gain) { if (itype == 1) for (i = gain; i < m + gain; i++) lsp[i] *= PI2; else if (itype == 2 || itype == 3) { for (i = gain; i < m + gain; i++) lsp[i] = lsp[i] / sampling * PI2; } if (loggain == 0) *lsp = log(*lsp); mgclsp2sp(alpha, gamma, lsp, m, x, no, gain); switch (otype) { case 1: break; case 2: for (i = no; i--;) x[i] = exp(x[i]); break; case 3: for (i = no; i--;) x[i] = exp(2 * x[i]); break; default: for (i = no; i--;) x[i] *= logk; break; } fwritef(x, sizeof(*x), no, stdout); } free(lsp); free(x); return (0); }
int main(int argc, char *argv[]) { FILE *fp = stdin; double *x, *mean, *med = NULL, **mtmp = NULL, **cov = NULL, **invcov = NULL, *var = NULL, conf = CONFLEV, *upper = NULL, *lower = NULL, t, err; int leng = LENG, nv = -1, i, j, k = 0, lp = 0, m, outtype = 0, count = 0; Boolean outmean = OUTMEAN, outcov = OUTCOV, outconf = OUTCONF, outmed = OUTMED, diagc = DIAGC, inv = INV, corr = CORR; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'l': leng = atoi(*++argv); --argc; break; case 'n': leng = atoi(*++argv) + 1; --argc; break; case 't': nv = atoi(*++argv); --argc; break; case 'o': outtype = atoi(*++argv); --argc; break; case 'c': conf = atof(*++argv); --argc; break; case 'd': diagc = 1 - diagc; break; case 'i': inv = 1 - inv; break; case 'r': corr = 1 - corr; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else fp = getfp(*argv, "rb"); if (conf < 0 || conf > 100) { fprintf(stderr, "%s : Confidence level must be greater than 0 and less than 1.0!\n", cmnd); } switch (outtype) { case 1: outcov = FA; break; case 2: outmean = FA; break; case 3: outcov = FA; outconf = TR; break; case 4: outcov = FA; outmean = FA; outmed = TR; break; } if (diagc && corr) diagc = FA; if (diagc && inv) diagc = FA; if (corr && inv) corr = FA; mean = dgetmem(leng + leng); x = mean + leng; if (outmed) { if (nv == -1) { typedef struct _float_list { float *f; struct _float_list *next; } float_list; float_list *top = NULL, *prev = NULL, *cur = NULL; top = prev = (float_list *) getmem(1, sizeof(float_list)); prev->next = NULL; while (freadf(x, sizeof(*x), leng, fp) == leng) { cur = (float_list *) getmem(1, sizeof(float_list)); cur->f = fgetmem(leng); for (i = 0; i < leng; i++) { cur->f[i] = (float) x[i]; } count++; prev->next = cur; cur->next = NULL; prev = cur; } k = count; mtmp = (double **) getmem(leng, sizeof(*mtmp)); mtmp[0] = dgetmem(leng * k); for (i = 1; i < leng; i++) mtmp[i] = mtmp[i - 1] + k; med = dgetmem(leng); for (j = 0, cur = top->next; j < k; j++, cur = cur->next) { for (i = 0; i < leng; i++) { mtmp[i][j] = (double) cur->f[i]; } } } else { k = nv; mtmp = (double **) getmem(leng, sizeof(*mtmp)); mtmp[0] = dgetmem(leng * k); for (i = 1; i < leng; i++) mtmp[i] = mtmp[i - 1] + k; med = dgetmem(leng); for (j = 0; j < k; j++) { for (i = 0; i < leng; i++) { freadf(&mtmp[i][j], sizeof(**mtmp), 1, fp); } } } if (k % 2 == 0) { fprintf(stderr, "%s : warning: the number of vectors is even!\n", cmnd); } for (i = 0; i < leng; i++) { quicksort(mtmp[i], 0, k - 1); if (k % 2 == 1) { med[i] = mtmp[i][k / 2]; } else { med[i] = ((mtmp[i][k / 2] + mtmp[i][k / 2 - 1]) / 2); } } fwritef(med, sizeof(*med), leng, stdout); return (0); } if (outcov) { if (!diagc) { cov = (double **) getmem(leng, sizeof(*cov)); cov[0] = dgetmem(leng * leng); for (i = 1; i < leng; i++) cov[i] = cov[i - 1] + leng; if (inv) { invcov = (double **) getmem(leng, sizeof(*invcov)); invcov[0] = dgetmem(leng * leng); for (i = 1; i < leng; i++) invcov[i] = invcov[i - 1] + leng; } } else var = dgetmem(leng); } if (outconf) { var = dgetmem(leng); upper = dgetmem(leng); lower = dgetmem(leng); } while (!feof(fp)) { for (i = 0; i < leng; i++) { mean[i] = 0.0; if (outcov) { if (!diagc) for (j = 0; j < leng; j++) cov[i][j] = 0.0; else var[i] = 0.0; } if (outconf) { var[i] = 0.0; } } for (lp = nv; lp;) { if (freadf(x, sizeof(*x), leng, fp) != leng) break; for (i = 0; i < leng; i++) { mean[i] += x[i]; if (outcov) { if (!diagc) for (j = i; j < leng; j++) cov[i][j] += x[i] * x[j]; else var[i] += x[i] * x[i]; } if (outconf) { var[i] += x[i] * x[i]; } } --lp; } if (lp == 0 || nv == -1) { if (nv > 0) k = nv; else k = -lp - 1; for (i = 0; i < leng; i++) mean[i] /= k; if (outcov) { if (!diagc) for (i = 0; i < leng; i++) for (j = i; j < leng; j++) cov[j][i] = cov[i][j] = cov[i][j] / k - mean[i] * mean[j]; else for (i = 0; i < leng; i++) var[i] = var[i] / k - mean[i] * mean[i]; } if (outconf) { for (i = 0; i < leng; i++) { var[i] = (var[i] - k * mean[i] * mean[i]) / (k - 1); } t = t_percent(conf / 100, k - 1); for (i = 0; i < leng; i++) { err = t * sqrt(var[i] / k); upper[i] = mean[i] + err; lower[i] = mean[i] - err; } } if (corr) { for (i = 0; i < leng; i++) for (j = i + 1; j < leng; j++) cov[j][i] = cov[i][j] = cov[i][j] / sqrt(cov[i][i] * cov[j][j]); for (i = 0; i < leng; i++) cov[i][i] = 1.0; } if (outmean) fwritef(mean, sizeof(*mean), leng, stdout); if (outcov) { if (!diagc) { if (inv) { for (i = 0; i < leng; i++) { for (j = i + 1; j < leng; j++) { cov[j][i] /= cov[i][i]; for (m = i + 1; m < leng; m++) cov[j][m] -= cov[i][m] * cov[j][i]; } } for (m = 0; m < leng; m++) { for (i = 0; i < leng; i++) { if (i == m) invcov[i][m] = 1.0; else invcov[i][m] = 0.0; } for (i = 0; i < leng; i++) { for (j = i + 1; j < leng; j++) invcov[j][m] -= invcov[i][m] * cov[j][i]; } for (i = leng - 1; i >= 0; i--) { for (j = i + 1; j < leng; j++) invcov[i][m] -= cov[i][j] * invcov[j][m]; invcov[i][m] /= cov[i][i]; } } fwritef(invcov[0], sizeof(*invcov[0]), leng * leng, stdout); } else fwritef(cov[0], sizeof(*cov[0]), leng * leng, stdout); } else fwritef(var, sizeof(*var), leng, stdout); } if (outconf) { fwritef(upper, sizeof(*upper), leng, stdout); fwritef(lower, sizeof(*lower), leng, stdout); } } } return (0); }
void lbg(double *x, const int l, const int tnum, double *icb, int icbsize, double *cb, const int ecbsize, const int iter, const int mintnum, const int seed, const int centup, const double delta, const double end) { int i, j, k, it, maxindex, tnum1, tnum2; static int *cntcb, *tindex, size, sizex, sizecb; unsigned long next = SEED; double d0, d1, dl, err, tmp, rand; static double *cb1 = NULL; double *p, *q, *r; if (cb1 == NULL) { cb1 = dgetmem(ecbsize * l); tindex = (int *) dgetmem(tnum); cntcb = (int *) dgetmem(ecbsize); size = l; sizex = tnum; sizecb = ecbsize; } if (l > size) { free(cb1); cb1 = dgetmem(ecbsize * l); size = l; } if (tnum > sizex) { free(tindex); tindex = (int *) dgetmem(tnum); sizex = tnum; } if (ecbsize > sizecb) { free(cb1); free(cntcb); cb1 = dgetmem(ecbsize * l); cntcb = (int *) dgetmem(ecbsize); } movem(icb, cb, sizeof(*icb), icbsize * l); if (seed != 1) next = srnd((unsigned int) seed); for (; icbsize * 2 <= ecbsize;) { q = cb; r = cb + icbsize * l; for (i = 0; i < icbsize; i++) { for (j = 0; j < l; j++) { dl = delta * nrandom(&next); *r = *q - dl; r++; *q = *q + dl; q++; } } icbsize *= 2; d0 = MAXVALUE; for (it = 1; it <= iter; it++) { fillz((double *) cntcb, sizeof(*cntcb), icbsize); d1 = 0.0; p = x; for (i = 0; i < tnum; i++, p += l) { tindex[i] = vq(p, cb, l, icbsize); cntcb[tindex[i]]++; q = cb + tindex[i] * l; d1 += edist(p, q, l); } d1 /= tnum; err = abs((d0 - d1) / d1); if (err < end) break; d0 = d1; fillz(cb1, sizeof(*cb), icbsize * l); p = x; for (i = 0; i < tnum; i++) { q = cb1 + tindex[i] * l; for (j = 0; j < l; j++) *q++ += *p++; } k = maxindex = 0; for (i = 0; i < icbsize; i++) if (cntcb[i] > k) { k = cntcb[i]; maxindex = i; } q = cb; r = cb1; for (i = 0; i < icbsize; i++, r += l, q += l) if (cntcb[i] >= mintnum) for (j = 0; j < l; j++) q[j] = r[j] / (double) cntcb[i]; else { if (centup == 1) { p = cb + maxindex * l; for (j = 0; j < l; j++) { rand = nrandom(&next); q[j] = p[j] + delta * rand; p[j] = p[j] - delta * rand; } } else if (centup == 2) { if (i < icbsize / 2) { p = q + icbsize / 2 * l; tnum1 = cntcb[i]; tnum2 = cntcb[i + icbsize / 2]; for (j = 0; j < l; j++) { tmp = (tnum2 * q[j] + tnum1 * p[j]) / (tnum1 + tnum2); rand = nrandom(&next); q[j] = tmp + delta * rand; p[j] = tmp - delta * rand; } } else { p = q - icbsize / 2 * l; tnum1 = cntcb[i]; tnum2 = cntcb[i - icbsize / 2]; for (j = 0; j < l; j++) { tmp = (tnum2 * q[j] + tnum1 * p[j]) / (tnum1 + tnum2); rand = nrandom(&next); q[j] = tmp + delta * rand; p[j] = tmp - delta * rand; } } } } } if (icbsize == ecbsize) break; } return; }
void lsp2lpc(double *lsp, double *a, const int m) { int i, k, mh1, mh2, flag_odd; double xx, xf, xff; static double *f = NULL, *p, *q, *a0, *a1, *a2, *b0, *b1, *b2; static int size; flag_odd = 0; if (m % 2 == 0) mh1 = mh2 = m / 2; else { mh1 = (m + 1) / 2; mh2 = (m - 1) / 2; flag_odd = 1; } if (f == NULL) { f = dgetmem(5 * m + 6); p = f + m; q = p + mh1; a0 = q + mh2; a1 = a0 + (mh1 + 1); a2 = a1 + (mh1 + 1); b0 = a2 + (mh1 + 1); b1 = b0 + (mh2 + 1); b2 = b1 + (mh2 + 1); size = m; } if (m > size) { free(f); f = dgetmem(5 * m + 6); p = f + m; q = p + mh1; a0 = q + mh2; a1 = a0 + (mh1 + 1); a2 = a1 + (mh1 + 1); b0 = a2 + (mh1 + 1); b1 = b0 + (mh2 + 1); b2 = b1 + (mh2 + 1); size = m; } movem(lsp, f, sizeof(*lsp), m); fillz(a0, sizeof(*a0), mh1 + 1); fillz(b0, sizeof(*b0), mh2 + 1); fillz(a1, sizeof(*a1), mh1 + 1); fillz(b1, sizeof(*b1), mh2 + 1); fillz(a2, sizeof(*a2), mh1 + 1); fillz(b2, sizeof(*b2), mh2 + 1); /* lsp filter parameters */ for (i = k = 0; i < mh1; i++, k += 2) p[i] = -2.0 * cos(PI2 * f[k]); for (i = k = 0; i < mh2; i++, k += 2) q[i] = -2.0 * cos(PI2 * f[k + 1]); /* impulse response of analysis filter */ xx = 1.0; xf = xff = 0.0; for (k = 0; k <= m; k++) { if (flag_odd) { a0[0] = xx; b0[0] = xx - xff; xff = xf; xf = xx; } else { a0[0] = xx + xf; b0[0] = xx - xf; xf = xx; } for (i = 0; i < mh1; i++) { a0[i + 1] = a0[i] + p[i] * a1[i] + a2[i]; a2[i] = a1[i]; a1[i] = a0[i]; } for (i = 0; i < mh2; i++) { b0[i + 1] = b0[i] + q[i] * b1[i] + b2[i]; b2[i] = b1[i]; b1[i] = b0[i]; } if (k != 0) a[k - 1] = -0.5 * (a0[mh1] + b0[mh2]); xx = 0.0; } for (i = m - 1; i >= 0; i--) a[i + 1] = -a[i]; a[0] = 1.0; return; }
int main(int argc, char *argv[]) { FILE *fp; char *s, *infile = NULL, c; int size = SIZE, nout = 0, k, nd = -1, out = ' '; double *x, *y; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) { if (*(s = *++argv) == '-') { c = *++s; if ((c == 'l' || c == 'm') && (*++s == '\0')) { s = *++argv; --argc; } switch (c) { case 'l': size = atoi(s); break; case 'm': nd = atoi(s) + 1; break; case 'H': nout = 1; break; case 'i': case 'p': case 'r': c -= ('a' - 'A'); case 'A': case 'I': case 'P': case 'R': out = c; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, c); usage(1); } } else infile = s; } if (nd == -1) nd = size; if (nd > size) { fprintf(stderr, "%s : Order of sequence %d should be less than the FFT size %d!\n", cmnd, nd, size); return (1); } nout = (nout) ? size / 2 + 1 : size; fp = stdin; if (infile) { fp = getfp(infile, "rb"); } x = dgetmem(size + size); y = x + size; while (!feof(fp)) { fillz(x, size, sizeof(*x)); if (freadf(x, sizeof(*x), nd, fp) == 0) break; fftr(x, y, size); if (out == 'P') for (k = 0; k < size; k++) x[k] = x[k] * x[k] + y[k] * y[k]; else if (out == 'A') for (k = 0; k < size; k++) x[k] = sqrt(x[k] * x[k] + y[k] * y[k]); if (out != 'I') fwritef(x, sizeof(*x), nout, stdout); if (out == ' ' || out == 'I') fwritef(y, sizeof(*y), nout, stdout); } if (infile) { fclose(fp); } return (0); }
int main(int argc, char **argv) { FILE *fp = stdin, *fgmm = NULL; GMM gmm; double logp, ave_logp, *x; int M = DEF_M, L = DEF_L, T; Boolean aflag = DEF_A, full = FULL; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'h': usage(0); break; case 'l': L = atoi(*++argv); --argc; break; case 'm': M = atoi(*++argv); --argc; break; case 'f': full = TR - full; break; case 'a': aflag = TR; break; default: fprintf(stderr, "%s: Illegal option \"%s\".\n", cmnd, *argv); usage(1); } } else if (fgmm == NULL) fgmm = getfp(*argv, "rb"); else fp = getfp(*argv, "rb"); /* Read GMM parameters */ if (fgmm == NULL) { fprintf(stderr, "%s: GMM file must be specified!\n", cmnd); usage(1); } alloc_GMM(&gmm, M, L, full); load_GMM(&gmm, fgmm); prepareCovInv_GMM(&gmm); prepareGconst_GMM(&gmm); fclose(fgmm); /* Calculate and output log-probability */ T = 0; ave_logp = 0.0; x = dgetmem(L); while (freadf(x, sizeof(*x), L, fp) == L) { if (!aflag) { logp = log_outp(&gmm, L, x); fwritef(&logp, sizeof(double), 1, stdout); } else { ave_logp += log_outp(&gmm, L, x); T++; } } fclose(fp); if (aflag) { if (T == 0) { fprintf(stderr, "%s: No input data!\n", cmnd); usage(1); } else { ave_logp /= (double) T; fwritef(&ave_logp, sizeof(double), 1, stdout); } } return (0); }
int main(int argc, char **argv) { int length, frame_shift = FRAME_SHIFT, atype = ATYPE, otype = OTYPE; double *x, thresh_rapt = THRESH_RAPT, thresh_swipe = THRESH_SWIPE, thresh_reaper = THRESH_REAPER, sample_freq = SAMPLE_FREQ, L = LOW, H = HIGH; FILE *fp = stdin; float_list *top, *cur, *prev; void rapt(float_list * flist, int length, double sample_freq, int frame_shift, double min, double max, double threshold, int otype); void swipe(float_list * input, int length, double sample_freq, int frame_shift, double min, double max, double threshold, int otype); void reaper(float_list * input, int length, double sample_freq, int frame_shift, double min, double max, double threshold, int otype); if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'a': atype = atoi(*++argv); --argc; break; case 's': sample_freq = atof(*++argv); --argc; break; case 'p': frame_shift = atoi(*++argv); --argc; break; case 't': if ((*(*argv + 2)) == '0') { thresh_rapt = atof(*++argv); --argc; } else if ((*(*argv + 2)) == '1') { thresh_swipe = atof(*++argv); --argc; } else { thresh_reaper = atof(*++argv); --argc; } break; case 'L': L = atof(*++argv); --argc; break; case 'H': H = atof(*++argv); --argc; break; case 'o': otype = atoi(*++argv); --argc; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else { fp = getfp(*argv, "rb"); } sample_freq *= 1000.0; x = dgetmem(1); top = prev = (float_list *) malloc(sizeof(float_list)); length = 0; prev->next = NULL; while (freadf(x, sizeof(*x), 1, fp) == 1) { cur = (float_list *) malloc(sizeof(float_list)); cur->f = (float) x[0]; length++; prev->next = cur; cur->next = NULL; prev = cur; } if (atype == 0) { rapt(top->next, length, sample_freq, frame_shift, L, H, thresh_rapt, otype); } else if (atype == 1) { swipe(top->next, length, sample_freq, frame_shift, L, H, thresh_swipe, otype); } else { reaper(top->next, length, sample_freq, frame_shift, L, H, thresh_reaper, otype); } return (0); }
int main(int argc, char **argv) { int m = ORDER, otype = OTYPE, n = SPNUM, p = MAXITR, i; FILE *fp = stdin; double *a, *lsp, end = END, sampling = SAMPLING; Boolean gain = GAIN, loggain = LOGGAIN; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'm': m = atoi(*++argv); --argc; break; case 's': sampling = atof(*++argv); --argc; break; case 'o': otype = atoi(*++argv); --argc; break; case 'p': p = atoi(*++argv); --argc; break; case 'n': n = atoi(*++argv); --argc; break; case 'd': end = atof(*++argv); --argc; break; case 'k': gain = 1 - gain; break; case 'l': case 'L': loggain = 1 - loggain; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else fp = getfp(*argv, "rb"); lsp = dgetmem(m + m + 1); a = lsp + m; while (freadf(a, sizeof(*a), m + 1, fp) == m + 1) { lpc2lsp(a, lsp, m, n, p, end); if (otype == 0) for (i = 0; i < m; i++) lsp[i] *= PI2; else if (otype == 2 || otype == 3) for (i = 0; i < m; i++) lsp[i] *= sampling; if (otype == 3) for (i = 0; i < m; i++) lsp[i] *= 1000; if (gain) { if (loggain) *a = log(*a); fwritef(a, sizeof(*a), 1, stdout); } fwritef(lsp, sizeof(*lsp), m, stdout); } return (0); }
int main(int argc, char **argv) { int m = ORDER, fprd = FPERIOD, iprd = IPERIOD, i, j; FILE *fp = stdin, *fpc = NULL; double *c, *inc, *cc, *d, x; Boolean ngain = NGAIN; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'm': m = atoi(*++argv); --argc; break; case 'p': fprd = atoi(*++argv); --argc; break; case 'i': iprd = atoi(*++argv); --argc; break; case 'k': ngain = 1 - ngain; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else if (fpc == NULL) fpc = getfp(*argv, "rb"); else fp = getfp(*argv, "rb"); if (fpc == NULL) { fprintf(stderr, "%s : Cannot open cepstrum file!\n", cmnd); return (1); } c = dgetmem(m + m + m + 3 + m); cc = c + m + 1; inc = cc + m + 1; d = inc + m + 1; if (freadf(c, sizeof(*c), m + 1, fpc) != m + 1) return (1); for (;;) { if (freadf(cc, sizeof(*cc), m + 1, fpc) != m + 1) return (0); for (i = 0; i <= m; i++) inc[i] = (cc[i] - c[i]) * iprd / fprd; for (j = fprd, i = (iprd + 1) / 2; j--;) { if (freadf(&x, sizeof(x), 1, fp) != 1) return (0); if (!ngain) x *= c[0]; x = ltcdf(x, c, m, d); fwritef(&x, sizeof(x), 1, stdout); if (!--i) { for (i = 0; i <= m; i++) c[i] += inc[i]; i = iprd; } } movem(cc, c, sizeof(*cc), m + 1); } return (0); }
void mlsacheck(double *mcep, int m, int fftlen, int frame, double a, double r, int c) { int i; double gain, *x, *y, *mag = NULL, max = 0.0; x = dgetmem(fftlen); y = dgetmem(fftlen); fillz(x, sizeof(*x), fftlen); fillz(y, sizeof(*y), fftlen); /* calculate gain factor */ for (i = 0, gain = 0.0; i <= m; i++) { x[i] = mcep[i]; gain += x[i] * pow(-a, i); } /* gain normalization */ x[0] -= gain; /* check stability */ if (c == 0 || c == 2 || c == 3) { /* usual mode */ mag = dgetmem(fftlen); fillz(mag, sizeof(*mag), fftlen); fftr(x, y, fftlen); for (i = 0; i < fftlen; i++) { mag[i] = sqrt(x[i] * x[i] + y[i] * y[i]); if (mag[i] > max) max = mag[i]; } } else { /* fast mode */ for (i = 0; i <= m; i++) max += x[i]; } /* modification MLSA filter coefficients */ if (max > r) { /* output ascii report */ fprintf(stderr, "[No. %d] is unstable frame (maximum = %f, threshold = %f)\n", frame, max, r); /* modification */ if (c == 2) { /* clipping */ for (i = 0; i < fftlen; i++) { if (mag[i] > r) { x[i] *= r / mag[i]; y[i] *= r / mag[i]; } } } else if (c == 3) { /* scaling */ for (i = 0; i < fftlen; i++) { x[i] *= r / max; y[i] *= r / max; } } else if (c == 4) { /* fast mode */ for (i = 0; i <= m; i++) x[i] *= r / max; } } /* output MLSA filter coefficients */ if (c == 0 || c == 1 || max <= r) { /* no modification */ fwritef(mcep, sizeof(*mcep), m + 1, stdout); } else { if (c == 2 || c == 3) ifft(x, y, fftlen); x[0] += gain; fwritef(x, sizeof(*x), m + 1, stdout); } free(x); free(y); if (c == 0 || c == 2 || c == 3) free(mag); }
int main(int argc, char **argv) { int m = ORDER, flng = FLENG, ilng = FLENG, itype = ITYPE, etype = ETYPE, fftsz = FFTSZ, itr1 = MINITR, itr2 = MAXITR, flag = 0; FILE *fp = stdin; double *mc, *x, a = ALPHA, t = THETA, end = END, e = EPS, f = MINDET, s = SAMPLEF, T = EMPHHZ; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'a': a = atof(*++argv); --argc; break; case 't': t = atof(*++argv); --argc; break; case 'T': T = atof(*++argv); --argc; break; case 'm': m = atoi(*++argv); --argc; break; case 'l': flng = atoi(*++argv); --argc; break; case 's': s = atof(*++argv); --argc; break; case 'L': fftsz = atoi(*++argv); --argc; break; case 'q': itype = atoi(*++argv); --argc; break; case 'i': itr1 = atoi(*++argv); --argc; break; case 'j': itr2 = atoi(*++argv); --argc; break; case 'd': end = atof(*++argv); --argc; break; case 'e': etype = 1; e = atof(*++argv); --argc; break; case 'E': etype = 2; e = atof(*++argv); --argc; break; case 'f': f = atof(*++argv); --argc; break; case 'h': usage(0); default: fprintf(stderr, "%s : invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else fp = getfp(*argv, "rb"); if (T != 0.00 && t != 0.00) { fprintf(stderr, "%s : option t and T can't be used at the same time!\n", cmnd); usage(1); } else if (T > 1000 * (s / 2)) { fprintf(stderr, "%s : value of T must be T <= 1000*s/2 !\n", cmnd); usage(1); } else if (T > 0.00) { T /= 1000; t = (T / (s / 2)); } t *= M_PI; if (itype == 0) ilng = flng; else ilng = flng / 2 + 1; x = dgetmem(flng + m + 1); mc = x + flng; while (freadf(x, sizeof(*x), ilng, fp) == ilng) { flag = smcep(x, flng, mc, m, fftsz, a, t, itr1, itr2, end, etype, e, f, itype); fwritef(mc, sizeof(*mc), m + 1, stdout); } return (0); }
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); }
int main(int argc, char **argv) { int m = ORDER, flng = FLENG, ilng = FLENG, itr1 = MINITR, itr2 = MAXITR, itype = ITYPE, norm = NORM, flag = 0; FILE *fp = stdin; double *gc, *x, g = GAMMA, end = END, e = EPS, f = MINDET; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'm': m = atoi(*++argv); --argc; break; case 'l': flng = atoi(*++argv); --argc; break; case 'q': itype = atoi(*++argv); --argc; break; case 'g': g = atof(*++argv); --argc; break; case 'c': g = atoi(*++argv); --argc; if (g < 1) fprintf(stderr, "%s : value of c must be c>=1!\n", cmnd); g = -1.0 / g; break; case 'n': norm = 1 - norm; break; case 'i': itr1 = atoi(*++argv); --argc; break; case 'j': itr2 = atoi(*++argv); --argc; break; case 'd': end = atof(*++argv); --argc; break; case 'e': e = atof(*++argv); --argc; break; case 'f': f = atof(*++argv); --argc; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else fp = getfp(*argv, "rb"); if (itype == 0) ilng = flng; else ilng = flng / 2 + 1; x = dgetmem(flng + m + 1); gc = x + flng; while (freadf(x, sizeof(*x), ilng, fp) == ilng) { flag = gcep(x, flng, gc, m, g, itr1, itr2, end, e, f, itype); if (!norm) ignorm(gc, gc, m, g); fwritef(gc, sizeof(*gc), m + 1, stdout); } return 0; }
int main(int argc, char **argv) { int m = ORDER, period = PERIOD, i, j, pd = PADEORD; FILE *fp = stdin, *fpe = NULL; Boolean aveflag = AVEFLAG; double lambda = LAMBDA, step = STEP, tau = TAU, eps = EPS, *c, *e, *ep, *cc, *d, *avec, x, ll, gg, tt, mu, ttx; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'l': lambda = atof(*++argv); --argc; break; case 't': tau = atof(*++argv); --argc; break; case 'k': step = atof(*++argv); --argc; break; case 'm': m = atoi(*++argv); --argc; break; case 'p': period = atoi(*++argv); --argc; break; case 's': aveflag = 1 - aveflag; break; case 'P': pd = atoi(*++argv); --argc; break; case 'e': eps = atof(*++argv); --argc; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else fpe = getfp(*argv, "wb"); if ((pd < 4) || (pd > 5)) { fprintf(stderr, "%s : Order of Pade approximation should be 4 or 5!\n", cmnd); return (1); } c = dgetmem(5 * (m + 1) + (m + 1) * pd * 2); cc = c + m + 1; e = cc + m + 1; ep = e + m + 1; avec = ep + m + 1; d = avec + m + 1; j = period; ll = 1.0 - lambda; gg = 1.0; step /= (double) m; tt = 2 * (1.0 - tau); while (freadf(&x, sizeof(x), 1, fp) == 1) { for (i = 1; i <= m; i++) cc[i] = -c[i]; x = lmadf(x, cc, m, pd, d); for (i = m; i >= 1; i--) e[i] = e[i - 1]; e[0] = x; gg = gg * lambda + ll * e[0] * e[0]; c[0] = 0.5 * log(gg); gg = (gg < eps) ? eps : gg; mu = step / gg; ttx = tt * e[0]; for (i = 1; i <= m; i++) { ep[i] = tau * ep[i] - ttx * e[i]; c[i] -= mu * ep[i]; } if (aveflag) for (i = 0; i <= m; i++) avec[i] += c[i]; if (fpe != NULL) fwritef(&x, sizeof(x), 1, fpe); if (--j == 0) { j = period; if (aveflag) { for (i = 0; i <= m; i++) avec[i] /= period; fwritef(avec, sizeof(*avec), m + 1, stdout); fillz(avec, sizeof(*avec), m + 1); } else fwritef(c, sizeof(*c), m + 1, stdout); } } return (0); }
int main(int argc, char *argv[]) { FILE *fp; char *s, *infile = NULL, c; int size2; double *x, *y; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) { if (*(s = *++argv) == '-') { c = *++s; if ((c == 'l') && (*++s == '\0')) { s = *++argv; --argc; } switch (c) { case 'l': size = atoi(s); break; case 'i': case 'r': c -= ('a' - 'A'); case 'I': case 'R': out = c; break; case 'h': default: usage(); } } else infile = s; } fp = stdin; if (infile) { fp = getfp(infile, "rb"); } x = dgetmem(size2 = size + size); y = x + size; while (!feof(fp)) { if (freadf(x, sizeof(*x), size2, fp) != size2) break; ifft(x, y, size); if (out != 'I') fwritef(x, sizeof(*x), size, stdout); if (out != 'R') fwritef(y, sizeof(*y), size, stdout); } if (infile) { fclose(fp); } return (0); }
int main(int argc, char **argv) { FILE *fp = stdin; GMM gmm, tgmm, floor; double E = DEF_E, V = DEF_V, W = DEF_W, *dat, *pd, *cb, *icb, *logwgd, logb, *sum, *sum_m, **sum_v, diff, sum_w, ave_logp0, ave_logp1, change = MAXVALUE, tmp1, tmp2; int ispipe, l, ll, L = DEF_L, m, M = DEF_M, N, t, T = DEF_T, S = DEF_S, full = FULL, n1, i, j, Imin = DEF_IMIN, Imax = DEF_IMAX, *tindex, *cntcb; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; /* -- Check options -- */ while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'h': usage(0); break; case 'l': L = atoi(*++argv); --argc; break; case 'm': M = atoi(*++argv); --argc; break; case 't': T = atoi(*++argv); --argc; break; case 's': S = atoi(*++argv); --argc; break; case 'a': Imin = atoi(*++argv); --argc; break; case 'b': Imax = atoi(*++argv); --argc; break; case 'e': E = atof(*++argv); --argc; break; case 'v': V = atof(*++argv); --argc; break; case 'w': W = atof(*++argv); --argc; break; case 'f': full = 1 - full; break; default: fprintf(stderr, "%s: Illegal option \"%s\".\n", cmnd, *argv); usage(1); } } else fp = getfp(*argv, "rb"); /* -- Count number of training vectors -- */ if (T == -1) { ispipe = fseek(fp, 0L, SEEK_END); T = (int) (ftell(fp) / (double) L / (double) sizeof(float)); rewind(fp); if (ispipe == -1) { /* training data is from standard input via pipe */ fprintf(stderr, "\n %s (Error) -t option must be specified for the standard input via pipe.\n", cmnd); usage(1); } } /* Memory allocation */ /* Training data */ dat = dgetmem(T * L); /* for VQ */ N = 1; while (N < M) N *= 2; cb = dgetmem(N * L); icb = dgetmem(L); tindex = (int *) getmem(T, sizeof(int)); cntcb = (int *) getmem(M, sizeof(int)); /* GMM */ gmm.weight = dgetmem(M); gmm.gauss = (Gauss *) getmem(M, sizeof(Gauss)); for (m = 0; m < M; m++) { gmm.gauss[m].mean = dgetmem(L); gmm.gauss[m].var = dgetmem(L); if (full == 1) { gmm.gauss[m].cov = (double **) malloc(sizeof(double *) * L); gmm.gauss[m].inv = (double **) malloc(sizeof(double *) * L); for (l = 0; l < L; l++) { gmm.gauss[m].cov[l] = dgetmem(L); gmm.gauss[m].inv[l] = dgetmem(L); } } } if (full == 1) { floor.gauss = (Gauss *) getmem(1, sizeof(Gauss)); floor.gauss[0].cov = (double **) malloc(sizeof(double *) * L); for (l = 0; l < L; l++) floor.gauss[0].cov[l] = dgetmem(L); sum_m = dgetmem(L); sum_v = (double **) malloc(sizeof(double *) * L); } /* temporary */ tgmm.weight = dgetmem(M); tgmm.gauss = (Gauss *) getmem(M, sizeof(Gauss)); for (m = 0; m < M; m++) { tgmm.gauss[m].mean = dgetmem(L); tgmm.gauss[m].var = dgetmem(L); if (full == 1) { tgmm.gauss[m].cov = (double **) malloc(sizeof(double *) * L); tgmm.gauss[m].inv = (double **) malloc(sizeof(double *) * L); for (l = 0; l < L; l++) { tgmm.gauss[m].cov[l] = dgetmem(L); tgmm.gauss[m].inv[l] = dgetmem(L); } } } logwgd = dgetmem(M); sum = dgetmem(M); /* Read training data */ freadf(dat, sizeof(*dat), T * L, fp); /* Initialization of GMM parameters */ /* LBG */ vaverage(dat, L, T, icb); lbg(dat, L, T, icb, 1, cb, N, ITER, MINTRAIN, S, CENTUP, DELTA, END); for (t = 0, pd = dat; t < T; t++, pd += L) { tindex[t] = vq(pd, cb, L, M); cntcb[tindex[t]]++; } for (m = 0; m < M; m++) if (cntcb[m] == 0) { fprintf(stderr, "Error: No data for mixture No.%d\n", m); usage(1); } fprintf(stderr, "T = %d L = %d M = %d\n", T, L, M); /* flooring value for weights */ W = 1.0 / (double) M *(double) W; /* weights */ for (m = 0, sum_w = 0.0; m < M; m++) { gmm.weight[m] = (double) cntcb[m] / (double) T; if (gmm.weight[m] < W) gmm.weight[m] = W; sum_w += gmm.weight[m]; } if (sum_w != 1.0) for (m = 0; m < M; m++) gmm.weight[m] /= sum_w; /* mean */ for (m = 0, pd = cb; m < M; m++, pd += L) movem(pd, gmm.gauss[m].mean, sizeof(double), L); /* variance */ if (full != 1) { for (t = 0, pd = dat; t < T; t++, pd += L) for (l = 0; l < L; l++) { diff = gmm.gauss[tindex[t]].mean[l] - pd[l]; gmm.gauss[tindex[t]].var[l] += sq(diff); } for (m = 0; m < M; m++) for (l = 0; l < L; l++) { gmm.gauss[m].var[l] /= (double) cntcb[m]; if (gmm.gauss[m].var[l] < V) gmm.gauss[m].var[l] = V; } for (m = 0; m < M; m++) gmm.gauss[m].gconst = cal_gconst(gmm.gauss[m].var, L); } /* full covariance */ else { for (t = 0, pd = dat; t < T; t++, pd += L) { for (l = 0; l < L; l++) { for (i = 0; i <= l; i++) { if (l == i) { diff = (gmm.gauss[tindex[t]].mean[l] - pd[l]) * (gmm.gauss[tindex[t]].mean[i] - pd[i]); floor.gauss[0].cov[l][i] += diff; } } } } for (l = 0; l < L; l++) { for (i = 0; i <= l; i++) { if (l == i) { floor.gauss[0].cov[l][i] /= T; floor.gauss[0].cov[l][i] *= V; } } } for (t = 0, pd = dat; t < T; t++, pd += L) { for (l = 0; l < L; l++) { for (i = 0; i <= l; i++) { diff = (gmm.gauss[tindex[t]].mean[l] - pd[l]) * (gmm.gauss[tindex[t]].mean[i] - pd[i]); gmm.gauss[tindex[t]].cov[l][i] += diff; } } } for (m = 0; m < M; m++) for (l = 0; l < L; l++) for (i = 0; i <= l; i++) { gmm.gauss[m].cov[l][i] /= (double) cntcb[m]; } } /* EM training of GMM parameters */ for (i = 0; (i <= Imax) && ((i <= Imin) || (fabs(change) > E)); i++) { if (full != 1) fillz_gmm(&tgmm, M, L); else fillz_gmmf(&tgmm, M, L); fillz(sum, sizeof(double), M); if (full != 1) { for (m = 0; m < M; m++) gmm.gauss[m].gconst = cal_gconst(gmm.gauss[m].var, L); } else { for (m = 0, n1 = 0; m < M; m++) { gmm.gauss[m].gconst = cal_gconstf(gmm.gauss[m].cov, L); if (gmm.gauss[m].gconst == 0) { n1++; for (l = 0; l < L; l++) gmm.gauss[m].cov[l][l] += floor.gauss[0].cov[l][l]; gmm.gauss[m].gconst = cal_gconstf(gmm.gauss[m].cov, L); } if (gmm.gauss[m].gconst == 0) { fprintf(stderr, "ERROR : Can't caluculate covdet"); exit(EXIT_FAILURE); } /* calculate inv */ cal_inv(gmm.gauss[m].cov, gmm.gauss[m].inv, L); } } if (full == 1) fprintf(stderr, "%d cov can't caluculate covdet\n", n1); for (t = 0, ave_logp1 = 0.0, pd = dat; t < T; t++, pd += L) { for (m = 0, logb = LZERO; m < M; m++) { if (full != 1) { logwgd[m] = log_wgd(&gmm, m, pd, L); logb = log_add(logb, logwgd[m]); } /* full */ else { logwgd[m] = log_wgdf(&gmm, m, pd, L); logb = log_add(logb, logwgd[m]); } } ave_logp1 += logb; for (m = 0; m < M; m++) { tmp1 = exp(logwgd[m] - logb); sum[m] += tmp1; for (l = 0; l < L; l++) { tmp2 = tmp1 * pd[l]; tgmm.gauss[m].mean[l] += tmp2; if (full != 1) tgmm.gauss[m].var[l] += tmp2 * pd[l]; else { for (j = 0; j <= l; j++) { tgmm.gauss[m].cov[l][j] += tmp1 * (pd[l] - gmm.gauss[m].mean[l]) * (pd[j] - gmm. gauss[m].mean [j]); } } } } } /* Output average log likelihood at each iteration */ ave_logp1 /= (double) T; if (i == 1 && m == 1) ave_logp0 = ave_logp1; fprintf(stderr, "iter %3d : ", i); fprintf(stderr, "ave_logprob = %g", ave_logp1); if (i) { change = ave_logp1 - ave_logp0; fprintf(stderr, " change = %g", change); } fprintf(stderr, "\n"); ave_logp0 = ave_logp1; /* Update perameters */ /* weights */ for (m = 0; m < M; m++) gmm.weight[m] = sum[m] / (double) T; /* mean, variance */ for (m = 0; m < M; m++) { for (l = 0; l < L; l++) gmm.gauss[m].mean[l] = tgmm.gauss[m].mean[l] / sum[m]; if (full != 1) { for (l = 0; l < L; l++) { gmm.gauss[m].var[l] = tgmm.gauss[m].var[l] / sum[m] - sq(gmm.gauss[m].mean[l]); if (gmm.gauss[m].var[l] < V) gmm.gauss[m].var[l] = V; } } /* full */ else { for (l = 0; l < L; l++) { for (j = 0; j <= l; j++) { gmm.gauss[m].cov[l][j] = tgmm.gauss[m].cov[l][j] / sum[m]; } } } } } /* Output GMM parameters */ fwritef(gmm.weight, sizeof(double), M, stdout); if (full != 1) { for (m = 0; m < M; m++) { fwritef(gmm.gauss[m].mean, sizeof(double), L, stdout); fwritef(gmm.gauss[m].var, sizeof(double), L, stdout); } } else { for (m = 0; m < M; m++) { fwritef(gmm.gauss[m].mean, sizeof(double), L, stdout); for (i = 0; i < L; i++) for (j = 0; j < i; j++) gmm.gauss[m].cov[j][i] = gmm.gauss[m].cov[i][j]; for (l = 0; l < L; l++) fwritef(gmm.gauss[m].cov[l], sizeof(double), L, stdout); } } return (0); }
int fft(double *x, double *y, const int m) { int j, lmx, li; double *xp, *yp; double *sinp, *cosp; int lf, lix, tblsize; int mv2, mm1; double t1, t2; double arg; int checkm(const int); /************** * RADIX-2 FFT * **************/ if (checkm(m)) return (-1); /*********************** * SIN table generation * ***********************/ if ((_sintbl == 0) || (maxfftsize < m)) { tblsize = m - m / 4 + 1; arg = M_PI / m * 2; if (_sintbl != 0) free(_sintbl); _sintbl = sinp = dgetmem(tblsize); *sinp++ = 0; for (j = 1; j < tblsize; j++) *sinp++ = sin(arg * (double) j); _sintbl[m / 2] = 0; maxfftsize = m; } lf = maxfftsize / m; lmx = m; for (;;) { lix = lmx; lmx /= 2; if (lmx <= 1) break; sinp = _sintbl; cosp = _sintbl + maxfftsize / 4; for (j = 0; j < lmx; j++) { xp = &x[j]; yp = &y[j]; for (li = lix; li <= m; li += lix) { t1 = *(xp) - *(xp + lmx); t2 = *(yp) - *(yp + lmx); *(xp) += *(xp + lmx); *(yp) += *(yp + lmx); *(xp + lmx) = *cosp * t1 + *sinp * t2; *(yp + lmx) = *cosp * t2 - *sinp * t1; xp += lix; yp += lix; } sinp += lf; cosp += lf; } lf += lf; } xp = x; yp = y; for (li = m / 2; li--; xp += 2, yp += 2) { t1 = *(xp) - *(xp + 1); t2 = *(yp) - *(yp + 1); *(xp) += *(xp + 1); *(yp) += *(yp + 1); *(xp + 1) = t1; *(yp + 1) = t2; } /*************** * bit reversal * ***************/ j = 0; xp = x; yp = y; mv2 = m / 2; mm1 = m - 1; for (lmx = 0; lmx < mm1; lmx++) { if ((li = lmx - j) < 0) { t1 = *(xp); t2 = *(yp); *(xp) = *(xp + li); *(yp) = *(yp + li); *(xp + li) = t1; *(yp + li) = t2; } li = mv2; while (li <= j) { j -= li; li /= 2; } j += li; xp = x + j; yp = y + j; } return (0); }
int main(int argc, char *argv[]) { FILE *fp; char *s, *infile = NULL, c; double *x, *y; double *xp, *yp; void trans(double *p); int size2; int i, k; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) { if (*(s = *++argv) == '-') { c = *++s; if ((c == 'l' || c == 'm') && (*++s == '\0')) { s = *++argv; --argc; } switch (c) { case 'l': size = atoi(s); break; case 'm': n1 = atoi(s); if (argc == 1) { n2 = n1; } else { s = *++argv; argc--; if ((*s >= '0') && (*s <= '9')) { n2 = atoi(s); } else { n2 = n1; s = *--argv; argc++; } } break; case 't': case 'c': case 'q': if ((c == 't') || (*++s == 't')) outopt = 1; if ((c == 'c') || (*s == 'c')) outopt = 2; if (c == 'q') outopt = -1; break; case 'a': case 'i': case 'p': case 'r': c -= ('a' - 'A'); case 'A': case 'P': case 'I': case 'R': out = c; break; case 'h': default: usage(); } } else infile = s; } if (n1 > size) { fprintf(stderr, "%s : Region size %d should be less than the FFT size %d!\n", cmnd, n1, size); return (1); } if (n2 > size) { fprintf(stderr, "%s : Region size %d should be less than the FFT size %d!\n", cmnd, n2, size); return (1); } if (infile) fp = getfp(infile, "rb"); else fp = stdin; size2 = size * size; x = dgetmem(2 * size2); y = x + size2; while (!feof(fp)) { if (n1) { for (xp = x, k = n2; --k >= 0; xp += size) { if (freadf(xp, sizeof(*x), n1, fp) != n1) return (-1); if (n1 < size) fillz(xp + n1, sizeof(*x), size - n1); } for (yp = y, k = n2; --k >= 0; yp += size) { if (freadf(yp, sizeof(*y), n1, fp) != n1) return (-1); if (n1 < size) fillz(yp + n1, sizeof(*x), size - n1); } } else { if ((k = freadf(x, sizeof(*x), 2 * size2, fp)) == 0) break; n2 = n1 = sqrt((double) k / 2); if (k != n1 * n1 * 2) { fprintf(stderr, "%s : Region of support is not square!\n", cmnd); return (-1); } if (n1 < size) { fillz(yp = y + size * n1, sizeof(*x), size * (size - n1)); yp -= (size - n1); xp = x + k; for (k = n1; --k >= 0; yp -= (size - n1)) { fillz(yp, sizeof(*x), size - n1); for (i = n1; --i >= 0;) *--yp = *--xp; } fillz(yp = x + size * n1, sizeof(*x), size * (size - n1)); yp -= (size - n1); for (k = n1; --k >= 0; yp -= (size - n1)) { fillz(yp, sizeof(*x), size - n1); for (i = n1; --i >= 0;) *--yp = *--xp; } } } fft2(x, y, size); if (out == 'P') for (k = 0; k < size2; k++) x[k] = x[k] * x[k] + y[k] * y[k]; else if (out == 'A') for (k = 0; k < size2; k++) x[k] = sqrt(x[k] * x[k] + y[k] * y[k]); if (out != 'I') { if (outopt) trans(x); else fwritef(x, sizeof(*x), size2, stdout); } if ((out == ' ') || (out == 'I')) { if (outopt) trans(y); else fwritef(y, sizeof(*y), size2, stdout); } } free(x); return 0; }
/* perform conversion */ int vc(const GMM * gmm, const DELTAWINDOW * window, const size_t total_frame, const size_t source_vlen, const size_t target_vlen, const double *gv_mean, const double *gv_vari, const double *source, double *target) { size_t t, i, j, k, max_num_mix = 0, src_vlen_dyn = source_vlen * window->win_size, tgt_vlen_dyn = target_vlen * window->win_size; int m, l, shift; double max_post_mix = 0.0, logoutp = LZERO, *input = NULL, *src_with_dyn = NULL, *logwgd = NULL, **cov_xx_inv = NULL, ***cov_yx_xx = NULL, *gv_weight = NULL, ***cond_mean = NULL, ***cond_vari = NULL, **cond_post_mix = NULL; HTS_SStreamSet sss; HTS_PStreamSet pss; /* append dynamic feature */ src_with_dyn = dgetmem(total_frame * src_vlen_dyn); for (t = 0; t < total_frame; t++) { for (i = 0; i < window->win_size; i++) { j = window->win_size * source_vlen * t + source_vlen * i; for (shift = window->win_l_width[i]; shift <= window->win_r_width[i]; shift++) { l = t + shift; if (l < 0) { l = 0; } if (!(l < (int) total_frame)) { l = total_frame - 1; } for (k = 0; k < source_vlen; k++) { src_with_dyn[j + k] += window->win_coefficient[i][shift] * source[source_vlen * l + k]; } } } } /* calculate mean and covariace of conditional distribution given source feature and mixture component */ cond_post_mix = ddgetmem(total_frame, gmm->nmix); cond_mean = (double ***) getmem(gmm->nmix, sizeof(*(cond_mean))); for (m = 0; m < gmm->nmix; m++) { cond_mean[m] = ddgetmem(total_frame, tgt_vlen_dyn); } cond_vari = (double ***) getmem(gmm->nmix, sizeof(*(cond_vari))); for (m = 0; m < gmm->nmix; m++) { cond_vari[m] = ddgetmem(tgt_vlen_dyn, tgt_vlen_dyn); } cov_xx_inv = ddgetmem(src_vlen_dyn, src_vlen_dyn); cov_yx_xx = (double ***) getmem(gmm->nmix, sizeof(*(cov_yx_xx))); for (m = 0; m < gmm->nmix; m++) { cov_yx_xx[m] = ddgetmem(tgt_vlen_dyn, src_vlen_dyn); } for (m = 0; m < gmm->nmix; m++) { invert(gmm->gauss[m].cov, cov_xx_inv, src_vlen_dyn); for (i = 0; i < tgt_vlen_dyn; i++) { for (j = 0; j < src_vlen_dyn; j++) { for (k = 0; k < src_vlen_dyn; k++) { cov_yx_xx[m][i][j] += gmm->gauss[m].cov[src_vlen_dyn + i][k] * cov_xx_inv[k][j]; } } } } logwgd = dgetmem(gmm->nmix); input = dgetmem(src_vlen_dyn); for (t = 0; t < total_frame; t++) { for (i = 0; i < src_vlen_dyn; i++) { input[i] = src_with_dyn[t * src_vlen_dyn + i]; } for (m = 0, logoutp = LZERO; m < gmm->nmix; m++) { logwgd[m] = log_wgd(gmm, m, src_vlen_dyn, input); logoutp = log_add(logoutp, logwgd[m]); } for (m = 0; m < gmm->nmix; m++) { /* posterior probability of mixture component given source feature */ cond_post_mix[t][m] = exp(logwgd[m] - logoutp); for (i = 0; i < tgt_vlen_dyn; i++) { for (j = 0; j < src_vlen_dyn; j++) { cond_mean[m][t][i] += cov_yx_xx[m][i][j] * (input[j] - gmm->gauss[m].mean[j]); } cond_mean[m][t][i] += gmm->gauss[m].mean[src_vlen_dyn + i]; } } } for (m = 0; m < gmm->nmix; m++) { for (i = 0; i < tgt_vlen_dyn; i++) { for (j = 0; j < tgt_vlen_dyn; j++) { for (k = 0; k < src_vlen_dyn; k++) { cond_vari[m][i][j] += cov_yx_xx[m][i][k] * gmm->gauss[m].cov[k][src_vlen_dyn + j]; } cond_vari[m][i][j] = gmm->gauss[m].cov[src_vlen_dyn + i][src_vlen_dyn + j] - cond_vari[m][i][j]; } } } /* initialize parameter set of hts_engine */ HTS_PStreamSet_initialize(&pss); sss.nstream = 1; sss.total_state = total_frame; sss.total_frame = total_frame; sss.duration = (size_t *) getmem(total_frame, sizeof(size_t)); for (i = 0; i < total_frame; i++) { sss.duration[i] = 1; } sss.sstream = (HTS_SStream *) getmem(1, sizeof(HTS_SStream)); sss.sstream->vector_length = target_vlen; sss.sstream->mean = (double **) getmem(sss.total_state, sizeof(*(sss.sstream->mean))); sss.sstream->vari = (double **) getmem(sss.total_state, sizeof(*(sss.sstream->vari))); for (i = 0; i < sss.total_state; i++) { sss.sstream->mean[i] = dgetmem(tgt_vlen_dyn); sss.sstream->vari[i] = dgetmem(tgt_vlen_dyn); } sss.sstream->msd = NULL; /* no MSD */ sss.sstream->win_size = window->win_size; sss.sstream->win_l_width = (int *) getmem(window->win_size, sizeof(*(sss.sstream->win_l_width))); sss.sstream->win_r_width = (int *) getmem(window->win_size, sizeof(*(sss.sstream->win_r_width))); sss.sstream->win_coefficient = (double **) getmem(window->win_size, sizeof(*(sss.sstream->win_coefficient))); for (i = 0; i < window->win_size; i++) { sss.sstream->win_l_width[i] = window->win_l_width[i]; sss.sstream->win_r_width[i] = window->win_r_width[i]; if (sss.sstream->win_l_width[i] + sss.sstream->win_r_width[i] == 0) { sss.sstream->win_coefficient[i] = dgetmem(-2 * sss.sstream->win_l_width[i] + 1); } else { sss.sstream->win_coefficient[i] = dgetmem(-2 * sss.sstream->win_l_width[i]); } sss.sstream->win_coefficient[i] -= sss.sstream->win_l_width[i]; for (shift = sss.sstream->win_l_width[i]; shift <= sss.sstream->win_r_width[i]; shift++) { sss.sstream->win_coefficient[i][shift] = window->win_coefficient[i][shift]; } } sss.sstream->win_max_width = window->win_max_width; if ((gv_mean != NULL) && (gv_vari != NULL)) { /* set GV parameters */ sss.sstream->gv_mean = dgetmem(sss.sstream->vector_length); sss.sstream->gv_vari = dgetmem(sss.sstream->vector_length); for (i = 0; i < sss.sstream->vector_length; i++) { sss.sstream->gv_mean[i] = gv_mean[i]; sss.sstream->gv_vari[i] = gv_vari[i]; } } else { sss.sstream->gv_mean = NULL; sss.sstream->gv_vari = NULL; } sss.sstream->gv_switch = (HTS_Boolean *) getmem(total_frame, sizeof(HTS_Boolean)); for (i = 0; i < total_frame; i++) { sss.sstream->gv_switch[i] = TRUE; } gv_weight = dgetmem(tgt_vlen_dyn); for (i = 0; i < tgt_vlen_dyn; i++) { gv_weight[i] = 1.0; } /* initialize pdf sequence */ for (t = 0; t < total_frame; t++) { max_post_mix = cond_post_mix[t][0]; max_num_mix = 0; for (m = 1; m < gmm->nmix; m++) { if (max_post_mix < cond_post_mix[t][m]) { max_post_mix = cond_post_mix[t][m]; max_num_mix = m; } } for (i = 0; i < tgt_vlen_dyn; i++) { sss.sstream->mean[t][i] = cond_mean[max_num_mix][t][i]; sss.sstream->vari[t][i] = cond_vari[max_num_mix][i][i]; } } /* parameter generation by hts_engine API */ HTS_PStreamSet_create(&pss, &sss, NULL, gv_weight); for (t = 0; t < total_frame; t++) { k = t * target_vlen; for (i = 0; i < target_vlen; i++) { target[k + i] = pss.pstream->par[t][i]; } } /* release memory */ free(src_with_dyn); free(input); free(logwgd); free(cov_xx_inv[0]); free(cov_xx_inv); for (m = 0; m < gmm->nmix; m++) { free(cov_yx_xx[m][0]); free(cov_yx_xx[m]); free(cond_mean[m][0]); free(cond_mean[m]); free(cond_vari[m][0]); free(cond_vari[m]); } free(cov_yx_xx); free(cond_mean); free(cond_vari); free(cond_post_mix[0]); free(cond_post_mix); free(gv_weight); HTS_PStreamSet_clear(&pss); HTS_SStreamSet_clear(&sss); return (0); }
int main(int argc, char **argv) { int m = ORDER, l = LENG, otype = OTYPE, no, i; double alpha = ALPHA, gamma = GAMMA, *c, *x, *y, logk; Boolean norm = NORM, phase = PHASE, mulg = MULG; FILE *fp = stdin; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'm': m = atoi(*++argv); --argc; break; case 'a': alpha = atof(*++argv); --argc; break; case 'g': gamma = atof(*++argv); --argc; break; case 'c': gamma = atoi(*++argv); --argc; if (gamma < 1) fprintf(stderr, "%s : value of c must be c>=1!\n", cmnd); gamma = -1.0 / gamma; break; case 'n': norm = 1 - norm; break; case 'u': mulg = 1 - mulg; break; case 'l': l = atoi(*++argv); --argc; break; case 'o': otype = atoi(*++argv); --argc; break; case 'p': phase = 1 - phase; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else fp = getfp(*argv, "rb"); x = dgetmem(l + l + m + 1); y = x + l; c = y + l; no = l / 2 + 1; logk = 20.0 / log(10.0); while (freadf(c, sizeof(*c), m + 1, fp) == m + 1) { if (norm) ignorm(c, c, m, gamma); else if (mulg) { if (gamma == 0) { fprintf(stderr, "%s : gamma for input mgc coefficients should not equal to 0 if you specify -u option!\n", cmnd); usage(1); } c[0] = (c[0] - 1.0) / gamma; } if (mulg) { if (gamma == 0) { fprintf(stderr, "%s : gamma for input mgc coefficients should not equal to 0 if you specify -u option!\n", cmnd); usage(1); } for (i = m; i > 0; i--) c[i] /= gamma; } mgc2sp(c, m, alpha, gamma, x, y, l); if (phase) switch (otype) { case 1: for (i = no; i--;) x[i] = y[i]; break; case 2: for (i = no; i--;) x[i] = y[i] * 180 / PI; break; default: for (i = no; i--;) x[i] = y[i] / PI; break; } else switch (otype) { case 1: break; case 2: for (i = no; i--;) x[i] = exp(x[i]); break; case 3: for (i = no; i--;) x[i] = exp(2 * x[i]); break; default: for (i = no; i--;) x[i] *= logk; break; } fwritef(x, sizeof(*x), no, stdout); } return (0); }
int main(int argc, char **argv) { int m = ORDER, itype = ITYPE, i, gain = GAIN; FILE *fp = stdin; double *a, *lsp, sampling = SAMPLING; Boolean loggain = LOGGAIN; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'm': m = atoi(*++argv); --argc; break; case 's': sampling = atof(*++argv); --argc; break; case 'k': gain = 0; break; case 'l': case 'L': loggain = TR; break; case 'i': case 'q': itype = atoi(*++argv); --argc; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else fp = getfp(*argv, "rb"); lsp = dgetmem(m + m + 1 + gain); a = lsp + m + gain; while (freadf(lsp, sizeof(*lsp), m + gain, fp) == m + gain) { if (itype == 0) for (i = gain; i < m + gain; i++) lsp[i] /= PI2; else if (itype == 2 || itype == 3) for (i = gain; i < m + gain; i++) lsp[i] /= sampling; if (itype == 3) for (i = gain; i < m + gain; i++) lsp[i] /= 1000; lsp2lpc(lsp + gain, a, m); if (gain) { if (loggain) *lsp = exp(*lsp); fwritef(lsp, sizeof(*lsp), 1, stdout); } fwritef(a + gain, sizeof(*a), m + 1 - gain, stdout); } return (0); }
int main(int argc, char **argv) { int leng = LENG, m = ORDER, start = 0, end = 0, otype = OTYPE, i, no; FILE *fp = stdin; char phase = PHASE; double logk, *x, *y, *c; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) if (**++argv == '-') { switch (*(*argv + 1)) { case 'm': m = atoi(*++argv); --argc; break; case 'l': leng = atoi(*++argv); --argc; break; case 'p': phase = 1 - phase; break; case 'o': otype = atoi(*++argv); --argc; break; case 's': start = atoi(*++argv); --argc; break; case 'e': end = atoi(*++argv); --argc; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else fp = getfp(*argv, "rb"); if (end == 0) end = m; x = dgetmem(leng * 2); y = x + leng; c = dgetmem(m + 1); no = leng / 2 + 1; logk = 20.0 / log(10.0); while (freadf(c, sizeof(*c), m + 1, fp) == m + 1) { fillz(c, sizeof(*c), start); for (i = end + 1; i <= m; i++) c[i] = 0.0; c2sp(c, m, x, y, leng); if (phase) switch (otype) { case 1: for (i = no; i--;) x[i] = y[i]; break; case 2: for (i = no; i--;) x[i] = y[i] * 180 / PI; break; default: for (i = no; i--;) x[i] = y[i] / PI; break; } else switch (otype) { case 1: break; case 2: for (i = no; i--;) x[i] = exp(x[i]); break; default: for (i = no; i--;) x[i] *= logk; break; } fwritef(x, sizeof(*x), no, stdout); } return (0); }
int main(int argc, char **argv) { int l = LENG, fprd = FPERIOD, ns, i, rnum, ts, cs; FILE *fp = stdin; Boolean noctr = NOCTR; double *x, *xx, *p1, *p2, *p; char *s, c; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) if (*(s = *++argv) == '-') { c = *++s; switch (c) { case 'l': l = atoi(*++argv); --argc; break; case 'p': fprd = atoi(*++argv); --argc; break; case 'n': noctr = 1 - noctr; break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1)); usage(1); } } else fp = getfp(*argv, "rb"); x = dgetmem(l); if (!noctr) { i = (int) ((l + 1) / 2); rnum = freadf(&x[(int) (l / 2)], sizeof(*x), i, fp); } else rnum = freadf(x, sizeof(*x), l, fp); if (rnum == 0) return 0; cs = rnum; fwritef(x, sizeof(*x), l, stdout); if ((ns = (l - fprd)) > 0) { p = &x[fprd]; for (;;) { p1 = x; p2 = p; i = ns; while (i--) { *p1++ = *p2++; } rnum = freadf(p1, sizeof(*p1), fprd, fp); if (rnum < fprd) { ts = fprd - rnum; cs -= ts; while (rnum--) p1++; while (ts--) *p1++ = 0.0; } if (cs <= 0) break; fwritef(x, sizeof(*x), l, stdout); } } else { i = -ns; xx = dgetmem(i); for (;;) { if (freadf(xx, sizeof(*xx), i, fp) != i) break; rnum = freadf(x, sizeof(*x), l, fp); if (rnum < l) { if (rnum == 0) break; ts = l - rnum; p1 = x; while (rnum--) p1++; while (ts--) *p1++ = 0.0; } fwritef(x, sizeof(*x), l, stdout); } } return 0; }