feature-functions-test.cc
Go to the documentation of this file.
1 // feat/feature-functions-test.cc
2 
3 // Copyright 2013 Johns Hopkins University (author: Daniel Povey)
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 
29 // TODO: some of the other functions should be tested.
30 namespace kaldi {
31 
33  for (int32 i = 0; i < 1000; i++) {
34  int32 num_frames = 1 + (Rand() % 10 * 10);
35  int32 dim = 1 + Rand() % 10;
37  opts.center = (Rand() % 2 == 0);
38  opts.normalize_variance = (Rand() % 2 == 0);
39  opts.cmn_window = 5 + Rand() % 50;
40  opts.min_window = 1 + Rand() % 100;
41  if (opts.min_window > opts.cmn_window)
42  opts.min_window = opts.cmn_window;
43 
44  Matrix<BaseFloat> feats(num_frames, dim),
45  output_feats(num_frames, dim),
46  output_feats2(num_frames, dim);
47  feats.SetRandn();
48  SlidingWindowCmn(opts, feats, &output_feats);
49 
50  for (int32 t = 0; t < num_frames; t++) {
51  int32 window_begin, window_end;
52  if (opts.center) {
53  window_begin = t - (opts.cmn_window / 2),
54  window_end = window_begin + opts.cmn_window;
55  int32 shift = 0;
56  if (window_begin < 0)
57  shift = -window_begin;
58  else if (window_end > num_frames)
59  shift = num_frames - window_end;
60  window_end += shift;
61  window_begin += shift;
62  } else {
63  window_begin = t - opts.cmn_window;
64  window_end = t + 1;
65  if (window_end < opts.min_window)
66  window_end = opts.min_window;
67  }
68  if (window_begin < 0) window_begin = 0;
69  if (window_end > num_frames) window_end = num_frames;
70  int32 window_size = window_end - window_begin;
71  for (int32 d = 0; d < dim; d++) {
72  double sum = 0.0, sumsq = 0.0;
73  for (int32 t2 = window_begin; t2 < window_end; t2++) {
74  sum += feats(t2, d);
75  sumsq += feats(t2, d) * feats(t2, d);
76  }
77  double mean = sum / window_size, uncentered_covar = sumsq / window_size,
78  covar = uncentered_covar - mean * mean;
79  covar = std::max(covar, 1.0e-20);
80  double data = feats(t, d),
81  norm_data = data - mean;
82  if (opts.normalize_variance) {
83  if (window_size == 1) norm_data = 0.0;
84  else norm_data /= sqrt(covar);
85  }
86  output_feats2(t, d) = norm_data;
87  }
88  }
89  if (! output_feats.ApproxEqual(output_feats2, 0.0001)) {
90  KALDI_ERR << "Features differ " << output_feats << " vs. " << output_feats2;
91  }
92  }
93 }
94 
95 
96 }
97 
98 
99 
100 int main() {
101  using namespace kaldi;
102  try {
104  std::cout << "Tests succeeded.\n";
105  return 0;
106  } catch (const std::exception &e) {
107  std::cerr << e.what();
108  return 1;
109  }
110 }
111 
112 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void UnitTestOnlineCmvn()
int main()
kaldi::int32 int32
#define KALDI_ERR
Definition: kaldi-error.h:147
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
void SlidingWindowCmn(const SlidingWindowCmnOptions &opts, const MatrixBase< BaseFloat > &input, MatrixBase< BaseFloat > *output)
Applies sliding-window cepstral mean and/or variance normalization.