CMVN
Full doxygen reference (auto-generated from the headers): C++ API doxygen
Header: kaldi-native-fbank/csrc/feature-cmvn.h
Semantics follow kaldi's transform/cmvn.cc exactly (same arithmetic and stats layout).
knf::MatrixF feats = ...; // rows = frames
// full 2-row stats: row0 = [sums, count], row1 = [sumsq, (unused)]
knf::MatrixD stats = knf::ComputeCmvnStats(feats);
knf::CmvnOptions opts; // norm_vars=false, reverse=false
knf::MatrixF out = knf::ApplyCmvn(stats, opts, feats);
// incremental
knf::CmvnAccumulator acc(feats.cols());
acc.Accumulate(feats);
const knf::MatrixD &s = acc.Stats();
API (doxygen)
-
struct CmvnOptions
Options matching kaldi's apply-cmvn flags.
See also
- Used by:
Public Members
-
bool norm_vars = false
If true, normalize the variances as well: after subtracting the mean, each dimension is divided by its standard deviation.
Requires 2-row stats (kaldi apply-cmvn
--norm-vars).
-
bool reverse = false
If true, undo cmvn instead of applying it: the mean is added back and, if norm_vars is true, the standard deviation is multiplied back (kaldi apply-cmvn
--reverse).
-
class CmvnAccumulator
Incremental accumulator for speaker-level / streaming cmvn.
It always keeps the full 2-row layout (see InitCmvnStats()) and adds up weighted frames one by one, so stats can be collected over many utterances of the same speaker and normalized later with ApplyCmvn().
See also
- Used by:
Pipelines that collect speaker-level stats incrementally (e.g. accumulate all utterances of a speaker, then normalize).
Public Functions
-
explicit CmvnAccumulator(int32_t dim = 0)
Construct an accumulator for features of dimension
dim.- Parameters:
dim -- Feature dimension; 0 (the default) creates an empty accumulator whose stats are grown by the first Add().
-
void Accumulate(const MatrixF &feats)
Accumulate stats for a feature matrix (rows = frames); equivalent to calling Add() for each row with weight 1.0.
-
void Add(int32_t dim, const float *frame, float weight = 1.0f)
Accumulate stats for one frame of dimension
dim, applying the same updates as the free function AccCmvnStats().- Parameters:
dim -- Feature dimension; must match the dimension given to the constructor.
frame -- Pointer to one frame, a 1-D array of size dim.
weight -- Weight of this frame, e.g. 1.0.
-
inline const MatrixD &Stats() const
Return the accumulated 2-row stats: row 0 = [sums, count], row 1 = [sumsq, (unused)]; see InitCmvnStats() for the layout.
-
void knf::InitCmvnStats(int32_t dim, MatrixD *stats)
Initialize cmvn stats to zeros in the FULL 2-row layout.
The output matrix
statsis resized to 2 x (dim+1) and set to zero. Its layout matches kaldi's cmvn.ark format exactly:\[\begin{split} \mathrm{stats} = \left( \begin{array}{cccc|c} S_1 & S_2 & \cdots & S_D & c \\ Q_1 & Q_2 & \cdots & Q_D & - \end{array} \right) \end{split}\]Row 0 (above): columns 0 .. dim-1 hold the sums \( S_d = \sum_t x_t(d) \) of each feature dimension; the last column \( c \) holds the frame count (sum of weights).
Row 1 (below): columns 0 .. dim-1 hold the sums of squares \( Q_d = \sum_t x_t(d)^2 \); the last column is unused.
This is the form produced/consumed by AccCmvnStats(), ComputeCmvnStats() and ApplyCmvn() with norm_vars=true.
Note: ApplyCmvn() also accepts 1-row (mean-only) stats, where only row 0 above is present; this function, however, only produces the 2-row form used for accumulating both mean and variance.
knf::MatrixD stats; knf::InitCmvnStats(39, &stats); // stats: 2 x 40, all zeros
See also
- Parameters:
dim -- [in] Feature dimension of the frames that will be accumulated, e.g. 39 for 39-dim MFCC.
stats -- [out] The output stats matrix, resized to 2 x (dim+1).
-
void knf::AccCmvnStats(const float *feat, int32_t dim, float weight, MatrixD *stats)
Accumulate stats for one frame (kaldi AccCmvnStats, same op order).
The following updates are applied to
stats, which must use the full 2-row layout (see InitCmvnStats()); with \( w = \)weightand \( x_d = \)feat[d]:\[ c \mathrel{+}= w, \qquad S_d \mathrel{+}= x_d\, w, \qquad Q_d \mathrel{+}= x_d^2\, w \qquad (0 \le d < \mathrm{dim}) \]knf::MatrixD stats; knf::InitCmvnStats(dim, &stats); for (int32_t t = 0; t < num_frames; ++t) { knf::AccCmvnStats(feats.row(t).data(), dim, 1.0f, &stats); }
See also
- Parameters:
feat -- [in] Pointer to one frame, a 1-D array of size dim.
dim -- [in] Feature dimension of the frame; must equal stats->cols() - 1.
weight -- [in] Weight of this frame, e.g. 1.0.
stats -- [inout] Stats to update; must use the full 2-row layout.
-
void knf::AccCmvnStats(const MatrixF &feats, MatrixD *stats)
Accumulate stats for a feature matrix (rows = frames).
This is equivalent to calling the single-frame AccCmvnStats() above for every row of
feats, with weight 1.0.See also
- Parameters:
feats -- [in] Feature matrix of size num_frames x dim.
stats -- [inout] Stats to update; must use the full 2-row layout.
-
MatrixD knf::ComputeCmvnStats(const MatrixF &feats)
Compute full 2-row cmvn stats for a whole matrix.
Equivalent to InitCmvnStats() followed by AccCmvnStats() over all rows.
See also
- Parameters:
feats -- [in] Feature matrix of size num_frames x dim.
- Returns:
The stats matrix of size 2 x (dim+1); see InitCmvnStats() for the layout. The frame count in stats(0, dim) is num_frames.
-
void knf::ApplyCmvn(const MatrixD &stats, const CmvnOptions &opts, MatrixF *feats)
Apply cmvn to feats in place (kaldi ApplyCmvn / ApplyCmvnReverse).
Let \( c = \mathrm{stats}(0, \mathrm{dim}) \) be the frame count, \( \mu_d = \mathrm{stats}(0, d) / c \) the mean and, when norm_vars is true, \( \sigma_d^2 = \mathrm{stats}(1, d) / c - \mu_d^2 \) the variance of dimension \( d \).
Forward (opts.reverse == false), applied to every frame \( x \):
\[ x_d \leftarrow x_d - \mu_d \]and, if opts.norm_vars:\[ x_d \leftarrow (x_d - \mu_d) / \sigma_d \]Reverse (opts.reverse == true) undoes the normalization:
\[ x_d \leftarrow x_d + \mu_d \]and, if opts.norm_vars:\[ x_d \leftarrow x_d \cdot \sigma_d + \mu_d \]Accepts 1-row (mean-only) or 2-row stats; requesting norm_vars with 1-row stats is an error (same as kaldi).
knf::MatrixD stats = knf::ComputeCmvnStats(feats); knf::CmvnOptions opts; // norm_vars=false, reverse=false knf::ApplyCmvn(stats, opts, &feats); // feats normalized in place
See also
- Parameters:
stats -- [in] 1-row or 2-row cmvn stats; see InitCmvnStats().
opts -- [in] Options: norm_vars (normalize variances), reverse (undo cmvn).
feats -- [inout] Feature matrix of size num_frames x dim; each row is normalized in place.
-
MatrixF knf::ApplyCmvn(const MatrixD &stats, const CmvnOptions &opts, const MatrixF &feats)
Convenience version of ApplyCmvn(): returns the normalized copy.
Same semantics as the in-place ApplyCmvn() above, but
featsis left unchanged and a normalized copy is returned.- Parameters:
stats -- [in] 1-row or 2-row cmvn stats; see InitCmvnStats().
opts -- [in] Options: norm_vars (normalize variances), reverse (undo cmvn).
feats -- [in] Feature matrix of size num_frames x dim.
- Returns:
A copy of feats with cmvn applied.