add-deltas.cc
Go to the documentation of this file.
1 // featbin/add-deltas.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
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 #include "base/kaldi-common.h"
21 #include "util/common-utils.h"
22 #include "feat/feature-functions.h"
23 #include "matrix/kaldi-matrix.h"
24 
25 
26 int main(int argc, char *argv[]) {
27  try {
28  using namespace kaldi;
29 
30  const char *usage =
31  "Add deltas (typically to raw mfcc or plp features\n"
32  "Usage: add-deltas [options] in-rspecifier out-wspecifier\n";
34  int32 truncate = 0;
35  ParseOptions po(usage);
36  po.Register("truncate", &truncate, "If nonzero, first truncate features to this dimension.");
37  opts.Register(&po);
38 
39  po.Read(argc, argv);
40 
41  if (po.NumArgs() != 2) {
42  po.PrintUsage();
43  exit(1);
44  }
45 
46  std::string rspecifier = po.GetArg(1);
47  std::string wspecifier = po.GetArg(2);
48 
49  BaseFloatMatrixWriter feat_writer(wspecifier);
50  SequentialBaseFloatMatrixReader feat_reader(rspecifier);
51  for (; !feat_reader.Done(); feat_reader.Next()) {
52  std::string key = feat_reader.Key();
53  const Matrix<BaseFloat> &feats = feat_reader.Value();
54 
55  if (feats.NumRows() == 0) {
56  KALDI_WARN << "Empty feature matrix for key " << key;
57  continue;
58  }
59  Matrix<BaseFloat> new_feats;
60  if (truncate != 0) {
61  if (truncate > feats.NumCols())
62  KALDI_ERR << "Cannot truncate features as dimension " << feats.NumCols()
63  << " is smaller than truncation dimension.";
64  SubMatrix<BaseFloat> feats_sub(feats, 0, feats.NumRows(), 0, truncate);
65  ComputeDeltas(opts, feats_sub, &new_feats);
66  } else
67  ComputeDeltas(opts, feats, &new_feats);
68  feat_writer.Write(key, new_feats);
69  }
70  return 0;
71  } catch(const std::exception &e) {
72  std::cerr << e.what();
73  return -1;
74  }
75 }
76 
77 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int main(int argc, char *argv[])
Definition: add-deltas.cc:26
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].
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 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
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).
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)
void Register(OptionsItf *opts)
Sub-matrix representation.
Definition: kaldi-matrix.h:988