📄 hadapt.c
字号:
n->HTrans = NULL; /* check if fixed or global adaptation */ if (fixed) { n->nBases = 1; n->nodeIndex = 1; n->bases = CreateShortVec(rt->hmem, n->nBases); }}/* Allocate space for a baseform (regression class) structure */static BaseformAcc *CreateBaseform(RegTransInfo *rt) { BaseformAcc *b; int vSize; vSize = rt->vSize; b = (BaseformAcc *) New(rt->hmem, sizeof(BaseformAcc)); b->nComponents = 0; b->baseformIndex = 0; b->speechFlag = TRUE; b->occ = 0.0; b->H = NULL; b->Z = NULL; b->G = NULL; b->pdfList = NULL; return(b);}/* Allocate space for a baseform (regression class) accumulators G, Z and H */static void CreateBaseformAccStorage(BaseformAcc *b, RegTransInfo *rt){ int j, vSize; OffsetBMat *m; OffsetTriBMat *tm; vSize = rt->vSize; b->H = CreateVector(rt->hmem, vSize); ZeroVector(b->H); m = (OffsetBMat *) New(rt->hmem, sizeof(OffsetBMat)); m->a = CreateBlockMat(rt->hmem, vSize, rt->nBlocks); m->b = CreateVector(rt->hmem, vSize); ZeroBlockMat(m->a); ZeroVector(m->b); b->Z = m; b->G = (OffsetTriBMat **) New(rt->hmem, sizeof(OffsetTriBMat *)*vSize); --(b->G); for (j = 1; j <= vSize; j++) { tm = (OffsetTriBMat *) New(rt->hmem, sizeof(OffsetTriBMat)); tm->a = CreateBlockTriMat(rt->hmem, vSize, rt->nBlocks); tm->b = CreateVector(rt->hmem, vSize+1); ZeroBlockTriMat(tm->a); ZeroVector(tm->b); b->G[j] = tm; }}/* allocate memory for the transforms that are stored at the nodes */static void AllocNodeTransforms(RegTree *t, RegTransInfo *rt){ if (t != NULL) { AllocNodeTransforms(t->left, rt); CreateNodeInfo(t->nodeInfo, rt, FALSE); AllocNodeTransforms(t->right, rt); }}/*------------------------------------------------------------------------*//* General purpose functions *//*------------------------------------------------------------------------*//* add symmetric OffsetTriBMat G to Matrix m2 (lower triangle!) */static void AddGSymMatrix(OffsetTriBMat *G, Matrix m2) { int i, j, k, fk, nBlocks, bSize, bStart; int m; Vector v1, v2; Vector b; TriMat m1; m = NumRows(m2); b = G->b; nBlocks = GetTriMatBlockSize(G->a); bSize = (m-1)/nBlocks; if (bSize * nBlocks != m-1) HError(7460, "AddGMatrix: Number of blocks incompatible with vector size!"); /* add the offset */ for (i = 1; i <= m; i++) m2[i][1] += b[i]; /* add the matrix */ bStart = 0; for (i = 1; i <= nBlocks; i++) { m1 = G->a[i]; for (j = 1; j <= bSize; j++) { v1 = m1[j]; v2 = m2[bStart+j+1]; for (k = 1; k <= j; k++) { fk = bStart+k+1; v2[fk] += v1[k]; } } bStart+=bSize; }}/* add OffsetBMat Z to Matrix m2 */static void AddZMatrix(OffsetBMat *Z, Matrix m2) { int i, j, k, fk, nBlocks, bSize, bStart; int m; Vector v1, v2; Vector b; Matrix m1; b = Z->b; nBlocks = GetMatBlockSize(Z->a); m = NumRows(m2); bSize = m/nBlocks; if (bSize * nBlocks != m) HError(7460, "AddZMatrix: Number of blocks incompatible with vector size!"); /* add the offset */ for (i = 1; i <= m; i++) m2[i][1] += b[i]; /* add the matrix */ bStart = 0; for (i = 1; i <= nBlocks; i++) { m1 = Z->a[i]; for (j = 1; j <= bSize; j++) { v1 = m1[j]; v2 = m2[bStart+j]; for (k = 1; k <= bSize; k++) { fk = bStart+k+1; v2[fk] += v1[k]; } } bStart+=bSize; }}/* Convert a standard matrix into a block diagonal matrix */static void ConvertMat2BlockMat(Matrix W, OffsetBMat *M, int vSize, int nBlocks) { int i, j, fi, fj, n, bStart, bSize; BlockMatrix a; Matrix m; a = M->a; bSize = vSize/nBlocks; bStart = 0; for (n = 1; n <= nBlocks; n++) { m = a[n]; for (i = 1; i<= bSize; i++) { fi = i+bStart; for (j = 1; j<= bSize; j++) { fj = j+bStart; m[i][j] = W[fi][fj]; } } bStart += bSize; } }/* return the regression accumulator hook (on the mixture PDF) used commonly by many functions in this module */static RegAcc *GetRegAcc(MixPDF *mp){ return ((RegAcc *) mp->hook);}/* removes and frees a mean transform structure */static void RemoveMeanTransform(MemHeap *x, OffsetBMat *m) { FreeBlockMat(x, m->a); FreeVector(x, m->b); Dispose(x, m);}/* function that frees up unsued transforms */static void RemoveUnusedTransforms(RegTree *t, RegTransInfo *rt) { RegNode *n, *n1, *n2; if (t->left != NULL) { RemoveUnusedTransforms(t->left, rt); n = t->nodeInfo; n1 = t->left->nodeInfo; n2 = t->right->nodeInfo; if (n->WTrans != NULL && t != rt->rtree) { if (n1->WTrans != NULL && n2->WTrans != NULL) { RemoveMeanTransform(&rt->transMem, n->WTrans); n->WTrans = NULL; Dispose(&rt->transMem, n->HTrans); n->HTrans = NULL; } } RemoveUnusedTransforms(t->right, rt); }}/* function that frees up ALL transforms */static void RemoveAllTransforms(RegTree *t, RegTransInfo *rt) { RegNode *n; if (t != NULL) { RemoveAllTransforms(t->left, rt); n = t->nodeInfo; if (n->WTrans != NULL) { RemoveMeanTransform(&rt->transMem, n->WTrans); n->WTrans = NULL; Dispose(&rt->transMem, n->HTrans); n->HTrans = NULL; } RemoveAllTransforms(t->right, rt); }}/* Print the regression tree -- Used by tracing functions */static void PrintTree(RegTree *t, RegTransInfo *rt){ short idx; int i; if (t != NULL) { PrintTree(t->left, rt); if (t->left == NULL) { idx = t->nodeInfo->bases[1]; printf("%d : Terminal (%d, %f)\n", t->nodeInfo->nodeIndex, rt->baseforms[idx]->nComponents, t->nodeInfo->nodeOcc); } else { printf("%d : { %d %d } (%f)\n", t->nodeInfo->nodeIndex, t->left->nodeInfo->nodeIndex, t->right->nodeInfo->nodeIndex, t->nodeInfo->nodeOcc); printf("Bases are "); for (i = 1; i <= t->nodeInfo->nBases; i++) { idx = t->nodeInfo->bases[i]; printf("%d ", rt->baseforms[idx]->baseformIndex); } printf("\n"); fflush(stdout); } PrintTree(t->right, rt); } }/* Find a node in the regression tree given the index and return the node */static RegTree *FindNode(RegTree *t, RegTree *r, int i){ if (t != NULL) { r = FindNode(t->left, r, i); if (t->nodeInfo->nodeIndex == i) return t; r = FindNode(t->right, r, i); } return r;}/* find the number of terminals in the tree (returned in nTerms) */static void FindNumTerminals(RegTree *t, int *nTerms) { if (t != NULL) { FindNumTerminals(t->left, nTerms); if (t->left == NULL) *nTerms += 1; FindNumTerminals(t->right, nTerms); }}/* Given a base class number (terminal node index), find the baseforms index number */static int FindClassIndex(RegTransInfo *rt, int base) { int i; if (base == 0) HError(7430, "FindClassIndex: Regression class is zero;\nNeed to run HHEd to build a regression class tree before running adaptation!"); for (i = 1; i <= rt->nBaseTransforms; i++) if (rt->baseforms[i]->baseformIndex == base) break; if (i > rt->nBaseTransforms) HError(7430, "FindClassIndex: Can't find base form class %d\n", base); return i;}/* Update the node occupation count */static void GetNodeOcc(RegTree *t, RegTransInfo *rt) { short idx, i; RegNode *n; if (t != NULL) { GetNodeOcc(t->left, rt); n = t->nodeInfo; n->nodeOcc = 0.0; for (i = 1; i <= n->nBases; i++) { idx = n->bases[i]; if (rt->baseforms[idx] == NULL) HError(7430, "GetNodeOcc:Can't find base class %d for node %d", i, n->nodeIndex); n->nodeOcc += rt->baseforms[idx]->occ; } if (trace & T_DET) { printf("Occupation count for node %d is %f\n", n->nodeIndex, n->nodeOcc); fflush(stdout); } GetNodeOcc(t->right, rt); }}/* Count of the number mixture components present at each node */static void GetNodeComps(RegTree *t, RegTransInfo *rt) { short idx, i; RegNode *n; if (t != NULL) { GetNodeComps(t->left, rt); n = t->nodeInfo; n->nodeComps = 0; for (i = 1; i <= n->nBases; i++) { idx = n->bases[i]; if (rt->baseforms[idx] == NULL) HError(7430, "GetNodeComps:Can't find base class %d for node %d", i, n->nodeIndex); n->nodeComps += rt->baseforms[idx]->nComponents; } if (trace & T_DET) { printf("Mixture component count for node %d is %d\n", n->nodeIndex, n->nodeComps); fflush(stdout); } GetNodeComps(t->right, rt); }}/* General function to check a node's occupation, to see if a transform is available or whether one needs constructing */static Boolean CheckNodeOcc(RegTree *left, RegTree *right, RegTransInfo *rt){ if (left == NULL) { if (right->nodeInfo->nodeOcc < rt->nodeOccThresh || right->nodeInfo->nodeComps <= rt->vSize) return TRUE; else return FALSE; } if (right == NULL) { if (left->nodeInfo->nodeOcc < rt->nodeOccThresh || left->nodeInfo->nodeComps <= rt->vSize) return TRUE; else return FALSE; } if (left->nodeInfo->nodeOcc < rt->nodeOccThresh || right->nodeInfo->nodeOcc < rt->nodeOccThresh || left->nodeInfo->nodeComps <= rt->vSize || right->nodeInfo->nodeComps <= rt->vSize) return TRUE; else return FALSE;}/* ----------------------------------------------------------------------*//* Auxilliary Calculation Functions *//* ----------------------------------------------------------------------*//* note all auxilliary function calculation is used for tracing only *//* Calculate the auxilliary function for a regression class */static float CalcRegClassAuxilliary(BaseformAcc *base, float *frames) { int i, k, n, vSize; MixPDF *mp=NULL; RegAcc *ra=NULL; float sum = 0.0; Vector mean; Vector var; float gconst; float ivar; vSize = VectorSize(base->pdfList[1]->mean); n = base->nComponents; for (i = 1; i <= n; i++) { mp = base->pdfList[i]; ra = GetRegAcc(mp); mean = mp->mean; gconst = mp->gConst; var = mp->cov.var; if (ra->occ > MINOCC) { for(k = 1; k <= vSize; k++) { if (mp->ckind == INVDIAGC) ivar = var[k];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -