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

Go to the source code of this file.

Functions

static void UnitTestSimple ()
 
static void UnitTestHTKCompare1 ()
 
static void UnitTestFeat ()
 
int main ()
 

Function Documentation

◆ main()

int main ( )

Definition at line 165 of file feature-plp-test.cc.

References rnnlm::i, and UnitTestFeat().

165  {
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 }
static void UnitTestFeat()

◆ UnitTestFeat()

static void UnitTestFeat ( )
static

Definition at line 157 of file feature-plp-test.cc.

References UnitTestHTKCompare1(), and kaldi::UnitTestSimple().

Referenced by main().

157  {
158  UnitTestSimple();
160 }
static void UnitTestHTKCompare1()
static void UnitTestSimple()

◆ UnitTestHTKCompare1()

static void UnitTestHTKCompare1 ( )
static

Definition at line 71 of file feature-plp-test.cc.

References PlpOptions::cepstral_scale, OfflineFeatureTpl< F >::Compute(), kaldi::ComputeDeltas(), WaveData::Data(), FrameExtractionOptions::dither, PlpOptions::frame_opts, PlpOptions::htk_compat, rnnlm::i, rnnlm::j, KALDI_ASSERT, KALDI_ERR, MelBanksOptions::low_freq, PlpOptions::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(), PlpOptions::use_energy, FrameExtractionOptions::window_type, and kaldi::WriteHtk().

Referenced by UnitTestFeat().

71  {
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 }
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
bool WriteHtk(std::ostream &os, const MatrixBase< Real > &M, HtkHeader htk_hdr)
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
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)
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.
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

◆ UnitTestSimple()

static void UnitTestSimple ( )
static

Definition at line 36 of file feature-plp-test.cc.

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

36  {
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 }
kaldi::int32 int32
MelBanksOptions mel_opts
Definition: feature-plp.h:44
FrameExtractionOptions frame_opts
Definition: feature-plp.h:43
A class representing a vector.
Definition: kaldi-vector.h:406
PlpOptions contains basic options for computing PLP features.
Definition: feature-plp.h:42
This templated class is intended for offline feature extraction, i.e.