⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hadapt.c

📁 隐马尔科夫模型工具箱
💻 C
📖 第 1 页 / 共 5 页
字号:
/* ----------------------------------------------------------- *//*                                                             *//*                          ___                                *//*                       |_| | |_/   SPEECH                    *//*                       | | | | \   RECOGNITION               *//*                       =========   SOFTWARE                  */ /*                                                             *//*                                                             *//* ----------------------------------------------------------- *//*         Copyright: Microsoft Corporation                    *//*          1995-2000 Redmond, Washington USA                  *//*                    http://www.microsoft.com                 *//*                                                             *//*   Use of this software is governed by a License Agreement   *//*    ** See the file License for the Conditions of Use  **    *//*    **     This banner notice must not be removed      **    *//*                                                             *//* ----------------------------------------------------------- *//*         File: HAdapt.c      Adaptation Library module       *//* ----------------------------------------------------------- */char *hadapt_version = "!HVER!HAdapt:   3.2 [CUED 09/12/02]";char *hadapt_vc_id =  "$Id: HAdapt.c,v 1.9 2002/12/19 16:37:11 ge204 Exp $";#include <stdio.h>      /* Standard C Libraries */#include <stdlib.h>#include <string.h>#include <math.h>#include <ctype.h>#include "HShell.h"     /* HMM ToolKit Modules */#include "HMem.h"#include "HMath.h"#include "HSigP.h"#include "HWave.h"#include "HAudio.h"#include "HParm.h"#include "HLabel.h"#include "HModel.h"#include "HTrain.h"#include "HUtil.h"#include "HAdapt.h"/* trace flags */#define T_TOP   00001    /* Top level tracing */#define T_NAC   00002    /* trace on accumulates */#define T_TRA   00004    /* trace on transformations */#define T_AUX   00010    /* output the auxilliary function */#define T_RIO   00020    /* regression classes input output */#define T_USE   00040    /* regression classes tree usage */#define T_DET   00200    /* detailed trace for class level accumulates */#define T_MEM   00400    /* trace to print heap stats */#define MINOCC        1E-08#define OCC_THRESH    700.0static int trace = 0;               /* trace info */static Boolean initVar=FALSE;       /* use a variance transform */static Boolean initAdptSil=TRUE;    /* adapt the silence */static Boolean writeBin=FALSE;      /* write file in binary format */static int blocks = DEF_BLOCKSIZE;  /* number of blocks initialisation */static RegClassType regClass=DEF_REGCLASS; /* regClass type to be used */static float occThreshInit=OCC_THRESH;   /* tree ocupation count minimum */static char *undefined="Undefined"; /* undefined tmf header field variable *//* matrices and vectors for SVD */static DMatrix svdMat;             /* general double SVD matrix */static DMatrix u, v;               /* temporary matrices for SVD */static DVector w;                  /* temporary vector for SVD */static DMatrix svdMat1;            /* general double SVD matrix for btrans */static DMatrix u1, v1;             /* temp matrices for SVD for backtrans*/static DVector w1;                 /* temp vector for SVD for backtrans *//* matrices and vectors for accumulating regression   statistics at the base class level. */static Matrix *Gg;static Matrix Zg;static Vector Hg;static Matrix Wg;static DMatrix blkZ;static DMatrix blkG;static DMatrix blku;static DMatrix blkv;static DVector blkw;ConfParam *cParm[MAXGLOBS];      /* config parameters */int nParm = 0;/* ----------------------------------------------------------------------*//*                 Block Diagonal Matrix Functions                       *//* ----------------------------------------------------------------------*//* --------------- Sizing funtions ---------------------- *//* Return number of blocks in a block matrix */static int GetMatBlockSize(BlockMatrix B){   return ((int) B[0]);}/* Return number of blocks in a block double matrix */static int GetDMatBlockSize(BlockDMatrix B){   return ((int) B[0]);}/* Return number of blocks in a block tri diagonal matrix */static int GetTriMatBlockSize(BlockTriMatrix B){   return ((int) B[0]);}/*------------- Create the block matrix structures --------------------*//* Allocate space for a block matrtix    B[1..nBlocks][1..blockSize][1..blockSize] */static BlockMatrix CreateBlockMat(MemHeap *x, int vSize, int nBlocks) {   int blockSize = 0;   BlockMatrix B;   int i;   /* firstly do check */   blockSize = vSize / nBlocks;   if (blockSize * nBlocks != vSize)      HError(7460, "CreateBlockMat: Number of blocks incompatible with vector size!");     B = (BlockMatrix) New(x, (nBlocks+1)*sizeof(Matrix));   B[0] = (Matrix) nBlocks;   for (i = 1; i <= nBlocks; i++)      B[i] = CreateMatrix(x, blockSize, blockSize);     return B;}/* Allocate space for blockdouble matrix    B[1..nBlocks][1..blockSize][1..blockSize] */static BlockDMatrix CreateBlockDMat(MemHeap *x, int vSize, int nBlocks) {   int blockSize = 0;   BlockDMatrix B;   int i;   /* firstly do check */   blockSize = vSize / nBlocks;   if (blockSize * nBlocks != vSize)      HError(7460, "CreateBlockDMat: Number of blocks incompatible with vector size!");     B = (BlockDMatrix) New(x, (nBlocks+1)*sizeof(DMatrix));   B[0] = (DMatrix) nBlocks;   for (i = 1; i <= nBlocks; i++)      B[i] = CreateDMatrix(x, blockSize, blockSize);     return B;}/* Allocate space for tridiagonal matrix    B[1..nBlocks][1..blockSize][1..blockSize] */static BlockTriMatrix CreateBlockTriMat(MemHeap *x, int vSize, int nBlocks) {   int blockSize = 0;   BlockTriMatrix B;   int i;   /* firstly do check */   blockSize = vSize / nBlocks;   if (blockSize * nBlocks != vSize)      HError(7460, "CreateBlockTriMat: Number of blocks incompatible with vector size!");     B = (BlockTriMatrix) New(x, (nBlocks+1)*sizeof(TriMat));   B[0] = (TriMat) nBlocks;   for (i = 1; i <= nBlocks; i++)      B[i] = CreateTriMat(x, blockSize);     return B;}/* ----------- Free / delete block matrices ------------- */ /* Free memory allocated for a block matrix */static void FreeBlockMat(MemHeap *x, BlockMatrix B){     int i, nBlocks;   nBlocks = GetMatBlockSize(B);   for (i = 1; i <= nBlocks; i++)      FreeMatrix(x, B[i]);   Dispose(x, B);}/* Free memory  allocated for block double matrix */static void FreeBlockDMat(MemHeap *x, BlockDMatrix B){   int i, nBlocks;   nBlocks = GetDMatBlockSize(B);   for (i = 1; i <= nBlocks; i++)      FreeDMatrix(x, B[i]);   Dispose(x, B);}/* Free memory  allocated for block tridiagonal matrix */static void FreeBlockTriMat(MemHeap *x, BlockTriMatrix B){   int i, nBlocks;   nBlocks = GetTriMatBlockSize(B);   for (i = 1; i <= nBlocks; i++)      FreeTriMat(x, B[i]);   Dispose(x, B);}/* ------------- Zero block matrices ------------- *//*  Zero a block matrix */static void ZeroBlockMat(BlockMatrix B) {   int i, nBlocks;   nBlocks = GetMatBlockSize(B);   for (i = 1; i <= nBlocks; i++)      ZeroMatrix(B[i]);  }/* Zero a block double matrix */static void ZeroBlockDMat(BlockDMatrix B) {   int i, nBlocks;   nBlocks = GetDMatBlockSize(B);   for (i = 1; i <= nBlocks; i++)      ZeroDMatrix(B[i]);  }/* Zero a block tri-diagonal matrix */static void ZeroBlockTriMat(BlockTriMatrix B) {   int i, nBlocks;   nBlocks = GetTriMatBlockSize(B);   for (i = 1; i <= nBlocks; i++)      ZeroTriMat(B[i]);  }/* -------- Arithmetic functions with block matrices ------ *//* Multiply  a vector by a block matrix */ static void MultBlockMat_Vec(BlockMatrix B, Vector in, Vector out) {   int vSize, bSize, nBlocks;   int i, j, k, fj, fk, blockStart;   Matrix m;   Vector v;   /* do check */   vSize = VectorSize(in);   nBlocks = GetMatBlockSize(B);   bSize = 1;   if (nBlocks >= 1)      bSize = NumRows(B[1]);   else      HError(7460, "MultBlockMat_Vec: Block matrix has less than 1 block!");   if (bSize * nBlocks != vSize)      HError(7460, "MultBlockMat_Vec: Incompatible multiplication %d blocks, %d block size, %d full vector size\n", nBlocks, bSize, vSize);     /* allocate space for temporary vector v */   v = CreateVector(&gstack, vSize);   ZeroVector(v);   blockStart = 0;   for (i = 1; i <= nBlocks; i++) {      m = B[i];      for (j = 1; j <= bSize; j++) {         fj = j + blockStart;         for (k = 1; k <= bSize; k++) {            fk = k + blockStart;            v[fj] += m[j][k] * in[fk];         }      }      blockStart += bSize;   }   CopyVector(v, out);   FreeVector(&gstack, v);}/* Multiply a block matrix by a block matrix */ static void MultBlockMat_BlockMat(BlockMatrix B1, BlockMatrix B2,                                   BlockMatrix out) {   int vSize, bSize, nBlocks;   int n, i, j, k;   BlockMatrix m;   /* do check */   nBlocks = GetMatBlockSize(B1);   bSize = NumRows(B1[1]);   vSize = nBlocks * bSize;   /* allocate space for temporary vector v */   m = CreateBlockMat(&gstack, vSize, nBlocks);   ZeroBlockMat(m);   for (n = 1; n <= nBlocks; n++) {      for (i = 1; i <= bSize; i++)         for (j = 1; j <= bSize; j++)            for (k = 1; k <= bSize; k++)               m[n][i][j] += B1[n][i][k] * B2[n][k][j];      for (i = 1; i <= bSize; i++)         for (j = 1; j <= bSize; j++)            out[n][i][j] = m[n][i][j];   }     FreeBlockMat(&gstack, m);}/* Multiply a vector by a block double matrix */ static void MultBlockDMat_Vec(BlockDMatrix B, Vector in, Vector out) {   int vSize, bSize, nBlocks;   int i, j, k, fj, fk, blockStart;   DMatrix m;   Vector v;   bSize = 1;   /* do check */   vSize = VectorSize(in);   nBlocks = GetDMatBlockSize(B);   if (nBlocks >= 1)      bSize = NumDRows(B[1]);   else      HError(7460, "MultBlockDMat_Vec: Block matrix has less than 1 block!");   if (bSize * nBlocks != vSize)      HError(7460, "MultBlockDMat_Vec: Incompatible multiplication %d blocks, %d block size, %d full vector size\n", nBlocks, bSize, vSize);     /* allocate space for temporary vector v */   v = CreateVector(&gstack, vSize);   ZeroVector(v);   blockStart = 0;   for (i = 1; i <= nBlocks; i++) {      m = B[i];      for (j = 1; j <= bSize; j++) {         fj = j + blockStart;         for (k = 1; k <= bSize; k++) {            fk = k + blockStart;            v[fj] += m[j][k] * in[fk];         }      }      blockStart += bSize;   }   CopyVector(v, out);   FreeVector(&gstack, v);}/*------------------------------------------------------------------------*//*            Initialisations and general structures allocation           *//*------------------------------------------------------------------------*//* EXPORT->InitAdapt: initialise configuration parameters */void InitAdapt (void) {   int i;   Boolean b;   double d;   Register(hadapt_version,hadapt_vc_id);   nParm = GetConfig("HADAPT", TRUE, cParm, MAXGLOBS);     if (nParm>0){      if (GetConfInt(cParm,nParm,"TRACE",&i)) trace = i;      if (GetConfBool(cParm,nParm,"USEVAR",&b)) initVar = b;      if (GetConfBool(cParm,nParm,"ADPTSIL",&b)) initAdptSil = b;      if (GetConfInt(cParm,nParm,"BLOCKS",&i)) blocks = i;      if (GetConfBool(cParm,nParm,"SAVEBINARY",&b)) writeBin = b;      if (GetConfFlt(cParm,nParm,"OCCTHRESH",&d)) occThreshInit = (float) d;   }}/* Create space for the mean transformation matrix and offset vector */static OffsetBMat *CreateMeanTransform(MemHeap *x, int size, int nBlocks) {   OffsetBMat *m;     m = (OffsetBMat *) New(x, sizeof(OffsetBMat));     m->a = CreateBlockMat(x, size, nBlocks);   m->b = CreateVector(x, size);   ZeroBlockMat(m->a);   ZeroVector(m->b);     return m;  }/* create space for a transformation */static void CreateNodeTransform(RegNode *n, MemHeap *x,                                 RegTransType transKind, int vSize, int nBlocks){   int i;   n->WTrans = CreateMeanTransform(x, vSize, nBlocks);   n->HTrans = CreateVector(x, vSize);   for (i = 1; i <= vSize; i++)      n->HTrans[i] = 1.0; }/* Allocate space for a regression node of the regression tree */static void CreateNodeInfo(RegNode *n, RegTransInfo *rt, Boolean fixed){   n->nodeOcc = 0.0;   n->nodeComps = 0;   n->WTrans    = NULL;   n->backTrans = NULL;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -