Online CMVN

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

Header: kaldi-native-fbank/csrc/online-cmvn.h

Running (online) cepstral mean/variance normalization — a port of kaldi's OnlineCmvn (feat/online-feature.h), push-style: feed raw feature frames, get normalized frames back. See Online CMVN for the semantics, formulas and options.

knf::OnlineCmvnOptions opts;              // kaldi defaults
knf::OnlineCmvnState state(global_stats); // MatrixD [2, dim+1]
knf::OnlineCmvn cmvn(opts, state);

for (const knf::VectorF &frame : frames) {
  knf::VectorF y = cmvn.AcceptFeature(frame);   // normalized
}
cmvn.Freeze();                          // fix the normalization
knf::OnlineCmvnState s = cmvn.GetState();  // hand over to next utterance

API (doxygen)

struct OnlineCmvnOptions

Options for OnlineCmvn, mirroring kaldi's OnlineCmvnOptions (kaldi/src/feat/online-feature.h).

The defaults match kaldi's.

The options control two things: the sliding window of the running statistics, and how the sparse stats at the start of an utterance are backed off ("smoothed") with speaker and global stats. See OnlineCmvn::SmoothOnlineCmvnStats for the smoothing formulas.

Public Functions

std::string ToString() const

Return the options as a string.

Public Members

int32_t cmn_window = 600

Number of frames of sliding context for cepstral mean normalization (kaldi: –cmn-window).

In the steady state the normalization of frame t uses the stats of frames t-cmn_window+1 .. t.

int32_t speaker_frames = 600

Number of frames of previous utterance(s) from this speaker to back off to at the beginning of an utterance (kaldi: –speaker-frames).

Should be <= cmn_window; values above it are clamped at use time.

int32_t global_frames = 200

Number of frames of global-average stats to back off to further, when even the speaker stats cannot fill the window (kaldi: –global-frames).

bool normalize_mean = true

If true (default), subtract the running mean.

Must be true when normalize_variance is true (kaldi: –norm-means).

bool normalize_variance = false

If true, also divide by the running standard deviation (kaldi: –norm-vars).

Off by default; enable only if the features were trained with variance normalization.

int32_t modulus = 20

How often kaldi snapshots stats internally (every modulus frames).

Kept for configuration compatibility; this push-style port updates the stats incrementally, so the value does not affect results.

int32_t ring_buffer_size = 20

Size of kaldi's ring buffer for cached stats snapshots.

Kept for configuration compatibility (see modulus); must be >= modulus.

std::string skip_dims

Colon-separated list of dimensions to skip normalization of, e.g.

"13:14:15" (kaldi: –skip-dims). For skipped dims the stats are faked to mean 0 / variance 1, so the frame values pass through unchanged.

struct OnlineCmvnState

State of CMVN adaptation between utterances (not the within-utterance computation state).

Stats matrices follow kaldi's layout: 2 x (dim+1) as [ sum-stats count ; sum-squared-stats 0 ]. Pass the state of one utterance to a new OnlineCmvn to continue with the next utterance of the same speaker; the speaker stats then back off the normalization at the start of that utterance (see OnlineCmvn for the smoothing rules).

Public Functions

inline explicit OnlineCmvnState(const MatrixD &global_stats)

Construct with global stats (the required part of the state).

Public Members

MatrixD speaker_cmvn_stats

Total CMVN stats for this speaker up till now, 2 x (dim+1); may be empty at the very first utterance.

MatrixD global_cmvn_stats

Global CMVN stats, 2 x (dim+1), e.g.

loaded from kaldi's global_cmvn.stats. Required by OnlineCmvn.

MatrixD frozen_state

If non-empty, 2 x (dim+1): the "frozen" normalization in use since OnlineCmvn::Freeze() was called (kaldi uses this to estimate fMLLR on top of a fixed CMVN).

Carries over to the next utterance.

class OnlineCmvn

Online (running) cepstral mean and optional variance normalization.

A port of kaldi's OnlineCmvn (kaldi/src/feat/online-feature.h), push-style: feed raw feature frames one by one and get the normalized frames back (kaldi's version is a pull-style filter in an OnlineFeatureInterface chain).

Unlike the offline CMVN of feature-cmvn.h (which uses the whole utterance), this class looks only at the past. In the middle of a long utterance it accumulates stats from the previous cmn_window frames (default 600, i.e. 6 seconds) and normalizes the current frame with them. At the beginning of an utterance the window stats are augmented ("smoothed") with up to speaker_frames frames of speaker stats from previous utterances and up to global_frames frames of global stats, so the first frames are normalized against a reasonable distribution.

The frame being normalized is included in the stats that normalize it (exactly like kaldi). Variance normalization floors the variance at 1e-20.

Example:

knf::OnlineCmvnOptions opts;
knf::OnlineCmvnState state(global_stats);  // MatrixD [2, dim+1]
knf::OnlineCmvn cmvn(opts, state);
for (const knf::VectorF &f : frames) {
knf::VectorF y = cmvn.AcceptFeature(f);
}
auto next_state = cmvn.GetState();  // hand over to the next utterance

Note

Features normalized this way are only approximately zero-mean, especially at utterance start. Acoustic models must be trained with the same kind of normalization ("matched" online training).

Note

Parity with kaldi is tested frame-by-frame against apply-cmvn-online in tests/parity/parity_online_cmvn.py.

Public Functions

OnlineCmvn(const OnlineCmvnOptions &opts, const OnlineCmvnState &state)

Construct with options and adaptation state.

Parameters:
  • opts -- [in] The options (defaults match kaldi).

  • state -- [in] Adaptation state; state.global_cmvn_stats must be a 2 x (dim+1) matrix. speaker_cmvn_stats and frozen_state may be empty; if frozen_state is set, the normalization is frozen from the start (see Freeze()).

inline int32_t Dim() const

Feature dimension (dim of the global stats minus one).

inline int32_t NumFramesAccepted() const

Number of frames accepted so far.

VectorF AcceptFeature(const float *feat, int32_t dim)

Push one raw feature frame; return the normalized frame.

The frame is normalized with stats that include the frame itself (like kaldi). The raw frame also updates the speaker stats used by GetState().

Parameters:
  • feat -- [in] Pointer to dim raw feature values.

  • dim -- [in] Feature dimension (must equal Dim()).

Returns:

The normalized frame, same dimension.

VectorF AcceptFeature(const VectorF &feat)

Same as AcceptFeature(const float *, int32_t).

MatrixF AcceptFeatures(const MatrixF &feats)

Push a batch of raw feature frames.

Parameters:

feats -- [in] Raw features, rows are frames, [T, dim].

Returns:

The normalized features, [T, dim].

void Freeze()

Freeze the normalization at the current frame.

From now on frames are normalized with the current (smoothed) stats and the running stats stop updating. The frozen state is part of GetState() so later utterances of the speaker keep the same normalization (kaldi uses this to estimate fMLLR on top of a fixed CMVN).

OnlineCmvnState GetState() const

Return the adaptation state for the next utterance.

Contains the speaker stats accumulated over all accepted frames (plus any incoming speaker stats), the global stats, and any frozen state — equivalent to kaldi's OnlineCmvn::GetState(cur_frame).

Public Static Functions

static void SmoothOnlineCmvnStats(const MatrixD &speaker_stats, const MatrixD &global_stats, const OnlineCmvnOptions &opts, MatrixD *stats)

Smooth window stats with speaker and global stats.

A port of kaldi's static OnlineCmvn::SmoothOnlineCmvnStats. Adds up to speaker_frames worth of speaker stats and up to global_frames worth of global stats to stats, scaled by their counts, so the total count approaches cmn_window (a no-op once it is reached).

Parameters:
  • speaker_stats -- [in] Speaker stats [2, dim+1], may be empty.

  • global_stats -- [in] Global stats [2, dim+1] (required at runtime).

  • opts -- [in] The options in use.

  • stats -- [in,out] Window stats to smooth.