process-pitch-feats.cc
Go to the documentation of this file.
1 // featbin/process-pitch-feats.cc
2 
3 // Copyright 2013 Bagher BabaAli
4 // Johns Hopkins University (author: Daniel Povey)
5 //
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 
24 namespace kaldi {
25 
26 // For the probability-of-voicing features (the first element of
27 // each row), change p -> log ((p + 0.0001) / (1.00001 - p))
28 // This makes it more Gaussian; otherwise it clumps up near
29 // the edges of the range [0, 1].
31  int32 num_frames = mat->NumRows();
32  for (int32 i = 0; i < num_frames; i++) {
33  BaseFloat p = (*mat)(i, 0);
34  KALDI_ASSERT(p >= 0.0 && p <= 1.0);
35  (*mat)(i, 0) = Log((p + 0.0001) / (1.0001 - p));
36  }
37 }
38 
40  int32 num_frames = mat->NumRows();
41  for (int32 i = 0; i < num_frames; i++) {
42  KALDI_ASSERT((*mat)(i, 1) > 0.0);
43  (*mat)(i, 1) = Log((*mat)(i, 1));
44  }
45 }
46 
47 // Subtract the moving average over a largish window
48 // (e.g. 151 frames)
49 void SubtractMovingAverage(int32 normalization_window_size,
50  Matrix<BaseFloat> *mat) {
51  int32 num_frames = mat->NumRows();
52  Vector<BaseFloat> temp_pitch(num_frames);
53  Matrix<BaseFloat> &features = *mat;
54  int32 i;
55  for (i = 0; i < num_frames; i++)
56  temp_pitch(i) = features(i, 1);
57 
58  // Moving Window Normalization
59  BaseFloat mean = 0.0;
60  int32 mid_win = (normalization_window_size - 1) / 2;
61  for (i = 0; (i < num_frames) && (i < normalization_window_size); i++) {
62  mean += features(i, 1);
63  }
64  mean /= i;
65 
66  if (num_frames <= normalization_window_size) {
67  for (i = 0; i < num_frames; i++) {
68  features(i, 1) -= mean;
69  }
70  } else {
71  for (i = 0; i <= mid_win; i++) {
72  features(i, 1) -= mean;
73  }
74  for (i = (mid_win + 1); i < num_frames; i++) {
75  if (i + (mid_win + 1) < num_frames)
76  mean -= (temp_pitch(i - (mid_win + 1)) -
77  temp_pitch(i + (mid_win + 1))) / normalization_window_size;
78  features(i,1) -= mean;
79  }
80  }
81 }
82 
83 // Set to the moving average over a small window, e.g. 5 frames.
84 void SetToMovingAverage(int32 average_window_size,
85  Matrix<BaseFloat> *mat) {
86  int32 num_frames = mat->NumRows();
87  Matrix<BaseFloat> &features = *mat;
88  Vector<BaseFloat> temp_pitch(num_frames);
89  int32 width = (average_window_size - 1) / 2, i;
90  // e.g. if average_window_size is 5, width will equal 2.
91 
92  for (i = width; i < num_frames - width ; i++) {
93  temp_pitch(i) = features(i, 1);
94  for(int j = 1; j <= width; ++j) {
95  temp_pitch(i) += (features(i - j, 1) + features(i + j, 1));
96  }
97  temp_pitch(i) /= (2 * width + 1);
98  }
99  for (i = width; i < num_frames - width; i++)
100  features(i, 1) = temp_pitch(i);
101 }
102 
103 } // namespace kaldi
104 
105 int main(int argc, char *argv[]) {
106  try {
107  using namespace kaldi;
108  const char *usage =
109  "This is a rather special-purpose program which processes 2-dimensional\n"
110  "features consisting of (prob-of-voicing, pitch) into something suitable\n"
111  "to put into a speech recognizer. First use interpolate-feats\n"
112  "Usage: process-pitch-feats [options...] <feats-rspecifier> <feats-wspecifier>\n";
113 
114 
115  // construct all the global objects
116  ParseOptions po(usage);
117 
118  int32 normalization_window_size = 151; // should be odd number
119  int32 average_window_size = 5;
120 
121  // Register the options
122  po.Register("normalization-window-size",
123  &normalization_window_size, "Size of window used for "
124  "moving window nomalization (must be odd).");
125  po.Register("average-window-size",
126  &average_window_size,
127  "Size of moving average window (must be odd).");
128 
129  // parse options (+filling the registered variables)
130  po.Read(argc, argv);
131 
132  if (po.NumArgs() != 2) {
133  po.PrintUsage();
134  exit(1);
135  }
136 
137  KALDI_ASSERT(average_window_size > 0 && average_window_size % 2 == 1 &&
138  "--average-window-size option must be an odd positive number.");
139  KALDI_ASSERT(normalization_window_size > 0 && normalization_window_size % 2 == 1 &&
140  "--normalization-window-size option must be an odd positive number.");
141 
142  std::string input_rspecifier = po.GetArg(1);
143  std::string output_wspecifier = po.GetArg(2);
144 
145  SequentialBaseFloatMatrixReader reader(input_rspecifier);
146  BaseFloatMatrixWriter kaldi_writer; // typedef to TableWriter<something>.
147 
148  if (!kaldi_writer.Open(output_wspecifier))
149  KALDI_ERR << "Could not initialize output with wspecifier "
150  << output_wspecifier;
151 
152  int32 num_done = 0, num_err = 0;
153 
154  for (; !reader.Done(); reader.Next()) {
155  std::string utt = reader.Key();
156  Matrix<BaseFloat> features = reader.Value();
157  int num_frames = features.NumRows();
158 
159  if (num_frames == 0 && features.NumCols() != 2) {
160  KALDI_WARN << "Feature file has bad size "
161  << features.NumRows() << " by " << features.NumCols();
162  num_err++;
163  continue;
164  }
165 
166  ProcessPovFeatures(&features);
167  TakeLogOfPitch(&features);
168  SubtractMovingAverage(normalization_window_size, &features);
169  SetToMovingAverage(average_window_size, &features);
170  kaldi_writer.Write(utt, features);
171  num_done++;
172 
173  if (num_done % 10 == 0)
174  KALDI_LOG << "Processed " << num_done << " utterances";
175  KALDI_VLOG(2) << "Processed features for key " << utt;
176  }
177  KALDI_LOG << "Done " << num_done << " utterances, " << num_err
178  << " with errors.";
179  return (num_done != 0 ? 0 : 1);
180  } catch(const std::exception &e) {
181  std::cerr << e.what();
182  return -1;
183  }
184 }
185 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool Open(const std::string &wspecifier)
void SubtractMovingAverage(int32 normalization_window_size, Matrix< BaseFloat > *mat)
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
int main(int argc, char *argv[])
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void TakeLogOfPitch(Matrix< BaseFloat > *mat)
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
double Log(double x)
Definition: kaldi-math.h:100
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
A class representing a vector.
Definition: kaldi-vector.h:406
void ProcessPovFeatures(Matrix< BaseFloat > *mat)
#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
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
#define KALDI_LOG
Definition: kaldi-error.h:153
void SetToMovingAverage(int32 average_window_size, Matrix< BaseFloat > *mat)