SphinxMatrixHolder< kFeatDim > Class Template Reference

A class for reading/writing Sphinx format matrices. More...

#include <kaldi-holder-inl.h>

Collaboration diagram for SphinxMatrixHolder< kFeatDim >:

Public Types

typedef Matrix< BaseFloatT
 

Public Member Functions

 SphinxMatrixHolder ()
 
void Clear ()
 
bool Read (std::istream &is)
 
TValue ()
 
void Swap (SphinxMatrixHolder *other)
 
bool ExtractRange (const SphinxMatrixHolder &other, const std::string &range)
 

Static Public Member Functions

static bool Write (std::ostream &os, bool binary, const T &m)
 
static bool IsReadInBinary ()
 

Private Member Functions

 KALDI_DISALLOW_COPY_AND_ASSIGN (SphinxMatrixHolder)
 

Private Attributes

T feats_
 

Detailed Description

template<int kFeatDim>
class kaldi::SphinxMatrixHolder< kFeatDim >

A class for reading/writing Sphinx format matrices.

Definition at line 816 of file kaldi-holder-inl.h.

Member Typedef Documentation

◆ T

typedef Matrix<BaseFloat> T

Definition at line 818 of file kaldi-holder-inl.h.

Constructor & Destructor Documentation

◆ SphinxMatrixHolder()

SphinxMatrixHolder ( )
inline

Definition at line 820 of file kaldi-holder-inl.h.

820 {}

Member Function Documentation

◆ Clear()

void Clear ( )
inline

Definition at line 822 of file kaldi-holder-inl.h.

822 { feats_.Resize(0, 0); }
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).

◆ ExtractRange()

bool ExtractRange ( const SphinxMatrixHolder< kFeatDim > &  other,
const std::string &  range 
)
inline

Definition at line 904 of file kaldi-holder-inl.h.

References KaldiObjectHolder< KaldiType >::KALDI_DISALLOW_COPY_AND_ASSIGN(), and KALDI_ERR.

905  {
906  KALDI_ERR << "ExtractRange is not defined for this type of holder.";
907  return false;
908  }
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ IsReadInBinary()

static bool IsReadInBinary ( )
inlinestatic

Definition at line 896 of file kaldi-holder-inl.h.

896 { return true; }

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( SphinxMatrixHolder< kFeatDim >  )
private

◆ Read()

bool Read ( std::istream &  is)
inline

Definition at line 851 of file kaldi-holder-inl.h.

References rnnlm::i, rnnlm::j, KALDI_SWAP4, KALDI_VLOG, KALDI_WARN, and kaldi::MachineIsLittleEndian().

851  {
852  int32 nmfcc;
853 
854  is.read(reinterpret_cast<char*> (&nmfcc), sizeof(nmfcc));
855  if (MachineIsLittleEndian())
856  KALDI_SWAP4(nmfcc);
857  KALDI_VLOG(2) << "#feats: " << nmfcc;
858  int32 nfvec = nmfcc / kFeatDim;
859  if ((nmfcc % kFeatDim) != 0) {
860  KALDI_WARN << "Sphinx feature count is inconsistent with vector length ";
861  return false;
862  }
863 
864  feats_.Resize(nfvec, kFeatDim);
865  for (MatrixIndexT i = 0; i < feats_.NumRows(); i++) {
866  if (sizeof(BaseFloat) == sizeof(float32)) {
867  is.read(reinterpret_cast<char*> (feats_.RowData(i)),
868  kFeatDim * sizeof(float32));
869  if (!is.good()) {
870  KALDI_WARN << "Unexpected error/EOF while reading Sphinx features ";
871  return false;
872  }
873  if (MachineIsLittleEndian()) {
874  for (MatrixIndexT j = 0; j < kFeatDim; j++)
875  KALDI_SWAP4(feats_(i, j));
876  }
877  } else { // KALDI_DOUBLEPRECISION=1
878  float32 tmp[kFeatDim];
879  is.read(reinterpret_cast<char*> (tmp), sizeof(tmp));
880  if (!is.good()) {
881  KALDI_WARN << "Unexpected error/EOF while reading Sphinx features ";
882  return false;
883  }
884  for (MatrixIndexT j = 0; j < kFeatDim; j++) {
885  if (MachineIsLittleEndian())
886  KALDI_SWAP4(tmp[j]);
887  feats_(i, j) = static_cast<BaseFloat>(tmp[j]);
888  }
889  }
890  }
891 
892  return true;
893  }
Real * RowData(MatrixIndexT i)
Returns pointer to data for one row (non-const)
Definition: kaldi-matrix.h:87
kaldi::int32 int32
float BaseFloat
Definition: kaldi-types.h:29
int32 MatrixIndexT
Definition: matrix-common.h:98
float float32
Definition: kaldi-types.h:53
int MachineIsLittleEndian()
Definition: kaldi-utils.h:83
#define KALDI_WARN
Definition: kaldi-error.h:150
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
#define KALDI_SWAP4(a)
Definition: kaldi-utils.h:107

◆ Swap()

void Swap ( SphinxMatrixHolder< kFeatDim > *  other)
inline

Definition at line 900 of file kaldi-holder-inl.h.

References SphinxMatrixHolder< kFeatDim >::feats_.

900  {
901  feats_.Swap(&(other->feats_));
902  }
void Swap(Matrix< Real > *other)
Swaps the contents of *this and *other. Shallow swap.

◆ Value()

T& Value ( )
inline

Definition at line 898 of file kaldi-holder-inl.h.

◆ Write()

static bool Write ( std::ostream &  os,
bool  binary,
const T m 
)
inlinestatic

Definition at line 825 of file kaldi-holder-inl.h.

References rnnlm::i, rnnlm::j, KALDI_SWAP4, KALDI_WARN, kaldi::MachineIsLittleEndian(), MatrixBase< Real >::NumCols(), and MatrixBase< Real >::NumRows().

825  {
826  if (!binary) {
827  KALDI_WARN << "SphinxMatrixHolder can't write Sphinx features in text ";
828  return false;
829  }
830 
831  int32 size = m.NumRows() * m.NumCols();
832  if (MachineIsLittleEndian())
833  KALDI_SWAP4(size);
834  // write the header
835  os.write(reinterpret_cast<char*> (&size), sizeof(size));
836 
837  for (MatrixIndexT i = 0; i < m.NumRows(); i++) {
838  std::vector<float32> tmp(m.NumCols());
839  for (MatrixIndexT j = 0; j < m.NumCols(); j++) {
840  tmp[j] = static_cast<float32>(m(i, j));
841  if (MachineIsLittleEndian())
842  KALDI_SWAP4(tmp[j]);
843  }
844  os.write(reinterpret_cast<char*>(&(tmp[0])),
845  tmp.size() * 4);
846  }
847  return true;
848  }
kaldi::int32 int32
int32 MatrixIndexT
Definition: matrix-common.h:98
float float32
Definition: kaldi-types.h:53
int MachineIsLittleEndian()
Definition: kaldi-utils.h:83
#define KALDI_WARN
Definition: kaldi-error.h:150
#define KALDI_SWAP4(a)
Definition: kaldi-utils.h:107

Member Data Documentation

◆ feats_

T feats_
private

Definition at line 912 of file kaldi-holder-inl.h.

Referenced by SphinxMatrixHolder< kFeatDim >::Swap().


The documentation for this class was generated from the following file: