kaldi-native-fbank C++ API
Kaldi-compatible feature extraction C++ API
Loading...
Searching...
No Matches
knf::FeatureTransform Class Reference

A linear (or affine) feature transform, e.g. More...

#include <feature-transform.h>

Public Member Functions

 FeatureTransform (const MatrixF &mat)
 Construct a transform from a matrix directly.
int32_t InputDim () const
 Number of columns of the stored matrix.
int32_t OutputDim () const
 Number of rows of the stored matrix (the output dimension).
bool IsAffine (int32_t input_dim) const
 True if the transform is affine for inputs of dimension input_dim.
MatrixF Apply (const MatrixF &feats) const
 Apply the transform to a feature matrix.
void Apply (const float *feat, int32_t dim, float *out) const
 Apply the transform to a single feature frame.

Static Public Member Functions

static FeatureTransform Read (const std::string &filename)
 Read a transform matrix from a kaldi matrix file.

Detailed Description

A linear (or affine) feature transform, e.g.

an LDA matrix.

The transform is a matrix \( M \) of shape \( (D_{out} \times n) \); see the file description for the exact semantics, which follow kaldi's transform-feats (src/featbin/transform-feats.cc):

if \( n = D_{in} \) (the input feature dimension), the transform is linear: \( y = x\, M^{T} \); if \( n = D_{in} + 1 \), the transform is affine: the input features get an implicit 1.0 appended, so \( y = x\, M_{[:,\,0:n-1]}^{T} + M_{[:,\,n-1]} \); otherwise Apply() throws.

knf::MatrixF out = lda.Apply(feats);
A linear (or affine) feature transform, e.g.
Definition feature-transform.h:67
static FeatureTransform Read(const std::string &filename)
Read a transform matrix from a kaldi matrix file.
MatrixF Apply(const MatrixF &feats) const
Apply the transform to a feature matrix.
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MatrixF
Row-major feature matrix: rows are frames, columns are features.
Definition feature-matrix.h:26
Used by:
Pipelines that reduce/rotate features (mfcc -> cmvn -> splice -> lda).
See also
Read, Apply

Constructor & Destructor Documentation

◆ FeatureTransform()

knf::FeatureTransform::FeatureTransform ( const MatrixF & mat)
explicit

Construct a transform from a matrix directly.

Parameters
[in]matThe transform matrix of shape (OutputDim() x InputDim()), or one column wider for an affine transform (the last column is the offset).

Member Function Documentation

◆ Apply() [1/2]

void knf::FeatureTransform::Apply ( const float * feat,
int32_t dim,
float * out ) const

Apply the transform to a single feature frame.

Same semantics as the matrix version of Apply().

Parameters
[in]featPointer to one frame of dimension dim.
[in]dimDimension of the frame.
[out]outPointer to an array of OutputDim() floats; the caller must pre-allocate it.

◆ Apply() [2/2]

MatrixF knf::FeatureTransform::Apply ( const MatrixF & feats) const

Apply the transform to a feature matrix.

kaldi transform-feats semantics (rows = frames):

if the transform has feats.cols() columns, the output is \( Y = X M^{T} \) (linear); if it has feats.cols() + 1 columns, the transform is affine and the last column is the offset: \( Y = X M_{[:,\,0:n-1]}^{T} + \mathbf{1} b^{T} \); otherwise an exception is thrown.

Parameters
[in]featsFeature matrix of size T x D, float32.
Returns
The transformed matrix of size T x OutputDim().

◆ InputDim()

int32_t knf::FeatureTransform::InputDim ( ) const

Number of columns of the stored matrix.

This is the feature dimension in the linear form; an affine transform consumes InputDim()-1 features plus the implicit 1.0.

◆ IsAffine()

bool knf::FeatureTransform::IsAffine ( int32_t input_dim) const

True if the transform is affine for inputs of dimension input_dim.

The matrix alone cannot record the intended input dimension, so the rule is the one kaldi's transform-feats applies: the transform is affine iff it has one column more than the feature dimension ( \( n = D_{in} + 1 \)), the extra column being the offset.

Parameters
[in]input_dimThe input feature dimension.

◆ OutputDim()

int32_t knf::FeatureTransform::OutputDim ( ) const

Number of rows of the stored matrix (the output dimension).

◆ Read()

FeatureTransform knf::FeatureTransform::Read ( const std::string & filename)
static

Read a transform matrix from a kaldi matrix file.

Supports the kaldi binary format ("\0B" + "FM"/"DM" + dims + data, as written by kaldi's Matrix::Write) and the kaldi text format ([ ... ]).

Parameters
[in]filenamePath of the matrix file, e.g. lda.mat.
Returns
The transform.

The documentation for this class was generated from the following file:
  • /home/runner/work/kaldi-native-fbank/kaldi-native-fbank/kaldi-native-fbank/csrc/feature-transform.h