kaldi-native-fbank C++ API
Kaldi-compatible feature extraction C++ API
Loading...
Searching...
No Matches
online-cmvn.h
Go to the documentation of this file.
1// kaldi-native-fbank/kaldi-native-fbank/csrc/online-cmvn.h
2//
3// Copyright (c) 2026 Xiaomi Corporation
4//
6//
7// See ../../LICENSE for more information
8#ifndef KALDI_NATIVE_FBANK_CSRC_ONLINE_CMVN_H_
9#define KALDI_NATIVE_FBANK_CSRC_ONLINE_CMVN_H_
10
11#include <cstdint>
12#include <string>
13#include <vector>
14
16
17namespace knf {
18
32 int32_t cmn_window = 600;
36 int32_t speaker_frames = 600;
39 int32_t global_frames = 200;
42 bool normalize_mean = true;
46 bool normalize_variance = false;
50 int32_t modulus = 20;
53 int32_t ring_buffer_size = 20;
57 std::string skip_dims;
58
60 std::string ToString() const;
61};
62
90
131 public:
141 OnlineCmvn(const OnlineCmvnOptions &opts, const OnlineCmvnState &state);
142
144 int32_t Dim() const { return dim_; }
146 int32_t NumFramesAccepted() const { return num_frames_; }
147
158 VectorF AcceptFeature(const float *feat, int32_t dim);
161
169
178 void Freeze();
179
188
202 static void SmoothOnlineCmvnStats(const MatrixD &speaker_stats,
203 const MatrixD &global_stats,
204 const OnlineCmvnOptions &opts,
205 MatrixD *stats);
206
207 private:
208 void Init(const OnlineCmvnState &state);
209 // Update sliding stats with frame t (add) and frame t - cmn_window
210 // (subtract, if it exists), then cache the raw frame.
211 void UpdateStats(const VectorF &feat);
212 // Apply smoothed (or frozen) stats to one frame in place.
213 void NormalizeFrame(VectorF *feat) const;
214
215 OnlineCmvnOptions opts_;
216 std::vector<int32_t> skip_dims_;
217 int32_t dim_ = 0;
218 int32_t num_frames_ = 0;
219
220 // Sliding-window stats over the last <= cmn_window frames.
221 MatrixD stats_;
222 // Raw frame history for window subtraction: ring of cmn_window frames.
223 std::vector<VectorF> raw_ring_;
224
225 // Speaker stats of *previous* utterances, used for smoothing (kaldi's
226 // orig_state_.speaker_cmvn_stats). Fixed while frames are accepted.
227 MatrixD prev_speaker_cmvn_stats_;
228 // Speaker stats accumulated for GetState() (prev speaker stats + all
229 // frames accepted so far), like kaldi's OnlineCmvn::GetState.
230 MatrixD speaker_cmvn_stats_;
231 MatrixD global_cmvn_stats_;
232 MatrixD frozen_state_; // empty unless Freeze() was called
233};
234
235} // namespace knf
236
237#endif // KALDI_NATIVE_FBANK_CSRC_ONLINE_CMVN_H_
OnlineCmvn(const OnlineCmvnOptions &opts, const OnlineCmvnState &state)
Construct with options and adaptation state.
int32_t NumFramesAccepted() const
Number of frames accepted so far.
Definition online-cmvn.h:146
void Freeze()
Freeze the normalization at the current frame.
VectorF AcceptFeature(const float *feat, int32_t dim)
Push one raw feature frame; return the normalized frame.
VectorF AcceptFeature(const VectorF &feat)
Same as AcceptFeature(const float *, int32_t).
OnlineCmvnState GetState() const
Return the adaptation state for the next utterance.
MatrixF AcceptFeatures(const MatrixF &feats)
Push a batch of raw feature frames.
static void SmoothOnlineCmvnStats(const MatrixD &speaker_stats, const MatrixD &global_stats, const OnlineCmvnOptions &opts, MatrixD *stats)
Smooth window stats with speaker and global stats.
int32_t Dim() const
Feature dimension (dim of the global stats minus one).
Definition online-cmvn.h:144
kaldi-native-fbank/kaldi-native-fbank/csrc/feature-matrix.h
This file is part of kaldi-native-fbank.
Definition feature-cmvn.h:42
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
Eigen::VectorXf VectorF
1-D feature/aux vector (float).
Definition feature-matrix.h:35
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MatrixD
Double-precision accumulator/statistics matrix (e.g.
Definition feature-matrix.h:31
Options for OnlineCmvn, mirroring kaldi's OnlineCmvnOptions (kaldi/src/feat/online-feature....
Definition online-cmvn.h:28
int32_t global_frames
Number of frames of global-average stats to back off to further, when even the speaker stats cannot f...
Definition online-cmvn.h:39
bool normalize_mean
If true (default), subtract the running mean.
Definition online-cmvn.h:42
std::string ToString() const
Return the options as a string.
int32_t ring_buffer_size
Size of kaldi's ring buffer for cached stats snapshots.
Definition online-cmvn.h:53
int32_t modulus
How often kaldi snapshots stats internally (every modulus frames).
Definition online-cmvn.h:50
bool normalize_variance
If true, also divide by the running standard deviation (kaldi: –norm-vars).
Definition online-cmvn.h:46
int32_t cmn_window
Number of frames of sliding context for cepstral mean normalization (kaldi: –cmn-window).
Definition online-cmvn.h:32
int32_t speaker_frames
Number of frames of previous utterance(s) from this speaker to back off to at the beginning of an utt...
Definition online-cmvn.h:36
std::string skip_dims
Colon-separated list of dimensions to skip normalization of, e.g.
Definition online-cmvn.h:57
State of CMVN adaptation between utterances (not the within-utterance computation state).
Definition online-cmvn.h:73
OnlineCmvnState()=default
MatrixD frozen_state
If non-empty, 2 x (dim+1): the "frozen" normalization in use since OnlineCmvn::Freeze() was called (k...
Definition online-cmvn.h:83
OnlineCmvnState(const MatrixD &global_stats)
Construct with global stats (the required part of the state).
Definition online-cmvn.h:87
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.
Definition online-cmvn.h:76
MatrixD global_cmvn_stats
Global CMVN stats, 2 x (dim+1), e.g.
Definition online-cmvn.h:79