All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
select-voiced-frames.cc File Reference
Include dependency graph for select-voiced-frames.cc:

Go to the source code of this file.

Functions

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

Function Documentation

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

Definition at line 28 of file select-voiced-frames.cc.

References VectorBase< Real >::Dim(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, KALDI_ASSERT, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), ParseOptions::Read(), MatrixBase< Real >::Row(), VectorBase< Real >::Sum(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

28  {
29  try {
30  using namespace kaldi;
31  using kaldi::int32;
32 
33  const char *usage =
34  "Select a subset of frames of the input files, based on the output of\n"
35  "compute-vad or a similar program (a vector of length num-frames,\n"
36  "containing 1.0 for voiced, 0.0 for unvoiced).\n"
37  "Usage: select-voiced-frames [options] <feats-rspecifier> "
38  " <vad-rspecifier> <feats-wspecifier>\n"
39  "E.g.: select-voiced-frames [options] scp:feats.scp scp:vad.scp ark:-\n";
40 
41  ParseOptions po(usage);
42  po.Read(argc, argv);
43 
44  if (po.NumArgs() != 3) {
45  po.PrintUsage();
46  exit(1);
47  }
48 
49  std::string feat_rspecifier = po.GetArg(1),
50  vad_rspecifier = po.GetArg(2),
51  feat_wspecifier = po.GetArg(3);
52 
53  SequentialBaseFloatMatrixReader feat_reader(feat_rspecifier);
54  RandomAccessBaseFloatVectorReader vad_reader(vad_rspecifier);
55  BaseFloatMatrixWriter feat_writer(feat_wspecifier);
56 
57  int32 num_done = 0, num_err = 0;
58 
59  for (;!feat_reader.Done(); feat_reader.Next()) {
60  std::string utt = feat_reader.Key();
61  const Matrix<BaseFloat> &feat = feat_reader.Value();
62  if (feat.NumRows() == 0) {
63  KALDI_WARN << "Empty feature matrix for utterance " << utt;
64  num_err++;
65  continue;
66  }
67  if (!vad_reader.HasKey(utt)) {
68  KALDI_WARN << "No VAD input found for utterance " << utt;
69  num_err++;
70  continue;
71  }
72  const Vector<BaseFloat> &voiced = vad_reader.Value(utt);
73 
74  if (feat.NumRows() != voiced.Dim()) {
75  KALDI_WARN << "Mismatch in number for frames " << feat.NumRows()
76  << " for features and VAD " << voiced.Dim()
77  << ", for utterance " << utt;
78  num_err++;
79  continue;
80  }
81  if (voiced.Sum() == 0.0) {
82  KALDI_WARN << "No features were judged as voiced for utterance "
83  << utt;
84  num_err++;
85  continue;
86  }
87  int32 dim = 0;
88  for (int32 i = 0; i < voiced.Dim(); i++)
89  if (voiced(i) != 0.0)
90  dim++;
91  Matrix<BaseFloat> voiced_feat(dim, feat.NumCols());
92  int32 index = 0;
93  for (int32 i = 0; i < feat.NumRows(); i++) {
94  if (voiced(i) != 0.0) {
95  KALDI_ASSERT(voiced(i) == 1.0); // should be zero or one.
96  voiced_feat.Row(index).CopyFromVec(feat.Row(i));
97  index++;
98  }
99  }
100  KALDI_ASSERT(index == dim);
101  feat_writer.Write(utt, voiced_feat);
102  num_done++;
103  }
104 
105  KALDI_LOG << "Done selecting voiced frames; processed "
106  << num_done << " utterances, "
107  << num_err << " had errors.";
108  return (num_done != 0 ? 0 : 1);
109  } catch(const std::exception &e) {
110  std::cerr << e.what();
111  return -1;
112  }
113 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
Real Sum() const
Returns sum of the elements.
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
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_WARN
Definition: kaldi-error.h:130
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
#define KALDI_LOG
Definition: kaldi-error.h:133
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:62