OnlineGenericBaseFeature< C > Class Template Reference

This is a templated class for online feature extraction; it's templated on a class like MfccComputer or PlpComputer that does the basic feature extraction. More...

#include <online-feature.h>

Inheritance diagram for OnlineGenericBaseFeature< C >:
Collaboration diagram for OnlineGenericBaseFeature< C >:

Public Member Functions

virtual int32 Dim () const
 
virtual bool IsLastFrame (int32 frame) const
 Returns true if this is the last frame. More...
 
virtual BaseFloat FrameShiftInSeconds () const
 
virtual int32 NumFramesReady () const
 returns the feature dimension. More...
 
virtual void GetFrame (int32 frame, VectorBase< BaseFloat > *feat)
 Gets the feature vector for this frame. More...
 
 OnlineGenericBaseFeature (const typename C::Options &opts)
 
virtual void AcceptWaveform (BaseFloat sampling_rate, const VectorBase< BaseFloat > &waveform)
 This would be called from the application, when you get more wave data. More...
 
virtual void InputFinished ()
 InputFinished() tells the class you won't be providing any more waveform. More...
 
- Public Member Functions inherited from OnlineFeatureInterface
virtual void GetFrames (const std::vector< int32 > &frames, MatrixBase< BaseFloat > *feats)
 This is like GetFrame() but for a collection of frames. More...
 
virtual ~OnlineFeatureInterface ()
 Virtual destructor. More...
 

Private Member Functions

void ComputeFeatures ()
 
void MaybeCreateResampler (BaseFloat sampling_rate)
 

Private Attributes

computer_
 
std::unique_ptr< LinearResampleresampler_
 
FeatureWindowFunction window_function_
 
RecyclingVector features_
 
bool input_finished_
 
BaseFloat sampling_frequency_
 
int64 waveform_offset_
 
Vector< BaseFloatwaveform_remainder_
 

Detailed Description

template<class C>
class kaldi::OnlineGenericBaseFeature< C >

This is a templated class for online feature extraction; it's templated on a class like MfccComputer or PlpComputer that does the basic feature extraction.

Definition at line 78 of file online-feature.h.

Constructor & Destructor Documentation

◆ OnlineGenericBaseFeature()

OnlineGenericBaseFeature ( const typename C::Options &  opts)
explicit

Definition at line 70 of file online-feature.cc.

References KALDI_ASSERT.

71  :
72  computer_(opts), window_function_(computer_.GetFrameOptions()),
73  features_(opts.frame_opts.max_feature_vectors),
75  // RE the following assert: search for ONLINE_IVECTOR_LIMIT in
76  // online-ivector-feature.cc.
77  // Casting to uint32, an unsigned type, means that -1 would be treated
78  // as `very large`.
79  KALDI_ASSERT(static_cast<uint32>(opts.frame_opts.max_feature_vectors) > 200);
80 }
FeatureWindowFunction window_function_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

Member Function Documentation

◆ AcceptWaveform()

void AcceptWaveform ( BaseFloat  sampling_rate,
const VectorBase< BaseFloat > &  waveform 
)
virtual

This would be called from the application, when you get more wave data.

Note: the sampling_rate is typically only provided so the code can assert that it matches the sampling rate expected in the options.

Implements OnlineBaseFeature.

Definition at line 131 of file online-feature.cc.

References OnlineGenericBaseFeature< C >::ComputeFeatures(), VectorBase< Real >::Dim(), OnlineGenericBaseFeature< C >::input_finished_, KALDI_ERR, OnlineGenericBaseFeature< C >::MaybeCreateResampler(), VectorBase< Real >::Range(), OnlineGenericBaseFeature< C >::resampler_, Vector< Real >::Resize(), and OnlineGenericBaseFeature< C >::waveform_remainder_.

Referenced by kaldi::TestOnlineAppendFeature(), kaldi::TestOnlineMfcc(), kaldi::TestOnlinePlp(), and kaldi::TestOnlineTransform().

132  {
133  if (original_waveform.Dim() == 0)
134  return; // Nothing to do.
135  if (input_finished_)
136  KALDI_ERR << "AcceptWaveform called after InputFinished() was called.";
137 
138  Vector<BaseFloat> appended_wave;
139  Vector<BaseFloat> resampled_wave;
140 
141  const VectorBase<BaseFloat> *waveform;
142 
143  MaybeCreateResampler(sampling_rate);
144  if (resampler_ == nullptr) {
145  waveform = &original_waveform;
146  } else {
147  resampler_->Resample(original_waveform, false, &resampled_wave);
148  waveform = &resampled_wave;
149  }
150 
151  appended_wave.Resize(waveform_remainder_.Dim() + waveform->Dim());
152  if (waveform_remainder_.Dim() != 0)
153  appended_wave.Range(0, waveform_remainder_.Dim())
154  .CopyFromVec(waveform_remainder_);
155  appended_wave.Range(waveform_remainder_.Dim(), waveform->Dim())
156  .CopyFromVec(*waveform);
157  waveform_remainder_.Swap(&appended_wave);
158  ComputeFeatures();
159 }
void MaybeCreateResampler(BaseFloat sampling_rate)
Vector< BaseFloat > waveform_remainder_
#define KALDI_ERR
Definition: kaldi-error.h:147
std::unique_ptr< LinearResample > resampler_

◆ ComputeFeatures()

void ComputeFeatures ( )
private

Definition at line 162 of file online-feature.cc.

References OnlineGenericBaseFeature< C >::computer_, VectorBase< Real >::CopyFromVec(), kaldi::ExtractWindow(), OnlineGenericBaseFeature< C >::features_, kaldi::FirstSampleOfFrame(), OnlineGenericBaseFeature< C >::input_finished_, KALDI_ASSERT, kaldi::kUndefined, kaldi::NumFrames(), RecyclingVector::PushBack(), RecyclingVector::Size(), OnlineGenericBaseFeature< C >::waveform_offset_, OnlineGenericBaseFeature< C >::waveform_remainder_, and OnlineGenericBaseFeature< C >::window_function_.

Referenced by OnlineGenericBaseFeature< C >::AcceptWaveform(), and OnlineGenericBaseFeature< C >::InputFinished().

162  {
163  const FrameExtractionOptions &frame_opts = computer_.GetFrameOptions();
164  int64 num_samples_total = waveform_offset_ + waveform_remainder_.Dim();
165  int32 num_frames_old = features_.Size(),
166  num_frames_new = NumFrames(num_samples_total, frame_opts,
168  KALDI_ASSERT(num_frames_new >= num_frames_old);
169 
170  Vector<BaseFloat> window;
171  bool need_raw_log_energy = computer_.NeedRawLogEnergy();
172  for (int32 frame = num_frames_old; frame < num_frames_new; frame++) {
173  BaseFloat raw_log_energy = 0.0;
175  frame_opts, window_function_, &window,
176  need_raw_log_energy ? &raw_log_energy : NULL);
177  Vector<BaseFloat> *this_feature = new Vector<BaseFloat>(computer_.Dim(),
178  kUndefined);
179  // note: this online feature-extraction code does not support VTLN.
180  BaseFloat vtln_warp = 1.0;
181  computer_.Compute(raw_log_energy, vtln_warp, &window, this_feature);
182  features_.PushBack(this_feature);
183  }
184  // OK, we will now discard any portion of the signal that will not be
185  // necessary to compute frames in the future.
186  int64 first_sample_of_next_frame = FirstSampleOfFrame(num_frames_new,
187  frame_opts);
188  int32 samples_to_discard = first_sample_of_next_frame - waveform_offset_;
189  if (samples_to_discard > 0) {
190  // discard the leftmost part of the waveform that we no longer need.
191  int32 new_num_samples = waveform_remainder_.Dim() - samples_to_discard;
192  if (new_num_samples <= 0) {
193  // odd, but we'll try to handle it.
194  waveform_offset_ += waveform_remainder_.Dim();
195  waveform_remainder_.Resize(0);
196  } else {
197  Vector<BaseFloat> new_remainder(new_num_samples);
198  new_remainder.CopyFromVec(waveform_remainder_.Range(samples_to_discard,
199  new_num_samples));
200  waveform_offset_ += samples_to_discard;
201  waveform_remainder_.Swap(&new_remainder);
202  }
203  }
204 }
kaldi::int32 int32
void PushBack(Vector< BaseFloat > *item)
The ownership of the item is passed to this collection - do not delete the item.
Vector< BaseFloat > waveform_remainder_
int Size() const
This method returns the size as if no "recycling" had happened, i.e.
void ExtractWindow(int64 sample_offset, const VectorBase< BaseFloat > &wave, int32 f, const FrameExtractionOptions &opts, const FeatureWindowFunction &window_function, Vector< BaseFloat > *window, BaseFloat *log_energy_pre_window)
FeatureWindowFunction window_function_
float BaseFloat
Definition: kaldi-types.h:29
int64 FirstSampleOfFrame(int32 frame, const FrameExtractionOptions &opts)
int32 NumFrames(int64 num_samples, const FrameExtractionOptions &opts, bool flush)
This function returns the number of frames that we can extract from a wave file with the given number...
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Dim()

virtual int32 Dim ( ) const
inlinevirtual

Implements OnlineFeatureInterface.

Definition at line 83 of file online-feature.h.

Referenced by kaldi::TestOnlineTransform().

◆ FrameShiftInSeconds()

virtual BaseFloat FrameShiftInSeconds ( ) const
inlinevirtual

Implements OnlineFeatureInterface.

Definition at line 90 of file online-feature.h.

90  {
91  return computer_.GetFrameOptions().frame_shift_ms / 1000.0f;
92  }

◆ GetFrame()

void GetFrame ( int32  frame,
VectorBase< BaseFloat > *  feat 
)
virtual

Gets the feature vector for this frame.

Before calling this for a given frame, it is assumed that you called NumFramesReady() and it returned a number greater than "frame". Otherwise this call will likely crash with an assert failure. This function is not declared const, in case there is some kind of caching going on, but most of the time it shouldn't modify the class.

Implements OnlineFeatureInterface.

Definition at line 64 of file online-feature.cc.

References VectorBase< Real >::CopyFromVec().

65  {
66  feat->CopyFromVec(*(features_.At(frame)));
67 };
Vector< BaseFloat > * At(int index) const
The ownership is being retained by this collection - do not delete the item.

◆ InputFinished()

void InputFinished ( )
virtual

InputFinished() tells the class you won't be providing any more waveform.

This will help flush out the last few frames of delta or LDA features (it will typically affect the return value of IsLastFrame.

Implements OnlineBaseFeature.

Definition at line 107 of file online-feature.cc.

References OnlineGenericBaseFeature< C >::ComputeFeatures(), VectorBase< Real >::Dim(), OnlineGenericBaseFeature< C >::input_finished_, VectorBase< Real >::Range(), OnlineGenericBaseFeature< C >::resampler_, Vector< Real >::Resize(), and OnlineGenericBaseFeature< C >::waveform_remainder_.

Referenced by kaldi::TestOnlineAppendFeature(), kaldi::TestOnlineMfcc(), kaldi::TestOnlinePlp(), and kaldi::TestOnlineTransform().

107  {
108  if (resampler_ != nullptr) {
109  // There may be a few samples left once we flush the resampler_ object, telling it
110  // that the file has finished. This should rarely make any difference.
111  Vector<BaseFloat> appended_wave;
112  Vector<BaseFloat> resampled_wave;
113  resampler_->Resample(appended_wave, true, &resampled_wave);
114 
115  if (resampled_wave.Dim() != 0) {
116  appended_wave.Resize(waveform_remainder_.Dim() +
117  resampled_wave.Dim());
118  if (waveform_remainder_.Dim() != 0)
119  appended_wave.Range(0, waveform_remainder_.Dim())
120  .CopyFromVec(waveform_remainder_);
121  appended_wave.Range(waveform_remainder_.Dim(), resampled_wave.Dim())
122  .CopyFromVec(resampled_wave);
123  waveform_remainder_.Swap(&appended_wave);
124  }
125  }
126  input_finished_ = true;
127  ComputeFeatures();
128 }
Vector< BaseFloat > waveform_remainder_
std::unique_ptr< LinearResample > resampler_

◆ IsLastFrame()

virtual bool IsLastFrame ( int32  frame) const
inlinevirtual

Returns true if this is the last frame.

Frame indices are zero-based, so the first frame is zero. IsLastFrame(-1) will return false, unless the file is empty (which is a case that I'm not sure all the code will handle, so be careful). This function may return false for some frame if we haven't yet decided to terminate decoding, but later true if we decide to terminate decoding. This function exists mainly to correctly handle end effects in feature extraction, and is not a mechanism to determine how many frames are in the decodable object (as it used to be, and for backward compatibility, still is, in the Decodable interface).

Implements OnlineFeatureInterface.

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

87  {
88  return input_finished_ && frame == NumFramesReady() - 1;
89  }
virtual int32 NumFramesReady() const
returns the feature dimension.

◆ MaybeCreateResampler()

void MaybeCreateResampler ( BaseFloat  sampling_rate)
private

Definition at line 84 of file online-feature.cc.

References OnlineGenericBaseFeature< C >::computer_, KALDI_ASSERT, KALDI_ERR, and OnlineGenericBaseFeature< C >::resampler_.

Referenced by OnlineGenericBaseFeature< C >::AcceptWaveform().

85  {
86  BaseFloat expected_sampling_rate = computer_.GetFrameOptions().samp_freq;
87 
88  if (resampler_ != nullptr) {
89  KALDI_ASSERT(resampler_->GetInputSamplingRate() == sampling_rate);
90  KALDI_ASSERT(resampler_->GetOutputSamplingRate() == expected_sampling_rate);
91  } else if (((sampling_rate > expected_sampling_rate) &&
92  computer_.GetFrameOptions().allow_downsample) ||
93  ((sampling_rate < expected_sampling_rate) &&
94  computer_.GetFrameOptions().allow_upsample)) {
95  resampler_.reset(new LinearResample(
96  sampling_rate, expected_sampling_rate,
97  std::min(sampling_rate / 2, expected_sampling_rate / 2), 6));
98  } else if (sampling_rate != expected_sampling_rate) {
99  KALDI_ERR << "Sampling frequency mismatch, expected "
100  << expected_sampling_rate << ", got " << sampling_rate
101  << "\nPerhaps you want to use the options "
102  "--allow_{upsample,downsample}";
103  }
104 }
float BaseFloat
Definition: kaldi-types.h:29
#define KALDI_ERR
Definition: kaldi-error.h:147
std::unique_ptr< LinearResample > resampler_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ NumFramesReady()

virtual int32 NumFramesReady ( ) const
inlinevirtual

returns the feature dimension.

Returns the total number of frames, since the start of the utterance, that are now available. In an online-decoding context, this will likely increase with time as more data becomes available.

Implements OnlineFeatureInterface.

Definition at line 94 of file online-feature.h.

94 { return features_.Size(); }
int Size() const
This method returns the size as if no "recycling" had happened, i.e.

Member Data Documentation

◆ computer_

◆ features_

RecyclingVector features_
private

Definition at line 139 of file online-feature.h.

Referenced by OnlineGenericBaseFeature< C >::ComputeFeatures().

◆ input_finished_

◆ resampler_

◆ sampling_frequency_

BaseFloat sampling_frequency_
private

Definition at line 146 of file online-feature.h.

◆ waveform_offset_

int64 waveform_offset_
private

Definition at line 150 of file online-feature.h.

Referenced by OnlineGenericBaseFeature< C >::ComputeFeatures().

◆ waveform_remainder_

◆ window_function_

FeatureWindowFunction window_function_
private

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

Referenced by OnlineGenericBaseFeature< C >::ComputeFeatures().


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