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

📄 hmath.c

📁 该压缩包为最新版htk的源代码,htk是现在比较流行的语音处理软件,请有兴趣的朋友下载使用
💻 C
📖 第 1 页 / 共 3 页
字号:
/* ----------------------------------------------------------- *//*                                                             *//*                          ___                                *//*                       |_| | |_/   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: HMath.c   Math Support Module                 *//* ----------------------------------------------------------- */char *hmath_version = "!HVER!HMath:   3.3 [CUED 28/04/05]";char *hmath_vc_id = "$Id: HMath.c,v 1.1.1.1 2005/05/12 10:52:50 jal58 Exp $";/*   This library provides math support in the following three areas         a) Vector/Matrix Operators      b) Log Arithmetic      c) Random Deviates         It relies on the basic vector and matrix types defined by HMem.   The separation of functionality betwee HMem and HMath is such   that no routine in this module requires any knowledge of the   hidden fields in these types.  Thus, if a change of representation   is required, it should only be necessary to change HMem.*/#include "HShell.h"        /* HTK Libraries */#include "HMem.h"#include "HMath.h"/* ----------------------------- Trace Flags ------------------------- */static int trace = 0;/* -------------------- Configuration Parameters --------------------- */static ConfParam *cParm[MAXGLOBS];       /* config parameters */static int numParm = 0;/* ------------------ Vector Oriented Routines ----------------------- *//*   ShortVecs and IntVecs are pointers to arrays of short/int.  Vectors   are pointers to arrays of float (ie float*).  All indexing is   v[1..n].  The size is stored in v[0].*//* EXPORT->ZeroShortVec: Zero the elements of v */void ZeroShortVec(ShortVec v){   int i,n;      n=ShortVecSize(v);   for (i=1;i<=n;i++) v[i]=0;}/* EXPORT->ZeroIntVec: Zero the elements of v */void ZeroIntVec(IntVec v){   int i,n;      n=IntVecSize(v);   for (i=1;i<=n;i++) v[i]=0;}/* EXPORT->ZeroVector: Zero the elements of v */void ZeroVector(Vector v){   int i,n;      n=VectorSize(v);   for (i=1;i<=n;i++) v[i]=0.0;}/* EXPORT->ZeroDVector: Zero the elements of v */void ZeroDVector(DVector v){   int i,n;      n=DVectorSize(v);   for (i=1;i<=n;i++) v[i]=0.0;}/* EXPORT->CopyShortVec: copy v1 into v2 */void CopyShortVec(ShortVec v1, ShortVec v2){   int i,size;       size = ShortVecSize(v1);   if (size != ShortVecSize(v2))      HError(5270,"CopyShortVec: sizes differ %d vs %d",             size,ShortVecSize(v2));   for (i=1; i<=size; i++)       v2[i] = v1[i];}/* EXPORT->CopyIntVec: copy v1 into v2 */void CopyIntVec(IntVec v1, IntVec v2){   int i,size;       size = IntVecSize(v1);   if (size != IntVecSize(v2))      HError(5270,"CopyIntVec: sizes differ %d vs %d",             size,IntVecSize(v2));   for (i=1; i<=size; i++)       v2[i] = v1[i];}/* EXPORT->CopyVector: copy v1 into v2 */void CopyVector(Vector v1, Vector v2){   int i,size;       size = VectorSize(v1);   if (size != VectorSize(v2))      HError(5270,"CopyVector: sizes differ %d vs %d",             size,VectorSize(v2));   for (i=1; i<=size; i++)       v2[i] = v1[i];}/* EXPORT->CopyDVector: copy v1 into v2 */void CopyDVector(DVector v1, DVector v2){   int i,size;       size = DVectorSize(v1);   if (size != DVectorSize(v2))      HError(5270,"CopyDVector: sizes differ %d vs %d",             size,DVectorSize(v2));   for (i=1; i<=size; i++)       v2[i] = v1[i];}/* EXPORT->ReadShortVec: read vector from src in ascii or binary */ Boolean ReadShortVec(Source *src, ShortVec v, Boolean binary){   return ReadShort(src,v+1,ShortVecSize(v),binary);}/* EXPORT->ReadIntVec: read vector from src in ascii or binary */ Boolean ReadIntVec(Source *src, IntVec v, Boolean binary){   return ReadInt(src,v+1,IntVecSize(v),binary);}/* EXPORT->ReadVector: read vector from src in ascii or binary */ Boolean ReadVector(Source *src, Vector v, Boolean binary){   return ReadFloat(src,v+1,VectorSize(v),binary);}/* EXPORT->WriteShortVec: write vector v to stream f */void WriteShortVec(FILE *f, ShortVec v, Boolean binary){   WriteShort(f,v+1,ShortVecSize(v),binary);   if (!binary) fputc('\n',f);}/* EXPORT->WriteIntVec: write vector v to stream f */void WriteIntVec(FILE *f, IntVec v, Boolean binary){   WriteInt(f,v+1,IntVecSize(v),binary);   if (!binary) fputc('\n',f);}/* EXPORT->WriteVector: write vector v to stream f */void WriteVector(FILE *f, Vector v, Boolean binary){   WriteFloat(f,v+1,VectorSize(v),binary);   if (!binary) fputc('\n',f);}/* Export->ShowShortVec: show the short vector v preceded by title */void ShowShortVec(char * title, ShortVec v,int maxTerms){   int i, size, maxi;      size = maxi = ShortVecSize(v);   if (maxi>maxTerms) maxi=maxTerms;   printf("%s\n   ",title);      for (i=1;i<=maxi;i++)  printf("%3d ",v[i]);   if (maxi<size) printf("...");   printf("\n");}/* Export->ShowIntVec: show the int vector v preceded by title */void ShowIntVec(char * title, IntVec v,int maxTerms){   int i, size, maxi;      size = maxi = IntVecSize(v);   if (maxi>maxTerms) maxi=maxTerms;   printf("%s\n   ",title);      for (i=1;i<=maxi;i++)  printf("%5d ",v[i]);   if (maxi<size) printf("...");   printf("\n");}/* Export->ShowVector: show the vector v preceded by title */void ShowVector(char * title,Vector v,int maxTerms){   int i, size, maxi;      size = maxi = VectorSize(v);   if (maxi>maxTerms) maxi=maxTerms;   printf("%s\n   ",title);      for (i=1;i<=maxi;i++)  printf("%8.2f ",v[i]);   if (maxi<size) printf("...");   printf("\n");}/* Export->ShowDVector: show the vector v preceded by title */void ShowDVector(char * title, DVector v,int maxTerms){   int i, size, maxi;      size = maxi = DVectorSize(v);   if (maxi>maxTerms) maxi=maxTerms;   printf("%s\n   ",title);      for (i=1;i<=maxi;i++)  printf("%10.4f ",v[i]);   if (maxi<size) printf("...");   printf("\n");}/* ------------------ Matrix Oriented Routines ----------------------- *//*  Matrices are pointers to an array of vectors (ie float**).  Matrices  are indexed m[1..r][1..c].  The rows of matrix are genuine vectors  and can be treated as such except that matrices must be disposed of  in their entirety.  If the rows of a matrix are substituted by  other vectors then it should not be disposed.  TriMats are indexed  m[1..r][1..i] where i is the row number ie only the lower triangle  is stored.*//* EXPORT->ZeroMatrix: Zero the elements of m */void ZeroMatrix(Matrix m){   int i,j,nr,nc;      nr=NumRows(m); nc=NumCols(m);   for (i=1;i<=nr;i++)      for (j=1;j<=nc;j++) m[i][j]=0.0;}/* EXPORT->ZeroDMatrix: Zero the elements of m */void ZeroDMatrix(DMatrix m){   int i,j,nr,nc;      nr=NumDRows(m); nc=DVectorSize(m[1]);   for (i=1;i<=nr;i++)      for (j=1;j<=nc;j++) m[i][j]=0.0;}/* EXPORT->ZeroTriMat: Zero the elements of m */void ZeroTriMat(TriMat m){   int i,j,size;      size = TriMatSize(m);   for (i=1;i<=size;i++)      for (j=1;j<=i;j++) m[i][j]=0.0;}/* EXPORT->CopyMatrix: copy matrix m1 to m2 */void CopyMatrix(Matrix m1, Matrix m2){   int i,nrows;      nrows = NumRows(m1);   if (nrows != NumRows(m2))      HError(5270,"CopyMatrix: row sizes differ %d vs %d",             nrows,NumRows(m2));   for (i=1; i<=nrows; i++)      CopyVector(m1[i],m2[i]);}/* EXPORT->CopyDMatrix: copy matrix m1 to m2 */void CopyDMatrix(DMatrix m1, DMatrix m2){   int i,nrows;      nrows = NumDRows(m1);   if (nrows != NumDRows(m2))      HError(5270,"CopyDMatrix: row sizes differ %d vs %d",             nrows,NumDRows(m2));   for (i=1; i<=nrows; i++)      CopyDVector(m1[i],m2[i]);}/* EXPORT->CopyTriMat: copy triangular matrix m1 to m2 */void CopyTriMat(TriMat m1, TriMat m2){   int i,size;      size = TriMatSize(m1);   if (size != TriMatSize(m2))      HError(5270,"CopyTriMat: sizes differ %d vs %d",             size,TriMatSize(m2));   for (i=1; i<=size; i++)      CopyVector(m1[i],m2[i]);}/* EXPORT->Mat2DMat: convert matrix m1 to double matrix m2 */void Mat2DMat(Matrix m1,  DMatrix m2){   int i,j,nrows,ncols;   nrows = NumRows(m1);   if (nrows != NumDRows(m2))      HError(5270,"Mat2DMat: row sizes differ %d vs %d",             nrows,NumDRows(m2));   ncols = NumCols(m1);   if (ncols != NumDCols(m2))      HError(5270,"Mat2DMat: col sizes differ %d vs %d",             ncols,NumDCols(m2));   for (i=1; i<=nrows; i++)      for (j=1; j<=ncols; j++)          m2[i][j] = m1[i][j];}/* EXPORT->DMat2Mat: convert double matrix m1 to matrix m2 */void DMat2Mat(DMatrix m1, Matrix m2){   int i,j,nrows,ncols;   nrows = NumDRows(m1);   if (nrows != NumRows(m2))      HError(5270,"DMat2Mat: row sizes differ %d vs %d",             nrows,NumRows(m2));   ncols = NumDCols(m1);   if (ncols != NumCols(m2))      HError(5270,"DMat2Mat: col sizes differ %d vs %d",             ncols,NumCols(m2));   for (i=1; i<=nrows; i++)      for (j=1; j<=ncols; j++)          m2[i][j] = m1[i][j];}/* EXPORT->Mat2Tri: convert matrix m1 to tri matrix m2 */void Mat2Tri (Matrix m1,  TriMat m2){   int i,j,nrows,ncols;   nrows = NumRows(m1); ncols = NumCols(m1);   if (nrows != ncols)      HError(5270,"Mat2Tri: source matrix not square %d vs %d",             nrows,ncols);      if (ncols != TriMatSize(m2))      HError(5270,"Mat2Tri: sizes differ %d vs %d",             ncols,TriMatSize(m2));   for (i=1; i<=nrows; i++)      for (j=1; j<=i; j++)          m2[i][j] = m1[i][j];}/* EXPORT->Tri2Mat: convert tri matrix m1 to matrix m2 */void Tri2Mat (TriMat m1, Matrix m2){   int i,j,nrows,ncols;   nrows = NumRows(m2); ncols = NumCols(m2);   if (nrows != ncols)      HError(5270,"Tri2Mat: target matrix not square %d vs %d",             nrows,ncols);      if (ncols != TriMatSize(m1))      HError(5270,"Tri2Mat: sizes differ %d vs %d",             TriMatSize(m1),ncols);   for (i=1; i<=nrows; i++)      for (j=1; j<=i; j++) {         m2[i][j] = m1[i][j];         if (i!=j) m2[j][i] = m1[i][j];      }}/* EXPORT->ReadMatrix: read matrix from source into m */Boolean ReadMatrix(Source *src, Matrix m, Boolean binary){   int i,nrows;      nrows = NumRows(m);   for (i=1; i<=nrows; i++)      if (!ReadVector(src,m[i],binary))          return FALSE;   return TRUE;}/* EXPORT->ReadTriMat: read symmetric matrix in lower triangular                       form from source into m */Boolean ReadTriMat(Source *src, TriMat m, Boolean binary){   int i,j,size;      size = TriMatSize(m);   for (j=1; j<=size; j++) {      for (i=j; i<=size; i++)         if (!ReadFloat(src,&(m[i][j]),1,binary))            return FALSE;   }   return TRUE;}/* EXPORT->WriteMatrix: write matrix to f */void WriteMatrix(FILE *f, Matrix m, Boolean binary){   int i,nrows;      nrows = NumRows(m);   for (i=1; i<=nrows; i++)      WriteVector(f,m[i],binary);}/* EXPORT->WriteTriMat: write symmetric matrix to stream f in                        upper triangular form */void WriteTriMat(FILE *f, TriMat m, Boolean binary){   int i,j,size;      size = TriMatSize(m);   for (j=1; j<=size; j++) {      for (i=j; i<=size; i++)         WriteFloat(f,&(m[i][j]),1,binary);      if (!binary) fputc('\n',f);   }}/* Export->ShowMatrix: show the matrix m preceded by title */void ShowMatrix(char * title,Matrix m,int maxCols,int maxRows){   int i,j;   int maxi,maxj,nrows,ncols;      maxi = nrows = NumRows(m);   if (maxi>maxRows) maxi = maxRows;   maxj = ncols = NumCols(m);   if (maxj>maxCols) maxj = maxCols;   printf("%s\n",title);   for (i=1;i<=maxi;i++) {      printf("   ");      for (j=1;j<=maxj;j++)         printf("%8.2f ",m[i][j]);      if (maxj<ncols) printf("...");      printf("\n");   }   if (maxi<nrows)      printf("   ...\n");}/* Export->ShowDMatrix: show the matrix m preceded by title */void ShowDMatrix(char * title,DMatrix m,int maxCols,int maxRows){   int i,j;   int maxi,maxj,nrows,ncols;      maxi = nrows = NumDRows(m);   if (maxi>maxRows) maxi = maxRows;   maxj = ncols = DVectorSize(m[1]);   if (maxj>maxCols) maxj = maxCols;   printf("%s\n",title);   for (i=1;i<=maxi;i++) {      printf("   ");      for (j=1;j<=maxj;j++)         printf("%10.4f ",m[i][j]);      if (maxj<ncols) printf("...");      printf("\n");   }   if (maxi<nrows)      printf("   ...\n");}/* Export->ShowTriMat: show the matrix m preceded by title */void ShowTriMat(char * title,TriMat m,int maxCols,int maxRows){   int i,j;   int maxi,maxj,size;      size = TriMatSize(m);   maxi = size;   if (maxi>maxRows) maxi = maxRows;   printf("%s\n",title);   for (i=1;i<=maxi;i++) {      printf("   ");      maxj = i;      if (maxj>maxCols) maxj = maxCols;      for (j=1;j<=maxj;j++)         printf("%8.2f ",m[i][j]);      if (maxj<i) printf("...");      printf("\n");   }   if (maxi<size)      printf("   ...\n");}/* -------------------- Matrix Operations ---------------------- *//* Choleski: Place lower triangular choleski factor of A in L.*//*           Return FALSE if matrix singular or not +definite */static Boolean Choleski(TriMat A, DMatrix L){   int size,i,j,k;   double sum;   size = TriMatSize(A);   for (i=1; i<=size; i++)      for (j=1; j<=i; j++) {         sum=A[i][j];         for (k=1; k<j; k++)            sum -= (L[i][k]*L[j][k]);         if ((i==j)&&(sum<=0.0))             return FALSE;         else if (i==j)            sum = sqrt(sum);         else if (L[j][j]==0.0)            return FALSE;         else            sum /= L[j][j];         L[i][j] = sum;      }   for (i=1; i<=size; i++)       for (j=i+1; j<=size; j++)          L[i][j] = 0.0;   return TRUE;}/* MSolve: solve Ly=e^i and L^t x = y, where e^i is a unit vector */static void MSolve(DMatrix L, int i, DVector x, DVector y){   int nr,j,k;   double sum;      nr=NumDRows(L);   for (j=1; j<i; j++) y[j] = 0.0;  /* forward sub */   y[i] = 1.0/L[i][i];   for (j=i+1; j<=nr; j++){      sum = 0.0;      for (k=i; k<j; k++)         sum -= L[j][k]*y[k];      y[j] = sum /L[j][j];   }   x[nr] = y[nr]/L[nr][nr];         /* backward sub */   for (j=nr-1; j>=1; j--){      sum = y[j];      for (k=j+1; k<=nr; k++)         sum -= L[k][j]*x[k];      x[j] = sum / L[j][j];   }

⌨️ 快捷键说明

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