feature-mfcc-test.cc
Go to the documentation of this file.
1 // feat/feature-mfcc-test.cc
2 
3 // Copyright 2009-2011 Karel Vesely; Petr Motlicek
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 
21 #include <iostream>
22 
23 #include "feat/feature-mfcc.h"
24 #include "base/kaldi-math.h"
26 #include "feat/wave-reader.h"
27 
28 using namespace kaldi;
29 
30 
31 
32 static void UnitTestReadWave() {
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 }
71 
72 
73 
76 static void UnitTestSimple() {
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 }
110 
111 
112 static void UnitTestHTKCompare1() {
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 }
194 
195 
196 static void UnitTestHTKCompare2() {
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 }
278 
279 
280 static void UnitTestHTKCompare3() {
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 }
363 
364 
365 static void UnitTestHTKCompare4() {
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 }
446 
447 
448 static void UnitTestHTKCompare5() {
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 }
534 
535 static void UnitTestHTKCompare6() {
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 }
619 
620 void UnitTestVtln() {
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 }
655 
656 static void UnitTestFeat() {
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 }
671 
672 
673 
674 int main() {
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 }
685 
686 
void Read(std::istream &is)
Read() will throw on error.
Definition: wave-reader.cc:272
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
static BaseFloat VtlnWarpFreq(BaseFloat vtln_low_cutoff, BaseFloat vtln_high_cutoff, BaseFloat low_freq, BaseFloat high_freq, BaseFloat vtln_warp_factor, BaseFloat freq)
void Compute(const VectorBase< BaseFloat > &wave, BaseFloat vtln_warp, Matrix< BaseFloat > *output)
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
Definition: kaldi-math.h:151
static void UnitTestReadWave()
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
static void UnitTestHTKCompare6()
bool WriteHtk(std::ostream &os, const MatrixBase< Real > &M, HtkHeader htk_hdr)
static void UnitTestSimple()
static void UnitTestHTKCompare1()
kaldi::int32 int32
static void UnitTestHTKCompare2()
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
void UnitTestVtln()
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).
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
static void UnitTestFeat()
FrameExtractionOptions frame_opts
Definition: feature-mfcc.h:39
static void UnitTestHTKCompare3()
#define KALDI_ERR
Definition: kaldi-error.h:147
static void UnitTestHTKCompare5()
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
int main()
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
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)
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
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
void Read(std::istream &in, bool binary, bool add=false)
Read function using C++ streams.
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501
static void UnitTestHTKCompare4()