OnlineTransform Class Reference

This online-feature class implements any affine or linear transform. More...

#include <online-feature.h>

Inheritance diagram for OnlineTransform:
Collaboration diagram for OnlineTransform:

Public Member Functions

virtual int32 Dim () const
 
virtual bool IsLastFrame (int32 frame) const
 Returns true if this is the last frame. More...
 
virtual BaseFloat FrameShiftInSeconds () const
 
virtual int32 NumFramesReady () const
 returns the feature dimension. More...
 
virtual void GetFrame (int32 frame, VectorBase< BaseFloat > *feat)
 Gets the feature vector for this frame. More...
 
virtual void GetFrames (const std::vector< int32 > &frames, MatrixBase< BaseFloat > *feats)
 This is like GetFrame() but for a collection of frames. More...
 
 OnlineTransform (const MatrixBase< BaseFloat > &transform, OnlineFeatureInterface *src)
 The transform can be a linear transform, or an affine transform where the last column is the offset. More...
 
- Public Member Functions inherited from OnlineFeatureInterface
virtual ~OnlineFeatureInterface ()
 Virtual destructor. More...
 

Private Attributes

OnlineFeatureInterfacesrc_
 
Matrix< BaseFloatlinear_term_
 
Vector< BaseFloatoffset_
 

Detailed Description

This online-feature class implements any affine or linear transform.

Definition at line 493 of file online-feature.h.

Constructor & Destructor Documentation

◆ OnlineTransform()

OnlineTransform ( const MatrixBase< BaseFloat > &  transform,
OnlineFeatureInterface src 
)

The transform can be a linear transform, or an affine transform where the last column is the offset.

Definition at line 521 of file online-feature.cc.

References OnlineFeatureInterface::Dim(), KALDI_ERR, OnlineTransform::linear_term_, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), OnlineTransform::offset_, MatrixBase< Real >::Range(), and OnlineTransform::src_.

522  :
523  src_(src) {
524  int32 src_dim = src_->Dim();
525  if (transform.NumCols() == src_dim) { // Linear transform
526  linear_term_ = transform;
527  offset_.Resize(transform.NumRows()); // Resize() will zero it.
528  } else if (transform.NumCols() == src_dim + 1) { // Affine transform
529  linear_term_ = transform.Range(0, transform.NumRows(), 0, src_dim);
530  offset_.Resize(transform.NumRows());
531  offset_.CopyColFromMat(transform, src_dim);
532  } else {
533  KALDI_ERR << "Dimension mismatch: source features have dimension "
534  << src_dim << " and LDA #cols is " << transform.NumCols();
535  }
536 }
kaldi::int32 int32
Matrix< BaseFloat > linear_term_
#define KALDI_ERR
Definition: kaldi-error.h:147
SubMatrix< Real > Range(const MatrixIndexT row_offset, const MatrixIndexT num_rows, const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Return a sub-part of matrix.
Definition: kaldi-matrix.h:202
Vector< BaseFloat > offset_
OnlineFeatureInterface * src_
virtual int32 Dim() const =0

Member Function Documentation

◆ Dim()

virtual int32 Dim ( ) const
inlinevirtual

Implements OnlineFeatureInterface.

Definition at line 498 of file online-feature.h.

Referenced by OnlineDeltaFeature::GetFrame().

498 { return offset_.Dim(); }
Vector< BaseFloat > offset_

◆ FrameShiftInSeconds()

virtual BaseFloat FrameShiftInSeconds ( ) const
inlinevirtual

Implements OnlineFeatureInterface.

Definition at line 503 of file online-feature.h.

503  {
504  return src_->FrameShiftInSeconds();
505  }
virtual BaseFloat FrameShiftInSeconds() const =0
OnlineFeatureInterface * src_

◆ GetFrame()

void GetFrame ( int32  frame,
VectorBase< BaseFloat > *  feat 
)
virtual

Gets the feature vector for this frame.

Before calling this for a given frame, it is assumed that you called NumFramesReady() and it returned a number greater than "frame". Otherwise this call will likely crash with an assert failure. This function is not declared const, in case there is some kind of caching going on, but most of the time it shouldn't modify the class.

Implements OnlineFeatureInterface.

Definition at line 538 of file online-feature.cc.

References VectorBase< Real >::AddMatVec(), VectorBase< Real >::CopyFromVec(), OnlineFeatureInterface::GetFrame(), kaldi::kNoTrans, OnlineTransform::linear_term_, MatrixBase< Real >::NumCols(), OnlineTransform::offset_, and OnlineTransform::src_.

538  {
539  Vector<BaseFloat> input_feat(linear_term_.NumCols());
540  src_->GetFrame(frame, &input_feat);
541  feat->CopyFromVec(offset_);
542  feat->AddMatVec(1.0, linear_term_, kNoTrans, input_feat, 1.0);
543 }
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
virtual void GetFrame(int32 frame, VectorBase< BaseFloat > *feat)=0
Gets the feature vector for this frame.
Matrix< BaseFloat > linear_term_
Vector< BaseFloat > offset_
OnlineFeatureInterface * src_

◆ GetFrames()

void GetFrames ( const std::vector< int32 > &  frames,
MatrixBase< BaseFloat > *  feats 
)
virtual

This is like GetFrame() but for a collection of frames.

There is a default implementation that just gets the frames one by one, but it may be overridden for efficiency by child classes (since sometimes it's more efficient to do things in a batch).

Reimplemented from OnlineFeatureInterface.

Definition at line 545 of file online-feature.cc.

References MatrixBase< Real >::AddMatMat(), MatrixBase< Real >::CopyRowsFromVec(), OnlineFeatureInterface::GetFrames(), KALDI_ASSERT, kaldi::kNoTrans, kaldi::kTrans, kaldi::kUndefined, OnlineTransform::linear_term_, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), OnlineTransform::offset_, and OnlineTransform::src_.

546  {
547  KALDI_ASSERT(static_cast<int32>(frames.size()) == feats->NumRows());
548  int32 num_frames = feats->NumRows(),
549  input_dim = linear_term_.NumCols();
550  Matrix<BaseFloat> input_feats(num_frames, input_dim, kUndefined);
551  src_->GetFrames(frames, &input_feats);
552  feats->CopyRowsFromVec(offset_);
553  feats->AddMatMat(1.0, input_feats, kNoTrans, linear_term_, kTrans, 1.0);
554 }
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
kaldi::int32 int32
virtual void GetFrames(const std::vector< int32 > &frames, MatrixBase< BaseFloat > *feats)
This is like GetFrame() but for a collection of frames.
Matrix< BaseFloat > linear_term_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Vector< BaseFloat > offset_
OnlineFeatureInterface * src_

◆ IsLastFrame()

virtual bool IsLastFrame ( int32  frame) const
inlinevirtual

Returns true if this is the last frame.

Frame indices are zero-based, so the first frame is zero. IsLastFrame(-1) will return false, unless the file is empty (which is a case that I'm not sure all the code will handle, so be careful). This function may return false for some frame if we haven't yet decided to terminate decoding, but later true if we decide to terminate decoding. This function exists mainly to correctly handle end effects in feature extraction, and is not a mechanism to determine how many frames are in the decodable object (as it used to be, and for backward compatibility, still is, in the Decodable interface).

Implements OnlineFeatureInterface.

Definition at line 500 of file online-feature.h.

500  {
501  return src_->IsLastFrame(frame);
502  }
virtual bool IsLastFrame(int32 frame) const =0
Returns true if this is the last frame.
OnlineFeatureInterface * src_

◆ NumFramesReady()

virtual int32 NumFramesReady ( ) const
inlinevirtual

returns the feature dimension.

Returns the total number of frames, since the start of the utterance, that are now available. In an online-decoding context, this will likely increase with time as more data becomes available.

Implements OnlineFeatureInterface.

Definition at line 507 of file online-feature.h.

Referenced by OnlineDeltaFeature::GetFrame().

507 { return src_->NumFramesReady(); }
OnlineFeatureInterface * src_
virtual int32 NumFramesReady() const =0
returns the feature dimension.

Member Data Documentation

◆ linear_term_

◆ offset_

◆ src_


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