Transform

Full doxygen reference (auto-generated from the headers): C++ API doxygen

Header: kaldi-native-fbank/csrc/feature-transform.h — the equivalent of kaldi's transform-feats (transform-feats).

knf::FeatureTransform lda = knf::FeatureTransform::Read("lda.mat");
knf::MatrixF out = lda.Apply(feats);   // (T, lda.OutputDim())

API (doxygen)

class FeatureTransform

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::FeatureTransform lda = knf::FeatureTransform::Read("lda.mat");
knf::MatrixF out = lda.Apply(feats);

See also

Read, Apply

Used by:

Pipelines that reduce/rotate features (mfcc -> cmvn -> splice -> lda).

Public Functions

explicit FeatureTransform(const MatrixF &mat)

Construct a transform from a matrix directly.

Parameters:

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

int32_t 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.

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.

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:

input_dim -- [in] The input feature dimension.

MatrixF 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:

feats -- [in] Feature matrix of size T x D, float32.

Returns:

The transformed matrix of size T x OutputDim().

void 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:
  • feat -- [in] Pointer to one frame of dimension dim.

  • dim -- [in] Dimension of the frame.

  • out -- [out] Pointer to an array of OutputDim() floats; the caller must pre-allocate it.

Public Static Functions

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

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:

filename -- [in] Path of the matrix file, e.g. lda.mat.

Returns:

The transform.