/********************************************************************************/ /* PVTK, the periodic vector toolkit: A very small toolkit for interfacing svmlight and HTK Copyright 2005, Trustees of the University of Illinois Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Revision history: May 2005, Sarah Borys: Increased maximum number of transforms August 2004, Mark Hasegawa-Johnson: First revision created Likely future revisions: PVTK should be cleaned up, made independent of HTK, merged with SpeechLib, and rewritten from the bottom up to be compatible with LAPACK and STL. */ /********************************************************************************/ #ifndef _PVTK_H_ #define _PVTK_H_ /* PVTK.h: "$Id: PVTK.h,v 1.3 2004/08/12 19:17:14 mhasegaw Exp mhasegaw $"; */ #include #include #include #include #include #include #include "HShell.h" #include "HMem.h" #include "HMath.h" #include "HSigP.h" #include "HWave.h" #include "HVQ.h" #include "HAudio.h" #include "HParm.h" #include "HLabel.h" #include "HModel.h" /* A Structure for storing HTK parameter-file information */ typedef struct _HParm { int nrows; /* number of rows that should be in h->X; only meaningful if h->X==NULL */ int ncols; /* number of columns that should be in h->X; only meaningful if h->X==NULL */ int sampPeriod; /* number of 100ns units between frames */ short HTKCode; /* code specifying the file type, compression, etcetera */ DMatrix X; /* data read from the file */ } HParm; /* The SVM structure */ typedef struct _SVMDef { int kerneltype; /* kernel type */ DMatrix SV; /* support vectors */ DVector alpha; /* weights for support vectors */ DVector w; /* hyperplane orthonormal vector */ double b; /* SVM offset parameter */ double d; /* polynomial kernel power parameter */ double g; /* RBF kernel inverse-variance parameter */ double r; /* poly and tanh kernels: offset parameter */ double s; /* poly and tanh kernels: scale parameter */ } SVMDef; /* Define kernel types, and other things that use "kerneltype" */ /* Non-negative codes are intended to duplicate those used by svm-light */ /* Negative codes are for SVM-like structures that aren't really SVMs, */ /* including training and test tokens read from a text file (K_INPUT) */ /* and training or test tokens read from an HTK file (K_HTK) */ #define K_LINEAR 0 #define K_POLY 1 #define K_RBF 2 #define K_SIGMOID 3 #define K_INPUT -1 #define K_HTK -2 /* Define trace options */ #define T_TOP 0x01 /* basic progress reporting */ #define T_IO 0x02 /* I/O debugging */ #define T_OPTS 0x04 /* debug command-line options */ #define T_VERBOSE 0x08 /* debug lots of things */ #define T_MEM 0x10 /* debug memory usage */ /* Define maximum lengths for different kinds of lists */ #define MAX_LINE 5000 /* needs to be long enough for a whole param or MLF line, not a whole support vector */ #define MAXTRANS 200 /* maximum allowable number of transforms or SVMs */ #define MAX_FILES 50000 /* maximum number of input files that we can read */ #define MAX_CLASSES 200 /* maximum number of classes */ #define MAX_PATTERNS 200 /* maximum number of patterns defining each class */ #define MAX_OUTPUTS 200 /* maximum number of output files */ #define MAX_LABELS 1000 /* maximum number of label lines from any one transcript */ /* Variance floor, for things that need a variance floor (such as sigmoid posterior) */ #define VARIANCE_FLOOR 1e-6 #define STDEV_FLOOR 1e-3 /* ---------------------- Function Prototypes ----------------------- */ /* Debugging tool */ void PrintRCSIdentifier(FILE *fid); /* Basic fread and fwrite for machine-independent big-endian data */ size_t byteswap(char *ptr, size_t size, size_t nmemb); size_t fwrite_bigendian(void *ptr, size_t size, size_t nmemb, FILE *stream); size_t fread_bigendian(void *ptr, size_t size, size_t nmemb, FILE *stream); /* Functions for reading DVector and DMatrix */ Boolean ReadDVector(FILE *src, DVector v, Boolean binary); Boolean ReadDMatrix(FILE *src, DMatrix m, Boolean binary); IntVec strtoIntVec(MemHeap *heap, char *s); DVector strtoDVector(MemHeap *heap, char *s); char *remove_wrapper(char *pat); /* Returns n, bounded to the range [min,max] */ int BoundInteger(int n, int min, int max); /* Functions for copying and zeroing row and column subvectors */ void CopySubVectorDD(DVector v1, DVector v2, int s1, int s2, int n); void CopySubColumnDD(DMatrix m1, int r1, int c1, DMatrix m2, int r2, int c2, int n); void ZeroSubVectorDD(DVector v1, int s1, int n); void CopySubVectorDF(DVector v1, Vector v2, int s1, int s2, int n); void CopySubVectorFD(Vector v1, DVector v2, int s1, int s2, int n); /* Mathematical manipulation of DVectors and DMatrices */ void DMatrixMax(DMatrix A, DVector v); void DMatrixMin(DMatrix A, DVector v); void TransposeDMatrix(DMatrix A, DMatrix B); void AddDVectors(DVector C, DVector A, DVector B); /* Read, write, and print svmlight and libsvm files */ char *fgetw(char *w, int size, FILE *fid); SVMDef *ReadSVMLightHeader(FILE *fid, MemHeap *heap, SVMDef *s); SVMDef *ReadLibSVMHeader(FILE *fid, MemHeap *heap, SVMDef *s); void ZeroSVMDef(SVMDef *s); SVMDef *ReadSVMFile(char *filename, MemHeap *heap, SVMDef *s, int trace); void PrintSVMDef(FILE *fid, SVMDef *s); int SaveSVMVectors(FILE *fid, SVMDef *s); int SaveSVMDef(char *filename, SVMDef *s); /* Read and write HTK files */ int read_MLF(FILE *fid, MemHeap *heap, int nTranscripts, char **TranscriptFilenames, int *nLabels, char ***TranscriptLabels, int **TranscriptStartTimes, int **TranscriptEndTimes); void ZeroHParm(HParm *h); void HParm2SVMDef(MemHeap *heap, HParm *h, SVMDef *s, int *sampPeriod, short *HTKCode); void SVMDef2HParm(MemHeap *heap, SVMDef *s, HParm *h, int *sampPeriod, short *HTKCode); int WriteHParm(MemHeap *heap, char *filename, HParm *h, int trace); HParm *ReadHParm(MemHeap *heap, char *filename, HParm *h, int trace); HParm *ReadHParmHeader(FILE *fid, HParm *h); void WriteHParmHeader(FILE *fid, HParm *h); void CopyHParmHeader(HParm *src, HParm *tgt); #endif /* _PVTK_H_ */