feature-plp-test.cc
Go to the documentation of this file.
1 // feat/feature-plp-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-plp.h"
24 #include "base/kaldi-math.h"
26 #include "feat/wave-reader.h"
27 
28 using namespace kaldi;
29 
30 
31 
32 
33 
36 static void UnitTestSimple() {
37  std::cout << "=== UnitTestSimple() ===\n";
38 
39  Vector<BaseFloat> v(100000);
41 
42  // init with noise
43  for (int32 i = 0; i < v.Dim(); i++) {
44  v(i) = (abs( i * 433024253 ) % 65535) - (65535 / 2);
45  }
46 
47  std::cout << "<<<=== Just make sure it runs... Nothing is compared\n";
48  // the parametrization object
49  PlpOptions op;
50  // trying to have same opts as baseline.
51  op.frame_opts.dither = 0.0;
52  op.frame_opts.preemph_coeff = 0.0;
53  op.frame_opts.window_type = "rectangular";
54  op.frame_opts.remove_dc_offset = false;
56  op.mel_opts.low_freq = 0.0;
57 // op.htk_compat = true;
58 
59  Plp plp(op);
60  // use default parameters
61 
62  // compute mfccs.
63  plp.Compute(v, 1.0, &m);
64 
65  // possibly dump
66  // std::cout << "== Output features == \n" << m;
67  std::cout << "Test passed :)\n\n";
68 }
69 
70 
71 static void UnitTestHTKCompare1() {
72  std::cout << "=== UnitTestHTKCompare1() ===\n";
73 
74  std::ifstream is("test_data/test.wav", std::ios_base::binary);
75  WaveData wave;
76  wave.Read(is);
77  KALDI_ASSERT(wave.Data().NumRows() == 1);
78  SubVector<BaseFloat> waveform(wave.Data(), 0);
79 
80  // read the HTK features
81  Matrix<BaseFloat> htk_features;
82  {
83  std::ifstream is("test_data/test.wav.plp_htk.1",
84  std::ios::in | std::ios_base::binary);
85  bool ans = ReadHtk(is, &htk_features, 0);
86  KALDI_ASSERT(ans);
87  }
88 
89  // use plp with default configuration...
90  PlpOptions op;
91  op.frame_opts.dither = 0.0;
92  op.frame_opts.preemph_coeff = 0.0;
93  op.frame_opts.window_type = "hamming";
94  op.frame_opts.remove_dc_offset = false;
96  op.mel_opts.low_freq = 0.0;
97  op.htk_compat = true;
98  op.use_energy = false; // C0 not energy.
99  op.cepstral_scale = 1.0;
100 
101  Plp plp(op);
102 
103  // calculate kaldi features
104  Matrix<BaseFloat> kaldi_raw_features;
105  plp.Compute(waveform, 1.0, &kaldi_raw_features);
106 
107  DeltaFeaturesOptions delta_opts;
108  Matrix<BaseFloat> kaldi_features;
109  ComputeDeltas(delta_opts,
110  kaldi_raw_features,
111  &kaldi_features);
112 
113  // compare the results
114  bool passed = true;
115  int32 i_old = -1;
116  KALDI_ASSERT(kaldi_features.NumRows() == htk_features.NumRows());
117  KALDI_ASSERT(kaldi_features.NumCols() == htk_features.NumCols());
118  // Ignore ends-- we make slightly different choices than
119  // HTK about how to treat the deltas at the ends.
120  for (int32 i = 10; i+10 < kaldi_features.NumRows(); i++) {
121  for (int32 j = 0; j < kaldi_features.NumCols(); j++) {
122  BaseFloat a = kaldi_features(i, j), b = htk_features(i, j);
123  if ((std::abs(b - a)) > 0.10) { //<< TOLERANCE TO DIFFERENCES!!!!!
124  // print the non-matching data only once per-line
125  if (i_old != i) {
126  std::cout << "\n\n\n[HTK-row: " << i << "] " << htk_features.Row(i) << "\n";
127  std::cout << "[Kaldi-row: " << i << "] " << kaldi_features.Row(i) << "\n\n\n";
128  i_old = i;
129  }
130  // print indices of non-matching cells
131  std::cout << "[" << i << ", " << j << "]";
132  passed = false;
133  }}}
134  if (!passed) KALDI_ERR << "Test failed";
135 
136  // write the htk features for later inspection
137  HtkHeader header = {
138  kaldi_features.NumRows(),
139  100000, // 10ms
140  static_cast<int16>(sizeof(float)*kaldi_features.NumCols()),
141  021413 // PLP_D_A_0
142  };
143  {
144  std::ofstream os("tmp.test.wav.plp_kaldi.1",
145  std::ios::out|std::ios::binary);
146  WriteHtk(os, kaldi_features, header);
147  }
148 
149  std::cout << "Test passed :)\n\n";
150 
151  unlink("tmp.test.wav.plp_kaldi.1");
152 }
153 
154 
155 
156 
157 static void UnitTestFeat() {
158  UnitTestSimple();
160 }
161 
162 
163 
164 
165 int main() {
166  try {
167  for (int i = 0; i < 5; i++)
168  UnitTestFeat();
169  std::cout << "Tests succeeded.\n";
170  return 0;
171  } catch (const std::exception &e) {
172  std::cerr << e.what();
173  return 1;
174  }
175 }
176 
177 
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 void UnitTestHTKCompare1()
void Compute(const VectorBase< BaseFloat > &wave, BaseFloat vtln_warp, Matrix< BaseFloat > *output)
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
bool WriteHtk(std::ostream &os, const MatrixBase< Real > &M, HtkHeader htk_hdr)
static void UnitTestSimple()
kaldi::int32 int32
MelBanksOptions mel_opts
Definition: feature-plp.h:44
const Matrix< BaseFloat > & Data() const
Definition: wave-reader.h:124
float BaseFloat
Definition: kaldi-types.h:29
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
FrameExtractionOptions frame_opts
Definition: feature-plp.h:43
#define KALDI_ERR
Definition: kaldi-error.h:147
BaseFloat cepstral_scale
Definition: feature-plp.h:52
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
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)
PlpOptions contains basic options for computing PLP features.
Definition: feature-plp.h:42
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.
int main()
A structure containing the HTK header.
Definition: kaldi-matrix.h:955
static void UnitTestFeat()
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501