feature-mfcc-test.cc File Reference
#include <iostream>
#include "feat/feature-mfcc.h"
#include "base/kaldi-math.h"
#include "matrix/kaldi-matrix-inl.h"
#include "feat/wave-reader.h"
Include dependency graph for feature-mfcc-test.cc:

Go to the source code of this file.

Functions

static void UnitTestReadWave ()
 
static void UnitTestSimple ()
 
static void UnitTestHTKCompare1 ()
 
static void UnitTestHTKCompare2 ()
 
static void UnitTestHTKCompare3 ()
 
static void UnitTestHTKCompare4 ()
 
static void UnitTestHTKCompare5 ()
 
static void UnitTestHTKCompare6 ()
 
void UnitTestVtln ()
 
static void UnitTestFeat ()
 
int main ()
 

Function Documentation

◆ main()

int main ( )

Definition at line 674 of file feature-mfcc-test.cc.

References rnnlm::i, and UnitTestFeat().

674  {
675  try {
676  for (int i = 0; i < 5; i++)
677  UnitTestFeat();
678  std::cout << "Tests succeeded.\n";
679  return 0;
680  } catch (const std::exception &e) {
681  std::cerr << e.what();
682  return 1;
683  }
684 }
static void UnitTestFeat()

◆ UnitTestFeat()

static void UnitTestFeat ( )
static

Definition at line 656 of file feature-mfcc-test.cc.

References UnitTestHTKCompare1(), UnitTestHTKCompare2(), UnitTestHTKCompare3(), UnitTestHTKCompare4(), UnitTestHTKCompare5(), UnitTestHTKCompare6(), UnitTestReadWave(), kaldi::UnitTestSimple(), and UnitTestVtln().

Referenced by main().

656  {
657  UnitTestVtln();
659  UnitTestSimple();
662  // commenting out this one as it doesn't compare right now I normalized
663  // the way the FFT bins are treated (removed offset of 0.5)... this seems
664  // to relate to the way frequency zero behaves.
669  std::cout << "Tests succeeded.\n";
670 }
static void UnitTestReadWave()
static void UnitTestHTKCompare6()
static void UnitTestHTKCompare1()
static void UnitTestHTKCompare2()
void UnitTestVtln()
static void UnitTestHTKCompare3()
static void UnitTestHTKCompare5()
static void UnitTestSimple()
static void UnitTestHTKCompare4()

◆ UnitTestHTKCompare1()

static void UnitTestHTKCompare1 ( )
static

Definition at line 112 of file feature-mfcc-test.cc.

References OfflineFeatureTpl< F >::Compute(), kaldi::ComputeDeltas(), WaveData::Data(), FrameExtractionOptions::dither, MfccOptions::frame_opts, MfccOptions::htk_compat, MelBanksOptions::htk_mode, rnnlm::i, rnnlm::j, KALDI_ASSERT, KALDI_ERR, MelBanksOptions::low_freq, MfccOptions::mel_opts, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), FrameExtractionOptions::preemph_coeff, WaveData::Read(), kaldi::ReadHtk(), FrameExtractionOptions::remove_dc_offset, FrameExtractionOptions::round_to_power_of_two, MatrixBase< Real >::Row(), MfccOptions::use_energy, FrameExtractionOptions::window_type, and kaldi::WriteHtk().

Referenced by UnitTestFeat().

112  {
113  std::cout << "=== UnitTestHTKCompare1() ===\n";
114 
115  std::ifstream is("test_data/test.wav", std::ios_base::binary);
116  WaveData wave;
117  wave.Read(is);
118  KALDI_ASSERT(wave.Data().NumRows() == 1);
119  SubVector<BaseFloat> waveform(wave.Data(), 0);
120 
121  // read the HTK features
122  Matrix<BaseFloat> htk_features;
123  {
124  std::ifstream is("test_data/test.wav.fea_htk.1",
125  std::ios::in | std::ios_base::binary);
126  bool ans = ReadHtk(is, &htk_features, 0);
127  KALDI_ASSERT(ans);
128  }
129 
130  // use mfcc with default configuration...
131  MfccOptions op;
132  op.frame_opts.dither = 0.0;
133  op.frame_opts.preemph_coeff = 0.0;
134  op.frame_opts.window_type = "hamming";
135  op.frame_opts.remove_dc_offset = false;
137  op.mel_opts.low_freq = 0.0;
138  op.mel_opts.htk_mode = true;
139  op.htk_compat = true;
140  op.use_energy = false; // C0 not energy.
141 
142  Mfcc mfcc(op);
143 
144  // calculate kaldi features
145  Matrix<BaseFloat> kaldi_raw_features;
146  mfcc.Compute(waveform, 1.0, &kaldi_raw_features);
147 
148  DeltaFeaturesOptions delta_opts;
149  Matrix<BaseFloat> kaldi_features;
150  ComputeDeltas(delta_opts,
151  kaldi_raw_features,
152  &kaldi_features);
153 
154  // compare the results
155  bool passed = true;
156  int32 i_old = -1;
157  KALDI_ASSERT(kaldi_features.NumRows() == htk_features.NumRows());
158  KALDI_ASSERT(kaldi_features.NumCols() == htk_features.NumCols());
159  // Ignore ends-- we make slightly different choices than
160  // HTK about how to treat the deltas at the ends.
161  for (int32 i = 10; i+10 < kaldi_features.NumRows(); i++) {
162  for (int32 j = 0; j < kaldi_features.NumCols(); j++) {
163  BaseFloat a = kaldi_features(i, j), b = htk_features(i, j);
164  if ((std::abs(b - a)) > 1.0) { //<< TOLERANCE TO DIFFERENCES!!!!!
165  // print the non-matching data only once per-line
166  if (i_old != i) {
167  std::cout << "\n\n\n[HTK-row: " << i << "] " << htk_features.Row(i) << "\n";
168  std::cout << "[Kaldi-row: " << i << "] " << kaldi_features.Row(i) << "\n\n\n";
169  i_old = i;
170  }
171  // print indices of non-matching cells
172  std::cout << "[" << i << ", " << j << "]";
173  passed = false;
174  }}}
175  if (!passed) KALDI_ERR << "Test failed";
176 
177  // write the htk features for later inspection
178  HtkHeader header = {
179  kaldi_features.NumRows(),
180  100000, // 10ms
181  static_cast<int16>(sizeof(float)*kaldi_features.NumCols()),
182  021406 // MFCC_D_A_0
183  };
184  {
185  std::ofstream os("tmp.test.wav.fea_kaldi.1",
186  std::ios::out|std::ios::binary);
187  WriteHtk(os, kaldi_features, header);
188  }
189 
190  std::cout << "Test passed :)\n\n";
191 
192  unlink("tmp.test.wav.fea_kaldi.1");
193 }
void Read(std::istream &is)
Read() will throw on error.
Definition: wave-reader.cc:272
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
MfccOptions contains basic options for computing MFCC features.
Definition: feature-mfcc.h:38
bool WriteHtk(std::ostream &os, const MatrixBase< Real > &M, HtkHeader htk_hdr)
kaldi::int32 int32
const Matrix< BaseFloat > & Data() const
Definition: wave-reader.h:124
float BaseFloat
Definition: kaldi-types.h:29
MelBanksOptions mel_opts
Definition: feature-mfcc.h:40
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
FrameExtractionOptions frame_opts
Definition: feature-mfcc.h:39
#define KALDI_ERR
Definition: kaldi-error.h:147
This class&#39;s purpose is to read in Wave files.
Definition: wave-reader.h:106
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void ComputeDeltas(const DeltaFeaturesOptions &delta_opts, const MatrixBase< BaseFloat > &input_features, Matrix< BaseFloat > *output_features)
This templated class is intended for offline feature extraction, i.e.
bool ReadHtk(std::istream &is, Matrix< Real > *M_ptr, HtkHeader *header_ptr)
Extension of the HTK header.
A structure containing the HTK header.
Definition: kaldi-matrix.h:955
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501

◆ UnitTestHTKCompare2()

static void UnitTestHTKCompare2 ( )
static

Definition at line 196 of file feature-mfcc-test.cc.

References OfflineFeatureTpl< F >::Compute(), kaldi::ComputeDeltas(), WaveData::Data(), FrameExtractionOptions::dither, MfccOptions::frame_opts, MfccOptions::htk_compat, MelBanksOptions::htk_mode, rnnlm::i, rnnlm::j, KALDI_ASSERT, KALDI_ERR, MelBanksOptions::low_freq, MfccOptions::mel_opts, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), FrameExtractionOptions::preemph_coeff, WaveData::Read(), kaldi::ReadHtk(), FrameExtractionOptions::remove_dc_offset, FrameExtractionOptions::round_to_power_of_two, MatrixBase< Real >::Row(), MfccOptions::use_energy, FrameExtractionOptions::window_type, and kaldi::WriteHtk().

Referenced by UnitTestFeat().

196  {
197  std::cout << "=== UnitTestHTKCompare2() ===\n";
198 
199  std::ifstream is("test_data/test.wav", std::ios_base::binary);
200  WaveData wave;
201  wave.Read(is);
202  KALDI_ASSERT(wave.Data().NumRows() == 1);
203  SubVector<BaseFloat> waveform(wave.Data(), 0);
204 
205  // read the HTK features
206  Matrix<BaseFloat> htk_features;
207  {
208  std::ifstream is("test_data/test.wav.fea_htk.2",
209  std::ios::in | std::ios_base::binary);
210  bool ans = ReadHtk(is, &htk_features, 0);
211  KALDI_ASSERT(ans);
212  }
213 
214  // use mfcc with default configuration...
215  MfccOptions op;
216  op.frame_opts.dither = 0.0;
217  op.frame_opts.preemph_coeff = 0.0;
218  op.frame_opts.window_type = "hamming";
219  op.frame_opts.remove_dc_offset = false;
221  op.mel_opts.low_freq = 0.0;
222  op.mel_opts.htk_mode = true;
223  op.htk_compat = true;
224  op.use_energy = true; // Use energy.
225 
226  Mfcc mfcc(op);
227 
228  // calculate kaldi features
229  Matrix<BaseFloat> kaldi_raw_features;
230  mfcc.Compute(waveform, 1.0, &kaldi_raw_features);
231 
232  DeltaFeaturesOptions delta_opts;
233  Matrix<BaseFloat> kaldi_features;
234  ComputeDeltas(delta_opts,
235  kaldi_raw_features,
236  &kaldi_features);
237 
238  // compare the results
239  bool passed = true;
240  int32 i_old = -1;
241  KALDI_ASSERT(kaldi_features.NumRows() == htk_features.NumRows());
242  KALDI_ASSERT(kaldi_features.NumCols() == htk_features.NumCols());
243  // Ignore ends-- we make slightly different choices than
244  // HTK about how to treat the deltas at the ends.
245  for (int32 i = 10; i+10 < kaldi_features.NumRows(); i++) {
246  for (int32 j = 0; j < kaldi_features.NumCols(); j++) {
247  BaseFloat a = kaldi_features(i, j), b = htk_features(i, j);
248  if ((std::abs(b - a)) > 1.0) { //<< TOLERANCE TO DIFFERENCES!!!!!
249  // print the non-matching data only once per-line
250  if (i_old != i) {
251  std::cout << "\n\n\n[HTK-row: " << i << "] " << htk_features.Row(i) << "\n";
252  std::cout << "[Kaldi-row: " << i << "] " << kaldi_features.Row(i) << "\n\n\n";
253  i_old = i;
254  }
255  // print indices of non-matching cells
256  std::cout << "[" << i << ", " << j << "]";
257  passed = false;
258  }}}
259  if (!passed) KALDI_ERR << "Test failed";
260 
261  // write the htk features for later inspection
262  HtkHeader header = {
263  kaldi_features.NumRows(),
264  100000, // 10ms
265  static_cast<int16>(sizeof(float)*kaldi_features.NumCols()),
266  021406 // MFCC_D_A_0
267  };
268  {
269  std::ofstream os("tmp.test.wav.fea_kaldi.2",
270  std::ios::out|std::ios::binary);
271  WriteHtk(os, kaldi_features, header);
272  }
273 
274  std::cout << "Test passed :)\n\n";
275 
276  unlink("tmp.test.wav.fea_kaldi.2");
277 }
void Read(std::istream &is)
Read() will throw on error.
Definition: wave-reader.cc:272
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
MfccOptions contains basic options for computing MFCC features.
Definition: feature-mfcc.h:38
bool WriteHtk(std::ostream &os, const MatrixBase< Real > &M, HtkHeader htk_hdr)
kaldi::int32 int32
const Matrix< BaseFloat > & Data() const
Definition: wave-reader.h:124
float BaseFloat
Definition: kaldi-types.h:29
MelBanksOptions mel_opts
Definition: feature-mfcc.h:40
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
FrameExtractionOptions frame_opts
Definition: feature-mfcc.h:39
#define KALDI_ERR
Definition: kaldi-error.h:147
This class&#39;s purpose is to read in Wave files.
Definition: wave-reader.h:106
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void ComputeDeltas(const DeltaFeaturesOptions &delta_opts, const MatrixBase< BaseFloat > &input_features, Matrix< BaseFloat > *output_features)
This templated class is intended for offline feature extraction, i.e.
bool ReadHtk(std::istream &is, Matrix< Real > *M_ptr, HtkHeader *header_ptr)
Extension of the HTK header.
A structure containing the HTK header.
Definition: kaldi-matrix.h:955
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501

◆ UnitTestHTKCompare3()

static void UnitTestHTKCompare3 ( )
static

Definition at line 280 of file feature-mfcc-test.cc.

References OfflineFeatureTpl< F >::Compute(), kaldi::ComputeDeltas(), WaveData::Data(), FrameExtractionOptions::dither, MfccOptions::frame_opts, MfccOptions::htk_compat, MelBanksOptions::htk_mode, rnnlm::i, rnnlm::j, KALDI_ASSERT, KALDI_ERR, MelBanksOptions::low_freq, MfccOptions::mel_opts, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), FrameExtractionOptions::preemph_coeff, WaveData::Read(), kaldi::ReadHtk(), FrameExtractionOptions::remove_dc_offset, FrameExtractionOptions::round_to_power_of_two, MatrixBase< Real >::Row(), MfccOptions::use_energy, FrameExtractionOptions::window_type, and kaldi::WriteHtk().

Referenced by UnitTestFeat().

280  {
281  std::cout << "=== UnitTestHTKCompare3() ===\n";
282 
283  std::ifstream is("test_data/test.wav", std::ios_base::binary);
284  WaveData wave;
285  wave.Read(is);
286  KALDI_ASSERT(wave.Data().NumRows() == 1);
287  SubVector<BaseFloat> waveform(wave.Data(), 0);
288 
289  // read the HTK features
290  Matrix<BaseFloat> htk_features;
291  {
292  std::ifstream is("test_data/test.wav.fea_htk.3",
293  std::ios::in | std::ios_base::binary);
294  bool ans = ReadHtk(is, &htk_features, 0);
295  KALDI_ASSERT(ans);
296  }
297 
298  // use mfcc with default configuration...
299  MfccOptions op;
300  op.frame_opts.dither = 0.0;
301  op.frame_opts.preemph_coeff = 0.0;
302  op.frame_opts.window_type = "hamming";
303  op.frame_opts.remove_dc_offset = false;
305  op.htk_compat = true;
306  op.use_energy = true; // Use energy.
307  op.mel_opts.low_freq = 20.0;
308  //op.mel_opts.debug_mel = true;
309  op.mel_opts.htk_mode = true;
310 
311  Mfcc mfcc(op);
312 
313  // calculate kaldi features
314  Matrix<BaseFloat> kaldi_raw_features;
315  mfcc.Compute(waveform, 1.0, &kaldi_raw_features);
316 
317  DeltaFeaturesOptions delta_opts;
318  Matrix<BaseFloat> kaldi_features;
319  ComputeDeltas(delta_opts,
320  kaldi_raw_features,
321  &kaldi_features);
322 
323  // compare the results
324  bool passed = true;
325  int32 i_old = -1;
326  KALDI_ASSERT(kaldi_features.NumRows() == htk_features.NumRows());
327  KALDI_ASSERT(kaldi_features.NumCols() == htk_features.NumCols());
328  // Ignore ends-- we make slightly different choices than
329  // HTK about how to treat the deltas at the ends.
330  for (int32 i = 10; i+10 < kaldi_features.NumRows(); i++) {
331  for (int32 j = 0; j < kaldi_features.NumCols(); j++) {
332  BaseFloat a = kaldi_features(i, j), b = htk_features(i, j);
333  if ((std::abs(b - a)) > 1.0) { //<< TOLERANCE TO DIFFERENCES!!!!!
334  // print the non-matching data only once per-line
335  if (static_cast<int32>(i_old) != i) {
336  std::cout << "\n\n\n[HTK-row: " << i << "] " << htk_features.Row(i) << "\n";
337  std::cout << "[Kaldi-row: " << i << "] " << kaldi_features.Row(i) << "\n\n\n";
338  i_old = i;
339  }
340  // print indices of non-matching cells
341  std::cout << "[" << i << ", " << j << "]";
342  passed = false;
343  }}}
344  if (!passed) KALDI_ERR << "Test failed";
345 
346  // write the htk features for later inspection
347  HtkHeader header = {
348  kaldi_features.NumRows(),
349  100000, // 10ms
350  static_cast<int16>(sizeof(float)*kaldi_features.NumCols()),
351  021406 // MFCC_D_A_0
352  };
353  {
354  std::ofstream os("tmp.test.wav.fea_kaldi.3",
355  std::ios::out|std::ios::binary);
356  WriteHtk(os, kaldi_features, header);
357  }
358 
359  std::cout << "Test passed :)\n\n";
360 
361  unlink("tmp.test.wav.fea_kaldi.3");
362 }
void Read(std::istream &is)
Read() will throw on error.
Definition: wave-reader.cc:272
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
MfccOptions contains basic options for computing MFCC features.
Definition: feature-mfcc.h:38
bool WriteHtk(std::ostream &os, const MatrixBase< Real > &M, HtkHeader htk_hdr)
kaldi::int32 int32
const Matrix< BaseFloat > & Data() const
Definition: wave-reader.h:124
float BaseFloat
Definition: kaldi-types.h:29
MelBanksOptions mel_opts
Definition: feature-mfcc.h:40
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
FrameExtractionOptions frame_opts
Definition: feature-mfcc.h:39
#define KALDI_ERR
Definition: kaldi-error.h:147
This class&#39;s purpose is to read in Wave files.
Definition: wave-reader.h:106
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void ComputeDeltas(const DeltaFeaturesOptions &delta_opts, const MatrixBase< BaseFloat > &input_features, Matrix< BaseFloat > *output_features)
This templated class is intended for offline feature extraction, i.e.
bool ReadHtk(std::istream &is, Matrix< Real > *M_ptr, HtkHeader *header_ptr)
Extension of the HTK header.
A structure containing the HTK header.
Definition: kaldi-matrix.h:955
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501

◆ UnitTestHTKCompare4()

static void UnitTestHTKCompare4 ( )
static

Definition at line 365 of file feature-mfcc-test.cc.

References OfflineFeatureTpl< F >::Compute(), kaldi::ComputeDeltas(), WaveData::Data(), FrameExtractionOptions::dither, MfccOptions::frame_opts, MfccOptions::htk_compat, MelBanksOptions::htk_mode, rnnlm::i, rnnlm::j, KALDI_ASSERT, KALDI_ERR, MelBanksOptions::low_freq, MfccOptions::mel_opts, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), WaveData::Read(), kaldi::ReadHtk(), FrameExtractionOptions::remove_dc_offset, FrameExtractionOptions::round_to_power_of_two, MatrixBase< Real >::Row(), MfccOptions::use_energy, FrameExtractionOptions::window_type, and kaldi::WriteHtk().

Referenced by UnitTestFeat().

365  {
366  std::cout << "=== UnitTestHTKCompare4() ===\n";
367 
368  std::ifstream is("test_data/test.wav", std::ios_base::binary);
369  WaveData wave;
370  wave.Read(is);
371  KALDI_ASSERT(wave.Data().NumRows() == 1);
372  SubVector<BaseFloat> waveform(wave.Data(), 0);
373 
374  // read the HTK features
375  Matrix<BaseFloat> htk_features;
376  {
377  std::ifstream is("test_data/test.wav.fea_htk.4",
378  std::ios::in | std::ios_base::binary);
379  bool ans = ReadHtk(is, &htk_features, 0);
380  KALDI_ASSERT(ans);
381  }
382 
383  // use mfcc with default configuration...
384  MfccOptions op;
385  op.frame_opts.dither = 0.0;
386  op.frame_opts.window_type = "hamming";
387  op.frame_opts.remove_dc_offset = false;
389  op.mel_opts.low_freq = 0.0;
390  op.htk_compat = true;
391  op.use_energy = true; // Use energy.
392  op.mel_opts.htk_mode = true;
393 
394  Mfcc mfcc(op);
395 
396  // calculate kaldi features
397  Matrix<BaseFloat> kaldi_raw_features;
398  mfcc.Compute(waveform, 1.0, &kaldi_raw_features);
399 
400  DeltaFeaturesOptions delta_opts;
401  Matrix<BaseFloat> kaldi_features;
402  ComputeDeltas(delta_opts,
403  kaldi_raw_features,
404  &kaldi_features);
405 
406  // compare the results
407  bool passed = true;
408  int32 i_old = -1;
409  KALDI_ASSERT(kaldi_features.NumRows() == htk_features.NumRows());
410  KALDI_ASSERT(kaldi_features.NumCols() == htk_features.NumCols());
411  // Ignore ends-- we make slightly different choices than
412  // HTK about how to treat the deltas at the ends.
413  for (int32 i = 10; i+10 < kaldi_features.NumRows(); i++) {
414  for (int32 j = 0; j < kaldi_features.NumCols(); j++) {
415  BaseFloat a = kaldi_features(i, j), b = htk_features(i, j);
416  if ((std::abs(b - a)) > 1.0) { //<< TOLERANCE TO DIFFERENCES!!!!!
417  // print the non-matching data only once per-line
418  if (static_cast<int32>(i_old) != i) {
419  std::cout << "\n\n\n[HTK-row: " << i << "] " << htk_features.Row(i) << "\n";
420  std::cout << "[Kaldi-row: " << i << "] " << kaldi_features.Row(i) << "\n\n\n";
421  i_old = i;
422  }
423  // print indices of non-matching cells
424  std::cout << "[" << i << ", " << j << "]";
425  passed = false;
426  }}}
427  if (!passed) KALDI_ERR << "Test failed";
428 
429  // write the htk features for later inspection
430  HtkHeader header = {
431  kaldi_features.NumRows(),
432  100000, // 10ms
433  static_cast<int16>(sizeof(float)*kaldi_features.NumCols()),
434  021406 // MFCC_D_A_0
435  };
436  {
437  std::ofstream os("tmp.test.wav.fea_kaldi.4",
438  std::ios::out|std::ios::binary);
439  WriteHtk(os, kaldi_features, header);
440  }
441 
442  std::cout << "Test passed :)\n\n";
443 
444  unlink("tmp.test.wav.fea_kaldi.4");
445 }
void Read(std::istream &is)
Read() will throw on error.
Definition: wave-reader.cc:272
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
MfccOptions contains basic options for computing MFCC features.
Definition: feature-mfcc.h:38
bool WriteHtk(std::ostream &os, const MatrixBase< Real > &M, HtkHeader htk_hdr)
kaldi::int32 int32
const Matrix< BaseFloat > & Data() const
Definition: wave-reader.h:124
float BaseFloat
Definition: kaldi-types.h:29
MelBanksOptions mel_opts
Definition: feature-mfcc.h:40
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
FrameExtractionOptions frame_opts
Definition: feature-mfcc.h:39
#define KALDI_ERR
Definition: kaldi-error.h:147
This class&#39;s purpose is to read in Wave files.
Definition: wave-reader.h:106
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void ComputeDeltas(const DeltaFeaturesOptions &delta_opts, const MatrixBase< BaseFloat > &input_features, Matrix< BaseFloat > *output_features)
This templated class is intended for offline feature extraction, i.e.
bool ReadHtk(std::istream &is, Matrix< Real > *M_ptr, HtkHeader *header_ptr)
Extension of the HTK header.
A structure containing the HTK header.
Definition: kaldi-matrix.h:955
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501

◆ UnitTestHTKCompare5()

static void UnitTestHTKCompare5 ( )
static

Definition at line 448 of file feature-mfcc-test.cc.

References OfflineFeatureTpl< F >::Compute(), kaldi::ComputeDeltas(), WaveData::Data(), FrameExtractionOptions::dither, MfccOptions::frame_opts, MfccOptions::htk_compat, MelBanksOptions::htk_mode, rnnlm::i, rnnlm::j, KALDI_ASSERT, KALDI_ERR, MelBanksOptions::low_freq, MfccOptions::mel_opts, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), WaveData::Read(), kaldi::ReadHtk(), FrameExtractionOptions::remove_dc_offset, FrameExtractionOptions::round_to_power_of_two, MatrixBase< Real >::Row(), MfccOptions::use_energy, MelBanksOptions::vtln_high, MelBanksOptions::vtln_low, FrameExtractionOptions::window_type, and kaldi::WriteHtk().

Referenced by UnitTestFeat().

448  {
449  std::cout << "=== UnitTestHTKCompare5() ===\n";
450 
451  std::ifstream is("test_data/test.wav", std::ios_base::binary);
452  WaveData wave;
453  wave.Read(is);
454  KALDI_ASSERT(wave.Data().NumRows() == 1);
455  SubVector<BaseFloat> waveform(wave.Data(), 0);
456 
457  // read the HTK features
458  Matrix<BaseFloat> htk_features;
459  {
460  std::ifstream is("test_data/test.wav.fea_htk.5",
461  std::ios::in | std::ios_base::binary);
462  bool ans = ReadHtk(is, &htk_features, 0);
463  KALDI_ASSERT(ans);
464  }
465 
466  // use mfcc with default configuration...
467  MfccOptions op;
468  op.frame_opts.dither = 0.0;
469  op.frame_opts.window_type = "hamming";
470  op.frame_opts.remove_dc_offset = false;
472  op.htk_compat = true;
473  op.use_energy = true; // Use energy.
474  op.mel_opts.low_freq = 0.0;
475  op.mel_opts.vtln_low = 100.0;
476  op.mel_opts.vtln_high = 7500.0;
477  op.mel_opts.htk_mode = true;
478 
479  BaseFloat vtln_warp = 1.1; // our approach identical to htk for warp factor >1,
480  // differs slightly for higher mel bins if warp_factor <0.9
481 
482  Mfcc mfcc(op);
483 
484  // calculate kaldi features
485  Matrix<BaseFloat> kaldi_raw_features;
486  mfcc.Compute(waveform, vtln_warp, &kaldi_raw_features);
487 
488  DeltaFeaturesOptions delta_opts;
489  Matrix<BaseFloat> kaldi_features;
490  ComputeDeltas(delta_opts,
491  kaldi_raw_features,
492  &kaldi_features);
493 
494  // compare the results
495  bool passed = true;
496  int32 i_old = -1;
497  KALDI_ASSERT(kaldi_features.NumRows() == htk_features.NumRows());
498  KALDI_ASSERT(kaldi_features.NumCols() == htk_features.NumCols());
499  // Ignore ends-- we make slightly different choices than
500  // HTK about how to treat the deltas at the ends.
501  for (int32 i = 10; i+10 < kaldi_features.NumRows(); i++) {
502  for (int32 j = 0; j < kaldi_features.NumCols(); j++) {
503  BaseFloat a = kaldi_features(i, j), b = htk_features(i, j);
504  if ((std::abs(b - a)) > 1.0) { //<< TOLERANCE TO DIFFERENCES!!!!!
505  // print the non-matching data only once per-line
506  if (static_cast<int32>(i_old) != i) {
507  std::cout << "\n\n\n[HTK-row: " << i << "] " << htk_features.Row(i) << "\n";
508  std::cout << "[Kaldi-row: " << i << "] " << kaldi_features.Row(i) << "\n\n\n";
509  i_old = i;
510  }
511  // print indices of non-matching cells
512  std::cout << "[" << i << ", " << j << "]";
513  passed = false;
514  }}}
515  if (!passed) KALDI_ERR << "Test failed";
516 
517  // write the htk features for later inspection
518  HtkHeader header = {
519  kaldi_features.NumRows(),
520  100000, // 10ms
521  static_cast<int16>(sizeof(float)*kaldi_features.NumCols()),
522  021406 // MFCC_D_A_0
523  };
524  {
525  std::ofstream os("tmp.test.wav.fea_kaldi.5",
526  std::ios::out|std::ios::binary);
527  WriteHtk(os, kaldi_features, header);
528  }
529 
530  std::cout << "Test passed :)\n\n";
531 
532  unlink("tmp.test.wav.fea_kaldi.5");
533 }
void Read(std::istream &is)
Read() will throw on error.
Definition: wave-reader.cc:272
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
MfccOptions contains basic options for computing MFCC features.
Definition: feature-mfcc.h:38
bool WriteHtk(std::ostream &os, const MatrixBase< Real > &M, HtkHeader htk_hdr)
kaldi::int32 int32
const Matrix< BaseFloat > & Data() const
Definition: wave-reader.h:124
float BaseFloat
Definition: kaldi-types.h:29
MelBanksOptions mel_opts
Definition: feature-mfcc.h:40
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
FrameExtractionOptions frame_opts
Definition: feature-mfcc.h:39
#define KALDI_ERR
Definition: kaldi-error.h:147
This class&#39;s purpose is to read in Wave files.
Definition: wave-reader.h:106
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void ComputeDeltas(const DeltaFeaturesOptions &delta_opts, const MatrixBase< BaseFloat > &input_features, Matrix< BaseFloat > *output_features)
This templated class is intended for offline feature extraction, i.e.
bool ReadHtk(std::istream &is, Matrix< Real > *M_ptr, HtkHeader *header_ptr)
Extension of the HTK header.
A structure containing the HTK header.
Definition: kaldi-matrix.h:955
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501

◆ UnitTestHTKCompare6()

static void UnitTestHTKCompare6 ( )
static

Definition at line 535 of file feature-mfcc-test.cc.

References OfflineFeatureTpl< F >::Compute(), kaldi::ComputeDeltas(), WaveData::Data(), FrameExtractionOptions::dither, MfccOptions::frame_opts, MelBanksOptions::high_freq, MfccOptions::htk_compat, rnnlm::i, rnnlm::j, KALDI_ASSERT, KALDI_ERR, MelBanksOptions::low_freq, MfccOptions::mel_opts, MelBanksOptions::num_bins, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), FrameExtractionOptions::preemph_coeff, WaveData::Read(), kaldi::ReadHtk(), FrameExtractionOptions::remove_dc_offset, FrameExtractionOptions::round_to_power_of_two, MatrixBase< Real >::Row(), MfccOptions::use_energy, FrameExtractionOptions::window_type, and kaldi::WriteHtk().

Referenced by UnitTestFeat().

535  {
536  std::cout << "=== UnitTestHTKCompare6() ===\n";
537 
538 
539  std::ifstream is("test_data/test.wav", std::ios_base::binary);
540  WaveData wave;
541  wave.Read(is);
542  KALDI_ASSERT(wave.Data().NumRows() == 1);
543  SubVector<BaseFloat> waveform(wave.Data(), 0);
544 
545  // read the HTK features
546  Matrix<BaseFloat> htk_features;
547  {
548  std::ifstream is("test_data/test.wav.fea_htk.6",
549  std::ios::in | std::ios_base::binary);
550  bool ans = ReadHtk(is, &htk_features, 0);
551  KALDI_ASSERT(ans);
552  }
553 
554  // use mfcc with default configuration...
555  MfccOptions op;
556  op.frame_opts.dither = 0.0;
557  op.frame_opts.preemph_coeff = 0.97;
558  op.frame_opts.window_type = "hamming";
559  op.frame_opts.remove_dc_offset = false;
561  op.mel_opts.num_bins = 24;
562  op.mel_opts.low_freq = 125.0;
563  op.mel_opts.high_freq = 7800.0;
564  op.htk_compat = true;
565  op.use_energy = false; // C0 not energy.
566 
567  Mfcc mfcc(op);
568 
569  // calculate kaldi features
570  Matrix<BaseFloat> kaldi_raw_features;
571  mfcc.Compute(waveform, 1.0, &kaldi_raw_features);
572 
573  DeltaFeaturesOptions delta_opts;
574  Matrix<BaseFloat> kaldi_features;
575  ComputeDeltas(delta_opts,
576  kaldi_raw_features,
577  &kaldi_features);
578 
579  // compare the results
580  bool passed = true;
581  int32 i_old = -1;
582  KALDI_ASSERT(kaldi_features.NumRows() == htk_features.NumRows());
583  KALDI_ASSERT(kaldi_features.NumCols() == htk_features.NumCols());
584  // Ignore ends-- we make slightly different choices than
585  // HTK about how to treat the deltas at the ends.
586  for (int32 i = 10; i+10 < kaldi_features.NumRows(); i++) {
587  for (int32 j = 0; j < kaldi_features.NumCols(); j++) {
588  BaseFloat a = kaldi_features(i, j), b = htk_features(i, j);
589  if ((std::abs(b - a)) > 1.0) { //<< TOLERANCE TO DIFFERENCES!!!!!
590  // print the non-matching data only once per-line
591  if (static_cast<int32>(i_old) != i) {
592  std::cout << "\n\n\n[HTK-row: " << i << "] " << htk_features.Row(i) << "\n";
593  std::cout << "[Kaldi-row: " << i << "] " << kaldi_features.Row(i) << "\n\n\n";
594  i_old = i;
595  }
596  // print indices of non-matching cells
597  std::cout << "[" << i << ", " << j << "]";
598  passed = false;
599  }}}
600  if (!passed) KALDI_ERR << "Test failed";
601 
602  // write the htk features for later inspection
603  HtkHeader header = {
604  kaldi_features.NumRows(),
605  100000, // 10ms
606  static_cast<int16>(sizeof(float)*kaldi_features.NumCols()),
607  021406 // MFCC_D_A_0
608  };
609  {
610  std::ofstream os("tmp.test.wav.fea_kaldi.6",
611  std::ios::out|std::ios::binary);
612  WriteHtk(os, kaldi_features, header);
613  }
614 
615  std::cout << "Test passed :)\n\n";
616 
617  unlink("tmp.test.wav.fea_kaldi.6");
618 }
void Read(std::istream &is)
Read() will throw on error.
Definition: wave-reader.cc:272
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
MfccOptions contains basic options for computing MFCC features.
Definition: feature-mfcc.h:38
bool WriteHtk(std::ostream &os, const MatrixBase< Real > &M, HtkHeader htk_hdr)
kaldi::int32 int32
const Matrix< BaseFloat > & Data() const
Definition: wave-reader.h:124
float BaseFloat
Definition: kaldi-types.h:29
MelBanksOptions mel_opts
Definition: feature-mfcc.h:40
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
FrameExtractionOptions frame_opts
Definition: feature-mfcc.h:39
#define KALDI_ERR
Definition: kaldi-error.h:147
This class&#39;s purpose is to read in Wave files.
Definition: wave-reader.h:106
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void ComputeDeltas(const DeltaFeaturesOptions &delta_opts, const MatrixBase< BaseFloat > &input_features, Matrix< BaseFloat > *output_features)
This templated class is intended for offline feature extraction, i.e.
bool ReadHtk(std::istream &is, Matrix< Real > *M_ptr, HtkHeader *header_ptr)
Extension of the HTK header.
A structure containing the HTK header.
Definition: kaldi-matrix.h:955
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501

◆ UnitTestReadWave()

static void UnitTestReadWave ( )
static

Definition at line 32 of file feature-mfcc-test.cc.

References VectorBase< Real >::CopyFromVec(), WaveData::Data(), VectorBase< Real >::Dim(), rnnlm::i, KALDI_ASSERT, WaveData::Read(), Vector< Real >::Read(), and Vector< Real >::Resize().

Referenced by UnitTestFeat().

32  {
33 
34  std::cout << "=== UnitTestReadWave() ===\n";
35 
36  Vector<BaseFloat> v, v2;
37 
38  std::cout << "<<<=== Reading waveform\n";
39 
40  {
41  std::ifstream is("test_data/test.wav", std::ios_base::binary);
42  WaveData wave;
43  wave.Read(is);
44  const Matrix<BaseFloat> data(wave.Data());
45  KALDI_ASSERT(data.NumRows() == 1);
46  v.Resize(data.NumCols());
47  v.CopyFromVec(data.Row(0));
48  }
49 
50  std::cout << "<<<=== Reading Vector<BaseFloat> waveform, prepared by matlab\n";
51  std::ifstream input(
52  "test_data/test_matlab.ascii"
53  );
54  KALDI_ASSERT(input.good());
55  v2.Read(input, false);
56  input.close();
57 
58  std::cout << "<<<=== Comparing freshly read waveform to 'libsndfile' waveform\n";
59  KALDI_ASSERT(v.Dim() == v2.Dim());
60  for (int32 i = 0; i < v.Dim(); i++) {
61  KALDI_ASSERT(v(i) == v2(i));
62  }
63  std::cout << "<<<=== Comparing done\n";
64 
65  // std::cout << "== The Waveform Samples == \n";
66  // std::cout << v;
67 
68  std::cout << "Test passed :)\n\n";
69 
70 }
void Read(std::istream &is)
Read() will throw on error.
Definition: wave-reader.cc:272
kaldi::int32 int32
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
const Matrix< BaseFloat > & Data() const
Definition: wave-reader.h:124
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
This class&#39;s purpose is to read in Wave files.
Definition: wave-reader.h:106
A class representing a vector.
Definition: kaldi-vector.h:406
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void Read(std::istream &in, bool binary, bool add=false)
Read function using C++ streams.

◆ UnitTestSimple()

static void UnitTestSimple ( )
static

Definition at line 76 of file feature-mfcc-test.cc.

References OfflineFeatureTpl< F >::Compute(), VectorBase< Real >::Dim(), FrameExtractionOptions::dither, MfccOptions::frame_opts, MfccOptions::htk_compat, MelBanksOptions::htk_mode, rnnlm::i, MelBanksOptions::low_freq, MfccOptions::mel_opts, FrameExtractionOptions::preemph_coeff, FrameExtractionOptions::remove_dc_offset, FrameExtractionOptions::round_to_power_of_two, and FrameExtractionOptions::window_type.

76  {
77  std::cout << "=== UnitTestSimple() ===\n";
78 
79  Vector<BaseFloat> v(100000);
81 
82  // init with noise
83  for (int32 i = 0; i < v.Dim(); i++) {
84  v(i) = (abs( i * 433024253 ) % 65535) - (65535 / 2);
85  }
86 
87  std::cout << "<<<=== Just make sure it runs... Nothing is compared\n";
88  // the parametrization object
89  MfccOptions op;
90  // trying to have same opts as baseline.
91  op.frame_opts.dither = 0.0;
92  op.frame_opts.preemph_coeff = 0.0;
93  op.frame_opts.window_type = "rectangular";
94  op.frame_opts.remove_dc_offset = false;
96  op.mel_opts.low_freq = 0.0;
97  op.mel_opts.htk_mode = true;
98  op.htk_compat = true;
99 
100  Mfcc mfcc(op);
101  // use default parameters
102 
103  // compute mfccs.
104  mfcc.Compute(v, 1.0, &m);
105 
106  // possibly dump
107  // std::cout << "== Output features == \n" << m;
108  std::cout << "Test passed :)\n\n";
109 }
MfccOptions contains basic options for computing MFCC features.
Definition: feature-mfcc.h:38
kaldi::int32 int32
MelBanksOptions mel_opts
Definition: feature-mfcc.h:40
FrameExtractionOptions frame_opts
Definition: feature-mfcc.h:39
A class representing a vector.
Definition: kaldi-vector.h:406
This templated class is intended for offline feature extraction, i.e.

◆ UnitTestVtln()

void UnitTestVtln ( )

Definition at line 620 of file feature-mfcc-test.cc.

References kaldi::AssertEqual(), rnnlm::i, KALDI_ASSERT, kaldi::RandUniform(), and MelBanks::VtlnWarpFreq().

Referenced by UnitTestFeat().

620  {
621  // Test the function VtlnWarpFreq.
622  BaseFloat low_freq = 10, high_freq = 7800,
623  vtln_low_cutoff = 20, vtln_high_cutoff = 7400;
624 
625  for (size_t i = 0; i < 100; i++) {
626  BaseFloat freq = 5000, warp_factor = 0.9 + RandUniform() * 0.2;
627  AssertEqual(MelBanks::VtlnWarpFreq(vtln_low_cutoff, vtln_high_cutoff,
628  low_freq, high_freq, warp_factor,
629  freq),
630  freq / warp_factor);
631 
632  AssertEqual(MelBanks::VtlnWarpFreq(vtln_low_cutoff, vtln_high_cutoff,
633  low_freq, high_freq, warp_factor,
634  low_freq),
635  low_freq);
636  AssertEqual(MelBanks::VtlnWarpFreq(vtln_low_cutoff, vtln_high_cutoff,
637  low_freq, high_freq, warp_factor,
638  high_freq),
639  high_freq);
640  BaseFloat freq2 = low_freq + (high_freq-low_freq) * RandUniform(),
641  freq3 = freq2 + (high_freq-freq2) * RandUniform(); // freq3>=freq2
642  BaseFloat w2 = MelBanks::VtlnWarpFreq(vtln_low_cutoff, vtln_high_cutoff,
643  low_freq, high_freq, warp_factor,
644  freq2);
645  BaseFloat w3 = MelBanks::VtlnWarpFreq(vtln_low_cutoff, vtln_high_cutoff,
646  low_freq, high_freq, warp_factor,
647  freq3);
648  KALDI_ASSERT(w3 >= w2); // increasing function.
649  BaseFloat w3dash = MelBanks::VtlnWarpFreq(vtln_low_cutoff, vtln_high_cutoff,
650  low_freq, high_freq, 1.0,
651  freq3);
652  AssertEqual(w3dash, freq3);
653  }
654 }
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
Definition: kaldi-math.h:151
float BaseFloat
Definition: kaldi-types.h:29
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
Definition: kaldi-math.h:276