|
kaldi-native-fbank C++ API
Kaldi-compatible feature extraction C++ API
|
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. | |
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.
|
explicit |
Construct a transform from a matrix directly.
| [in] | mat | The transform matrix of shape (OutputDim() x InputDim()), or one column wider for an affine transform (the last column is the offset). |
| 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().
| [in] | feat | Pointer to one frame of dimension dim. |
| [in] | dim | Dimension of the frame. |
| [out] | out | Pointer to an array of OutputDim() floats; the caller must pre-allocate it. |
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.
| [in] | feats | Feature matrix of size T x D, float32. |
| 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.
| 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.
| [in] | input_dim | The input feature dimension. |
| int32_t knf::FeatureTransform::OutputDim | ( | ) | const |
Number of rows of the stored matrix (the output dimension).
|
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 ([ ... ]).
| [in] | filename | Path of the matrix file, e.g. lda.mat. |