MfccComputer Class Reference

#include <feature-mfcc.h>

Collaboration diagram for MfccComputer:

Public Types

typedef MfccOptions Options
 

Public Member Functions

 MfccComputer (const MfccOptions &opts)
 
 MfccComputer (const MfccComputer &other)
 
const FrameExtractionOptionsGetFrameOptions () const
 
int32 Dim () const
 
bool NeedRawLogEnergy () const
 
void Compute (BaseFloat signal_raw_log_energy, BaseFloat vtln_warp, VectorBase< BaseFloat > *signal_frame, VectorBase< BaseFloat > *feature)
 Function that computes one frame of features from one frame of signal. More...
 
 ~MfccComputer ()
 

Protected Member Functions

const MelBanksGetMelBanks (BaseFloat vtln_warp)
 

Protected Attributes

MfccOptions opts_
 
Vector< BaseFloatlifter_coeffs_
 
Matrix< BaseFloatdct_matrix_
 
BaseFloat log_energy_floor_
 
std::map< BaseFloat, MelBanks * > mel_banks_
 
SplitRadixRealFft< BaseFloat > * srfft_
 
Vector< BaseFloatmel_energies_
 

Private Member Functions

MfccComputeroperator= (const MfccComputer &in)
 

Detailed Description

Definition at line 87 of file feature-mfcc.h.

Member Typedef Documentation

◆ Options

Definition at line 89 of file feature-mfcc.h.

Constructor & Destructor Documentation

◆ MfccComputer() [1/2]

MfccComputer ( const MfccOptions opts)
explicit

Definition at line 82 of file feature-mfcc.cc.

References MfccOptions::cepstral_lifter, kaldi::ComputeDctMatrix(), kaldi::ComputeLifterCoeffs(), MatrixBase< Real >::CopyFromMat(), MfccComputer::dct_matrix_, MfccOptions::energy_floor, MfccOptions::frame_opts, MfccComputer::GetMelBanks(), KALDI_ERR, MfccComputer::lifter_coeffs_, kaldi::Log(), MfccComputer::log_energy_floor_, MfccOptions::mel_opts, MelBanksOptions::num_bins, MfccOptions::num_ceps, FrameExtractionOptions::PaddedWindowSize(), Matrix< Real >::Resize(), and MfccComputer::srfft_.

82  :
83  opts_(opts), srfft_(NULL),
84  mel_energies_(opts.mel_opts.num_bins) {
85 
86  int32 num_bins = opts.mel_opts.num_bins;
87  if (opts.num_ceps > num_bins)
88  KALDI_ERR << "num-ceps cannot be larger than num-mel-bins."
89  << " It should be smaller or equal. You provided num-ceps: "
90  << opts.num_ceps << " and num-mel-bins: "
91  << num_bins;
92 
93  Matrix<BaseFloat> dct_matrix(num_bins, num_bins);
94  ComputeDctMatrix(&dct_matrix);
95  // Note that we include zeroth dct in either case. If using the
96  // energy we replace this with the energy. This means a different
97  // ordering of features than HTK.
98  SubMatrix<BaseFloat> dct_rows(dct_matrix, 0, opts.num_ceps, 0, num_bins);
99  dct_matrix_.Resize(opts.num_ceps, num_bins);
100  dct_matrix_.CopyFromMat(dct_rows); // subset of rows.
101  if (opts.cepstral_lifter != 0.0) {
102  lifter_coeffs_.Resize(opts.num_ceps);
103  ComputeLifterCoeffs(opts.cepstral_lifter, &lifter_coeffs_);
104  }
105  if (opts.energy_floor > 0.0)
106  log_energy_floor_ = Log(opts.energy_floor);
107 
108  int32 padded_window_size = opts.frame_opts.PaddedWindowSize();
109  if ((padded_window_size & (padded_window_size-1)) == 0) // Is a power of two...
110  srfft_ = new SplitRadixRealFft<BaseFloat>(padded_window_size);
111 
112  // We'll definitely need the filterbanks info for VTLN warping factor 1.0.
113  // [note: this call caches it.]
114  GetMelBanks(1.0);
115 }
void ComputeDctMatrix(Matrix< Real > *M)
ComputeDctMatrix computes a matrix corresponding to the DCT, such that M * v equals the DCT of vector...
kaldi::int32 int32
const MelBanks * GetMelBanks(BaseFloat vtln_warp)
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
Vector< BaseFloat > mel_energies_
Definition: feature-mfcc.h:144
BaseFloat log_energy_floor_
Definition: feature-mfcc.h:138
double Log(double x)
Definition: kaldi-math.h:100
Matrix< BaseFloat > dct_matrix_
Definition: feature-mfcc.h:137
void ComputeLifterCoeffs(BaseFloat Q, VectorBase< BaseFloat > *coeffs)
#define KALDI_ERR
Definition: kaldi-error.h:147
Vector< BaseFloat > lifter_coeffs_
Definition: feature-mfcc.h:136
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
SplitRadixRealFft< BaseFloat > * srfft_
Definition: feature-mfcc.h:140

◆ MfccComputer() [2/2]

MfccComputer ( const MfccComputer other)

Definition at line 117 of file feature-mfcc.cc.

References MfccComputer::mel_banks_, and MfccComputer::srfft_.

117  :
118  opts_(other.opts_), lifter_coeffs_(other.lifter_coeffs_),
119  dct_matrix_(other.dct_matrix_),
120  log_energy_floor_(other.log_energy_floor_),
121  mel_banks_(other.mel_banks_),
122  srfft_(NULL),
123  mel_energies_(other.mel_energies_.Dim(), kUndefined) {
124  for (std::map<BaseFloat, MelBanks*>::iterator iter = mel_banks_.begin();
125  iter != mel_banks_.end(); ++iter)
126  iter->second = new MelBanks(*(iter->second));
127  if (other.srfft_ != NULL)
128  srfft_ = new SplitRadixRealFft<BaseFloat>(*(other.srfft_));
129 }
Vector< BaseFloat > mel_energies_
Definition: feature-mfcc.h:144
BaseFloat log_energy_floor_
Definition: feature-mfcc.h:138
std::map< BaseFloat, MelBanks * > mel_banks_
Definition: feature-mfcc.h:139
Matrix< BaseFloat > dct_matrix_
Definition: feature-mfcc.h:137
Vector< BaseFloat > lifter_coeffs_
Definition: feature-mfcc.h:136
SplitRadixRealFft< BaseFloat > * srfft_
Definition: feature-mfcc.h:140

◆ ~MfccComputer()

Definition at line 133 of file feature-mfcc.cc.

References MfccComputer::mel_banks_, and MfccComputer::srfft_.

133  {
134  for (std::map<BaseFloat, MelBanks*>::iterator iter = mel_banks_.begin();
135  iter != mel_banks_.end();
136  ++iter)
137  delete iter->second;
138  delete srfft_;
139 }
std::map< BaseFloat, MelBanks * > mel_banks_
Definition: feature-mfcc.h:139
SplitRadixRealFft< BaseFloat > * srfft_
Definition: feature-mfcc.h:140

Member Function Documentation

◆ Compute()

void Compute ( BaseFloat  signal_raw_log_energy,
BaseFloat  vtln_warp,
VectorBase< BaseFloat > *  signal_frame,
VectorBase< BaseFloat > *  feature 
)

Function that computes one frame of features from one frame of signal.

Parameters
[in]signal_raw_log_energyThe log-energy of the frame of the signal prior to windowing and pre-emphasis, or log(numeric_limits<float>::min()), whichever is greater. Must be ignored by this function if this class returns false from this->NeedsRawLogEnergy().
[in]vtln_warpThe VTLN warping factor that the user wants to be applied when computing features for this utterance. Will normally be 1.0, meaning no warping is to be done. The value will be ignored for feature types that don't support VLTN, such as spectrogram features.
[in]signal_frameOne frame of the signal, as extracted using the function ExtractWindow() using the options returned by this->GetFrameOptions(). The function will use the vector as a workspace, which is why it's a non-const pointer.
[out]featurePointer to a vector of size this->Dim(), to which the computed feature will be written.

Definition at line 28 of file feature-mfcc.cc.

References VectorBase< Real >::AddMatVec(), MfccOptions::cepstral_lifter, kaldi::ComputePowerSpectrum(), VectorBase< Real >::Data(), MfccComputer::dct_matrix_, VectorBase< Real >::Dim(), MfccComputer::Dim(), MfccOptions::energy_floor, MfccOptions::frame_opts, MfccComputer::GetMelBanks(), MfccOptions::htk_compat, rnnlm::i, KALDI_ASSERT, kaldi::kNoTrans, MfccComputer::lifter_coeffs_, kaldi::Log(), MfccComputer::log_energy_floor_, M_SQRT2, MfccComputer::mel_energies_, VectorBase< Real >::MulElements(), MfccOptions::num_ceps, MfccComputer::opts_, FrameExtractionOptions::PaddedWindowSize(), MfccOptions::raw_energy, kaldi::RealFft(), VectorBase< Real >::SetZero(), MfccComputer::srfft_, MfccOptions::use_energy, and kaldi::VecVec().

31  {
32  KALDI_ASSERT(signal_frame->Dim() == opts_.frame_opts.PaddedWindowSize() &&
33  feature->Dim() == this->Dim());
34 
35  const MelBanks &mel_banks = *(GetMelBanks(vtln_warp));
36 
38  signal_raw_log_energy = Log(std::max<BaseFloat>(VecVec(*signal_frame, *signal_frame),
39  std::numeric_limits<float>::epsilon()));
40 
41  if (srfft_ != NULL) // Compute FFT using the split-radix algorithm.
42  srfft_->Compute(signal_frame->Data(), true);
43  else // An alternative algorithm that works for non-powers-of-two.
44  RealFft(signal_frame, true);
45 
46  // Convert the FFT into a power spectrum.
47  ComputePowerSpectrum(signal_frame);
48  SubVector<BaseFloat> power_spectrum(*signal_frame, 0,
49  signal_frame->Dim() / 2 + 1);
50 
51  mel_banks.Compute(power_spectrum, &mel_energies_);
52 
53  // avoid log of zero (which should be prevented anyway by dithering).
54  mel_energies_.ApplyFloor(std::numeric_limits<float>::epsilon());
55  mel_energies_.ApplyLog(); // take the log.
56 
57  feature->SetZero(); // in case there were NaNs.
58  // feature = dct_matrix_ * mel_energies [which now have log]
59  feature->AddMatVec(1.0, dct_matrix_, kNoTrans, mel_energies_, 0.0);
60 
61  if (opts_.cepstral_lifter != 0.0)
62  feature->MulElements(lifter_coeffs_);
63 
64  if (opts_.use_energy) {
65  if (opts_.energy_floor > 0.0 && signal_raw_log_energy < log_energy_floor_)
66  signal_raw_log_energy = log_energy_floor_;
67  (*feature)(0) = signal_raw_log_energy;
68  }
69 
70  if (opts_.htk_compat) {
71  BaseFloat energy = (*feature)(0);
72  for (int32 i = 0; i < opts_.num_ceps - 1; i++)
73  (*feature)(i) = (*feature)(i+1);
74  if (!opts_.use_energy)
75  energy *= M_SQRT2; // scale on C0 (actually removing a scale
76  // we previously added that's part of one common definition of
77  // the cosine transform.)
78  (*feature)(opts_.num_ceps - 1) = energy;
79  }
80 }
kaldi::int32 int32
const MelBanks * GetMelBanks(BaseFloat vtln_warp)
Vector< BaseFloat > mel_energies_
Definition: feature-mfcc.h:144
BaseFloat energy_floor
Definition: feature-mfcc.h:43
BaseFloat cepstral_lifter
Definition: feature-mfcc.h:46
BaseFloat log_energy_floor_
Definition: feature-mfcc.h:138
float BaseFloat
Definition: kaldi-types.h:29
#define M_SQRT2
Definition: kaldi-math.h:48
double Log(double x)
Definition: kaldi-math.h:100
FrameExtractionOptions frame_opts
Definition: feature-mfcc.h:39
Matrix< BaseFloat > dct_matrix_
Definition: feature-mfcc.h:137
void ComputePowerSpectrum(VectorBase< BaseFloat > *waveform)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Vector< BaseFloat > lifter_coeffs_
Definition: feature-mfcc.h:136
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
Definition: kaldi-vector.cc:37
SplitRadixRealFft< BaseFloat > * srfft_
Definition: feature-mfcc.h:140
void RealFft(VectorBase< Real > *v, bool forward)
RealFft is a fourier transform of real inputs.
int32 Dim() const
Definition: feature-mfcc.h:97

◆ Dim()

int32 Dim ( ) const
inline

Definition at line 97 of file feature-mfcc.h.

Referenced by MfccComputer::Compute().

97 { return opts_.num_ceps; }

◆ GetFrameOptions()

const FrameExtractionOptions& GetFrameOptions ( ) const
inline

Definition at line 93 of file feature-mfcc.h.

93  {
94  return opts_.frame_opts;
95  }
FrameExtractionOptions frame_opts
Definition: feature-mfcc.h:39

◆ GetMelBanks()

const MelBanks * GetMelBanks ( BaseFloat  vtln_warp)
protected

Definition at line 141 of file feature-mfcc.cc.

References MfccOptions::frame_opts, MfccComputer::mel_banks_, MfccOptions::mel_opts, and MfccComputer::opts_.

Referenced by MfccComputer::Compute(), and MfccComputer::MfccComputer().

141  {
142  MelBanks *this_mel_banks = NULL;
143  std::map<BaseFloat, MelBanks*>::iterator iter = mel_banks_.find(vtln_warp);
144  if (iter == mel_banks_.end()) {
145  this_mel_banks = new MelBanks(opts_.mel_opts,
147  vtln_warp);
148  mel_banks_[vtln_warp] = this_mel_banks;
149  } else {
150  this_mel_banks = iter->second;
151  }
152  return this_mel_banks;
153 }
std::map< BaseFloat, MelBanks * > mel_banks_
Definition: feature-mfcc.h:139
MelBanksOptions mel_opts
Definition: feature-mfcc.h:40
FrameExtractionOptions frame_opts
Definition: feature-mfcc.h:39

◆ NeedRawLogEnergy()

bool NeedRawLogEnergy ( ) const
inline

Definition at line 99 of file feature-mfcc.h.

◆ operator=()

MfccComputer& operator= ( const MfccComputer in)
private

Member Data Documentation

◆ dct_matrix_

Matrix<BaseFloat> dct_matrix_
protected

Definition at line 137 of file feature-mfcc.h.

Referenced by MfccComputer::Compute(), and MfccComputer::MfccComputer().

◆ lifter_coeffs_

Vector<BaseFloat> lifter_coeffs_
protected

Definition at line 136 of file feature-mfcc.h.

Referenced by MfccComputer::Compute(), and MfccComputer::MfccComputer().

◆ log_energy_floor_

BaseFloat log_energy_floor_
protected

Definition at line 138 of file feature-mfcc.h.

Referenced by MfccComputer::Compute(), and MfccComputer::MfccComputer().

◆ mel_banks_

std::map<BaseFloat, MelBanks*> mel_banks_
protected

◆ mel_energies_

Vector<BaseFloat> mel_energies_
protected

Definition at line 144 of file feature-mfcc.h.

Referenced by MfccComputer::Compute().

◆ opts_

MfccOptions opts_
protected

Definition at line 135 of file feature-mfcc.h.

Referenced by MfccComputer::Compute(), and MfccComputer::GetMelBanks().

◆ srfft_


The documentation for this class was generated from the following files: