shift-feats.cc File Reference
Include dependency graph for shift-feats.cc:

Go to the source code of this file.

Namespaces

 kaldi
 This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for mispronunciations detection tasks, the reference:
 

Functions

void ShiftFeatureMatrix (const Matrix< BaseFloat > &src, int32 shift, Matrix< BaseFloat > *rearranged)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 37 of file shift-feats.cc.

References kaldi::ClassifyRspecifier(), ParseOptions::GetArg(), KALDI_ERR, KALDI_LOG, KALDI_WARN, kaldi::kNoRspecifier, ParseOptions::NumArgs(), MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadKaldiObject(), ParseOptions::Register(), kaldi::ShiftFeatureMatrix(), and kaldi::WriteKaldiObject().

37  {
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 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
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
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
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
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
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