/**************************************************************************/ /* File: hmm_matrix.cc */ /**************************************************************************/ /* Project: Continuous Speech Recognition Search Algorithms */ /* Author: Neeraj Deshmukh, Aravind Ganapathiraju */ /* Class: EE 8993 Spring 1996 Date: March 12, 1996 */ /**************************************************************************/ /*======================================================================== This file defines the HMM state matrix class that models the various mean and covariances vectors for the EE 8993 speech recognizer ========================================================================*/ /*------------------------------------------------------------------------ system and ISIP include files ------------------------------------------------------------------------*/ #include "hmm_matrix.h" /*------------------------------------------------------------------------ forward declaration of classes ------------------------------------------------------------------------*/ class HMM_Matrix; /*========================================================================== function definitions fror class HMM_Matrix =========================================================================*/ /*--------------------------- constructors and destructor ---------------------------*/ // default constructors // HMM_Matrix :: HMM_Matrix (int_2 nRows_l, int_2 nColumns_l, float_4 dval_l) { numRows = nRows_l; numColumns = nColumns_l; element = new float_4*[numRows]; for(int_2 i = 0; i < numRows; i++) { element[i] = new float_4[numColumns]; for(int_2 j = 0; j < numColumns; j++) { element[i][j] = dval_l; } } } HMM_Matrix :: HMM_Matrix (int_2 nRows_l, int_2 nColumns_l, float_4 *dval_l) { numRows = nRows_l; numColumns = nColumns_l; element = new float_4*[numRows]; for(int_2 i = 0; i < numRows; i++) { element[i] = new float_4[numColumns]; if (dval_l) { for(int_2 j = 0; j < numColumns; j++) { element[i][j] = dval_l[i]; } } } } HMM_Matrix :: HMM_Matrix (int_2 nRows_l, int_2 nColumns_l, float_4 **dval_l) { numRows = nRows_l; numColumns = nColumns_l; element = new float_4*[numRows]; for(int_2 i = 0; i < numRows; i++) { element[i] = new float_4[numColumns]; if (dval_l) { for(int_2 j = 0; j < numColumns; j++) { element[i][j] = dval_l[i][j]; } } } } // copy constructors // HMM_Matrix :: HMM_Matrix (const HMM_Matrix & umat_l) { numRows = umat_l.numRows; numColumns = umat_l.numColumns; element = new float_4*[numRows]; for(int_2 i = 0; i < numRows; i++) { element[i] = new float_4[numColumns]; for(int_2 j = 0; j < numColumns; j++) { element[i][j] = umat_l.element[i][j]; } } } // destructor // HMM_Matrix :: ~HMM_Matrix () { // free dynamic memory buffer // delete [] element; } /*------------------------ matrix operation methods ------------------------*/ //overloading the ' = ' operator // HMM_Matrix & HMM_Matrix :: operator = (const HMM_Matrix & umat_l) { numRows = umat_l.numRows; numColumns = umat_l.numColumns; for(int_2 i = 0; i < numRows; i++) { for(int_2 j = 0; j < numColumns; j++) { element[i][j] = umat_l.element[i][j]; } } // return this value return (*this); } //addition of two matrices // HMM_Matrix HMM_Matrix :: operator + (const HMM_Matrix & umat_l) { // create new HMM_Matrix for result // HMM_Matrix sum (umat_l.numRows, umat_l.numColumns, 0.0); // perform addition // for (int i = 0; i < sum.numRows; i++) { for (int j = 0; j < sum.numColumns; j++) { sum.element[i][j] = element[i][j] + umat_l.element[i][j]; } } // return final answer // return (sum); } // scalar product // HMM_Matrix HMM_Matrix :: operator * (float_4 dd_l) { // create new matrix for result // HMM_Matrix product (numRows, numColumns, 0.0); // perform scalar multiplication // for (int i = 0; i < product.numRows; i++) { for (int j = 0; j < product.numColumns; j++) { product.element[i][j] = dd_l * element[i][j]; } } // return final answer // return (product); } // Matrix Multiplication // HMM_Matrix HMM_Matrix :: operator * (const HMM_Matrix & umat_l) { // create new matrix for result // HMM_Matrix product (numRows, umat_l.numColumns, 0.0); // perform termwise multiplication // for (int i = 0; i < product.numRows; i++) { for (int j = 0; j < product.numColumns; j++) { for (int k = 0; k < umat_l.numRows; k++) { product.element[i][j] += element[i][k] * umat_l.element[k][j] ; } } } // return final answer // return (product); } // matrix transpose // HMM_Matrix HMM_Matrix :: transpose_matrix_cc () { HMM_Matrix transpose (numColumns, numRows, 0.0); for (int i = 0; i < transpose.numRows; i++) { for (int j = 0; j < transpose.numColumns; j++) { transpose.element[i][j] = element[j][i]; } } //return the transposed matrix // return(transpose); } // set an element of a matrix // void HMM_Matrix :: set_element_cc (int_2 nR_l, int_2 nC_l, float_4 value_l) { element[nR_l][nC_l] = value_l; } // get an element of a matrix // float_4 HMM_Matrix :: get_element_cc (int_2 nR_l, int_2 nC_l) { return (element[nR_l][nC_l]); } //get number of rows of the matrix // int_2 HMM_Matrix :: get_num_rows_cc () { return ( numRows ); } //get number of columns of the matrix // int_2 HMM_Matrix :: get_num_columns_cc () { return ( numColumns ); } // dump the matrix values // void HMM_Matrix :: print_matrix_cc () { for (int i = 0; i < numRows; i++) { for (int j = 0; j < numColumns; j++) { fprintf (stdout, "%f ", element[i][j]); } fprintf (stdout, "\n"); } }