shift-feats.cc
Go to the documentation of this file.
1 // featbin/shift-feats.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 // 2013-2015 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 #include "matrix/kaldi-matrix.h"
24 
25 namespace kaldi {
26  void ShiftFeatureMatrix(const Matrix<BaseFloat> &src, int32 shift,
27  Matrix<BaseFloat>* rearranged) {
28  for (int32 r = 0; r < src.NumRows(); r++) {
29  int32 src_r = r - shift;
30  if (src_r < 0) src_r = 0;
31  if (src_r >= src.NumRows()) src_r = src.NumRows() - 1;
32  rearranged->Row(r).CopyFromVec(src.Row(src_r));
33  }
34  }
35 }
36 
37 int main(int argc, char *argv[]) {
38  try {
39  using namespace kaldi;
40 
41  const char *usage =
42  "Copy features, and possibly shift them while maintaining the "
43  "num-frames.\n"
44  "Usage: shift-feats [options] <feature-rspecifier> "
45  "<feature-wspecifier>\n"
46  "or: shift-feats [options] <feats-rxfilename> <feats-wxfilename>\n"
47  "e.g.: shift-feats --shift=-1 foo.scp bar.ark\n"
48  "or: shift-feats --shift=1 foo.mat bar.mat\n"
49  "See also: copy-feats, copy-matrix, select-feats, subset-feats,\n"
50  "subsample-feats, splice-feats, paste-feats, concat-feats, "
51  "extract-feature-segments\n";
52 
53  ParseOptions po(usage);
54  bool binary = true;
55  int32 shift = 0;
56  po.Register("shift", &shift, "Number of frames by which to shift the "
57  "features.");
58  po.Register("binary", &binary, "Binary-mode output (not relevant if "
59  "writing to archive)");
60 
61  po.Read(argc, argv);
62 
63  if (po.NumArgs() != 2) {
64  po.PrintUsage();
65  exit(1);
66  }
67 
68  int32 num_done = 0, num_err = 0;
69 
70  if (ClassifyRspecifier(po.GetArg(1), NULL, NULL) != kNoRspecifier) {
71  SequentialBaseFloatMatrixReader feat_reader(po.GetArg(1));
72  BaseFloatMatrixWriter feat_writer(po.GetArg(2));
73 
74 
75  for (; !feat_reader.Done(); feat_reader.Next()) {
76  const std::string &key = feat_reader.Key();
77  const Matrix<BaseFloat> &src = feat_reader.Value();
78  if (src.NumRows() == 0) {
79  KALDI_WARN << "Empty matrix for key " << key;
80  num_err++;
81  continue;
82  }
83  Matrix<BaseFloat> rearranged(src.NumRows(), src.NumCols());
84  ShiftFeatureMatrix(src, shift, &rearranged);
85  feat_writer.Write(key, rearranged);
86  num_done++;
87  }
88 
89  KALDI_LOG << "Shifted " << num_done << " features by "
90  << shift << " frames; " << num_err << " with errors.";
91  return (num_done > 0 ? 0 : 1);
92  } else {
93  std::string feat_rxfilename = po.GetArg(1),
94  feat_wxfilename = po.GetArg(2);
96  ReadKaldiObject(feat_rxfilename, &src);
97  if (src.NumRows() == 0)
98  KALDI_ERR << "Empty input matrix";
99  Matrix<BaseFloat> rearranged(src.NumRows(), src.NumCols());
100  ShiftFeatureMatrix(src, shift, &rearranged);
101  WriteKaldiObject(rearranged, feat_wxfilename, binary);
102  // we do not print any log messages here
103  }
104  } catch(const std::exception &e) {
105  std::cerr << e.what();
106  return -1;
107  }
108 }
109 
110 
111 /*
112 test:
113  echo "foo [ 1 1; 2 2; 3 3 ]" | shift-feats --shift=1 ark:- ark,t:-
114  outputs:
115  foo [
116  1 1
117  1 1
118  2 2 ]
119 
120 
121  echo "[ 1 1; 2 2; 3 3 ]" | ./shift-feats --print-args=false --binary=false \
122  --shift=1 - -
123 */
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: shift-feats.cc:37
void ShiftFeatureMatrix(const Matrix< BaseFloat > &src, int32 shift, Matrix< BaseFloat > *rearranged)
Definition: shift-feats.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 Register(const std::string &name, bool *ptr, const std::string &doc)
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *rxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
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 WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
#define KALDI_LOG
Definition: kaldi-error.h:153