FbankComputer Class Reference

Class for computing mel-filterbank features; see Computing MFCC features for more information. More...

#include <feature-fbank.h>

Collaboration diagram for FbankComputer:

Public Types

typedef FbankOptions Options
 

Public Member Functions

 FbankComputer (const FbankOptions &opts)
 
 FbankComputer (const FbankComputer &other)
 
int32 Dim () const
 
bool NeedRawLogEnergy () const
 
const FrameExtractionOptionsGetFrameOptions () 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...
 
 ~FbankComputer ()
 

Private Member Functions

const MelBanksGetMelBanks (BaseFloat vtln_warp)
 
FbankComputeroperator= (const FbankComputer &other)
 

Private Attributes

FbankOptions opts_
 
BaseFloat log_energy_floor_
 
std::map< BaseFloat, MelBanks * > mel_banks_
 
SplitRadixRealFft< BaseFloat > * srfft_
 

Detailed Description

Class for computing mel-filterbank features; see Computing MFCC features for more information.

Definition at line 86 of file feature-fbank.h.

Member Typedef Documentation

◆ Options

Definition at line 88 of file feature-fbank.h.

Constructor & Destructor Documentation

◆ FbankComputer() [1/2]

FbankComputer ( const FbankOptions opts)
explicit

Definition at line 26 of file feature-fbank.cc.

References FbankOptions::energy_floor, FbankOptions::frame_opts, FbankComputer::GetMelBanks(), kaldi::Log(), FbankComputer::log_energy_floor_, FrameExtractionOptions::PaddedWindowSize(), and FbankComputer::srfft_.

26  :
27  opts_(opts), srfft_(NULL) {
28  if (opts.energy_floor > 0.0)
29  log_energy_floor_ = Log(opts.energy_floor);
30 
31  int32 padded_window_size = opts.frame_opts.PaddedWindowSize();
32  if ((padded_window_size & (padded_window_size-1)) == 0) // Is a power of two...
33  srfft_ = new SplitRadixRealFft<BaseFloat>(padded_window_size);
34 
35  // We'll definitely need the filterbanks info for VTLN warping factor 1.0.
36  // [note: this call caches it.]
37  GetMelBanks(1.0);
38 }
kaldi::int32 int32
double Log(double x)
Definition: kaldi-math.h:100
SplitRadixRealFft< BaseFloat > * srfft_
BaseFloat log_energy_floor_
const MelBanks * GetMelBanks(BaseFloat vtln_warp)

◆ FbankComputer() [2/2]

FbankComputer ( const FbankComputer other)

Definition at line 40 of file feature-fbank.cc.

References FbankComputer::mel_banks_, and FbankComputer::srfft_.

40  :
41  opts_(other.opts_), log_energy_floor_(other.log_energy_floor_),
42  mel_banks_(other.mel_banks_), srfft_(NULL) {
43  for (std::map<BaseFloat, MelBanks*>::iterator iter = mel_banks_.begin();
44  iter != mel_banks_.end();
45  ++iter)
46  iter->second = new MelBanks(*(iter->second));
47  if (other.srfft_)
48  srfft_ = new SplitRadixRealFft<BaseFloat>(*(other.srfft_));
49 }
SplitRadixRealFft< BaseFloat > * srfft_
BaseFloat log_energy_floor_
std::map< BaseFloat, MelBanks * > mel_banks_

◆ ~FbankComputer()

Definition at line 51 of file feature-fbank.cc.

References FbankComputer::mel_banks_, and FbankComputer::srfft_.

51  {
52  for (std::map<BaseFloat, MelBanks*>::iterator iter = mel_banks_.begin();
53  iter != mel_banks_.end(); ++iter)
54  delete iter->second;
55  delete srfft_;
56 }
SplitRadixRealFft< BaseFloat > * srfft_
std::map< BaseFloat, MelBanks * > mel_banks_

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 72 of file feature-fbank.cc.

References VectorBase< Real >::ApplyFloor(), VectorBase< Real >::ApplyLog(), VectorBase< Real >::ApplyPow(), MelBanks::Compute(), kaldi::ComputePowerSpectrum(), VectorBase< Real >::Data(), VectorBase< Real >::Dim(), FbankComputer::Dim(), FbankOptions::energy_floor, FbankOptions::frame_opts, FbankComputer::GetMelBanks(), FbankOptions::htk_compat, KALDI_ASSERT, kaldi::Log(), FbankComputer::log_energy_floor_, FbankOptions::mel_opts, MelBanksOptions::num_bins, FbankComputer::opts_, FrameExtractionOptions::PaddedWindowSize(), FbankOptions::raw_energy, kaldi::RealFft(), FbankComputer::srfft_, FbankOptions::use_energy, FbankOptions::use_log_fbank, FbankOptions::use_power, and kaldi::VecVec().

75  {
76 
77  const MelBanks &mel_banks = *(GetMelBanks(vtln_warp));
78 
79  KALDI_ASSERT(signal_frame->Dim() == opts_.frame_opts.PaddedWindowSize() &&
80  feature->Dim() == this->Dim());
81 
82 
83  // Compute energy after window function (not the raw one).
85  signal_raw_log_energy = Log(std::max<BaseFloat>(VecVec(*signal_frame, *signal_frame),
86  std::numeric_limits<float>::epsilon()));
87 
88  if (srfft_ != NULL) // Compute FFT using split-radix algorithm.
89  srfft_->Compute(signal_frame->Data(), true);
90  else // An alternative algorithm that works for non-powers-of-two.
91  RealFft(signal_frame, true);
92 
93  // Convert the FFT into a power spectrum.
94  ComputePowerSpectrum(signal_frame);
95  SubVector<BaseFloat> power_spectrum(*signal_frame, 0,
96  signal_frame->Dim() / 2 + 1);
97 
98  // Use magnitude instead of power if requested.
99  if (!opts_.use_power)
100  power_spectrum.ApplyPow(0.5);
101 
102  int32 mel_offset = ((opts_.use_energy && !opts_.htk_compat) ? 1 : 0);
103  SubVector<BaseFloat> mel_energies(*feature,
104  mel_offset,
106 
107  // Sum with mel fiterbanks over the power spectrum
108  mel_banks.Compute(power_spectrum, &mel_energies);
109  if (opts_.use_log_fbank) {
110  // Avoid log of zero (which should be prevented anyway by dithering).
111  mel_energies.ApplyFloor(std::numeric_limits<float>::epsilon());
112  mel_energies.ApplyLog(); // take the log.
113  }
114 
115  // Copy energy as first value (or the last, if htk_compat == true).
116  if (opts_.use_energy) {
117  if (opts_.energy_floor > 0.0 && signal_raw_log_energy < log_energy_floor_) {
118  signal_raw_log_energy = log_energy_floor_;
119  }
120  int32 energy_index = opts_.htk_compat ? opts_.mel_opts.num_bins : 0;
121  (*feature)(energy_index) = signal_raw_log_energy;
122  }
123 }
kaldi::int32 int32
FrameExtractionOptions frame_opts
Definition: feature-fbank.h:42
double Log(double x)
Definition: kaldi-math.h:100
BaseFloat energy_floor
Definition: feature-fbank.h:45
int32 Dim() const
Definition: feature-fbank.h:93
SplitRadixRealFft< BaseFloat > * srfft_
void ComputePowerSpectrum(VectorBase< BaseFloat > *waveform)
BaseFloat log_energy_floor_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MelBanksOptions mel_opts
Definition: feature-fbank.h:43
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
Definition: kaldi-vector.cc:37
const MelBanks * GetMelBanks(BaseFloat vtln_warp)
void RealFft(VectorBase< Real > *v, bool forward)
RealFft is a fourier transform of real inputs.

◆ Dim()

int32 Dim ( ) const
inline

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

Referenced by FbankComputer::Compute().

93  {
94  return opts_.mel_opts.num_bins + (opts_.use_energy ? 1 : 0);
95  }
MelBanksOptions mel_opts
Definition: feature-fbank.h:43

◆ GetFrameOptions()

const FrameExtractionOptions& GetFrameOptions ( ) const
inline

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

99  {
100  return opts_.frame_opts;
101  }
FrameExtractionOptions frame_opts
Definition: feature-fbank.h:42

◆ GetMelBanks()

const MelBanks * GetMelBanks ( BaseFloat  vtln_warp)
private

Definition at line 58 of file feature-fbank.cc.

References FbankOptions::frame_opts, FbankComputer::mel_banks_, FbankOptions::mel_opts, and FbankComputer::opts_.

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

58  {
59  MelBanks *this_mel_banks = NULL;
60  std::map<BaseFloat, MelBanks*>::iterator iter = mel_banks_.find(vtln_warp);
61  if (iter == mel_banks_.end()) {
62  this_mel_banks = new MelBanks(opts_.mel_opts,
64  vtln_warp);
65  mel_banks_[vtln_warp] = this_mel_banks;
66  } else {
67  this_mel_banks = iter->second;
68  }
69  return this_mel_banks;
70 }
FrameExtractionOptions frame_opts
Definition: feature-fbank.h:42
MelBanksOptions mel_opts
Definition: feature-fbank.h:43
std::map< BaseFloat, MelBanks * > mel_banks_

◆ NeedRawLogEnergy()

bool NeedRawLogEnergy ( ) const
inline

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

◆ operator=()

FbankComputer& operator= ( const FbankComputer other)
private

Member Data Documentation

◆ log_energy_floor_

BaseFloat log_energy_floor_
private

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

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

◆ mel_banks_

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

◆ opts_

FbankOptions opts_
private

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

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

◆ srfft_


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