All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
compute-plp-feats.cc File Reference
Include dependency graph for compute-plp-feats.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 27 of file compute-plp-feats.cc.

References VectorBase< Real >::AddRowSumMat(), VectorBase< Real >::AddVec(), OfflineFeatureTpl< F >::Compute(), WaveData::Data(), SequentialTableReader< Holder >::Done(), WaveData::Duration(), PlpOptions::frame_opts, ParseOptions::GetArg(), RandomAccessTableReaderMapped< Holder >::HasKey(), rnnlm::i, KALDI_ASSERT, KALDI_ERR, KALDI_LOG, KALDI_VLOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumRows(), TableWriter< Holder >::Open(), ParseOptions::PrintUsage(), ParseOptions::Read(), PlpOptions::Register(), ParseOptions::Register(), FrameExtractionOptions::samp_freq, WaveData::SampFreq(), SequentialTableReader< Holder >::Value(), RandomAccessTableReaderMapped< Holder >::Value(), and TableWriter< Holder >::Write().

27  {
28  try {
29  using namespace kaldi;
30  const char *usage =
31  "Create PLP feature files.\n"
32  "Usage: compute-plp-feats [options...] <wav-rspecifier> <feats-wspecifier>\n";
33 
34  // construct all the global objects
35  ParseOptions po(usage);
36  PlpOptions plp_opts;
37  bool subtract_mean = false;
38  BaseFloat vtln_warp = 1.0;
39  std::string vtln_map_rspecifier;
40  std::string utt2spk_rspecifier;
41  int32 channel = -1;
42  BaseFloat min_duration = 0.0;
43  // Define defaults for gobal options
44  std::string output_format = "kaldi";
45 
46  // Register the options
47  po.Register("output-format", &output_format, "Format of the output "
48  "files [kaldi, htk]");
49  po.Register("subtract-mean", &subtract_mean, "Subtract mean of each "
50  "feature file [CMS]. ");
51  po.Register("vtln-warp", &vtln_warp, "Vtln warp factor (only applicable "
52  "if vtln-map not specified)");
53  po.Register("vtln-map", &vtln_map_rspecifier, "Map from utterance or "
54  "speaker-id to vtln warp factor (rspecifier)");
55  po.Register("utt2spk", &utt2spk_rspecifier, "Utterance to speaker-id "
56  "map (if doing VTLN and you have warps per speaker)");
57  po.Register("channel", &channel, "Channel to extract (-1 -> expect mono, "
58  "0 -> left, 1 -> right)");
59  po.Register("min-duration", &min_duration, "Minimum duration of segments "
60  "to process (in seconds).");
61 
62  plp_opts.Register(&po);
63 
64  po.Read(argc, argv);
65 
66  if (po.NumArgs() != 2) {
67  po.PrintUsage();
68  exit(1);
69  }
70 
71  std::string wav_rspecifier = po.GetArg(1);
72 
73  std::string output_wspecifier = po.GetArg(2);
74 
75  Plp plp(plp_opts);
76 
77  SequentialTableReader<WaveHolder> reader(wav_rspecifier);
78  BaseFloatMatrixWriter kaldi_writer; // typedef to TableWriter<something>.
80 
81  if (utt2spk_rspecifier != "")
82  KALDI_ASSERT(vtln_map_rspecifier != "" && "the utt2spk option is only "
83  "needed if the vtln-map option is used.");
84  RandomAccessBaseFloatReaderMapped vtln_map_reader(vtln_map_rspecifier,
85  utt2spk_rspecifier);
86 
87  if (output_format == "kaldi") {
88  if (!kaldi_writer.Open(output_wspecifier))
89  KALDI_ERR << "Could not initialize output with wspecifier "
90  << output_wspecifier;
91  } else if (output_format == "htk") {
92  if (!htk_writer.Open(output_wspecifier))
93  KALDI_ERR << "Could not initialize output with wspecifier "
94  << output_wspecifier;
95  } else {
96  KALDI_ERR << "Invalid output_format string " << output_format;
97  }
98 
99  int32 num_utts = 0, num_success = 0;
100  for (; !reader.Done(); reader.Next()) {
101  num_utts++;
102  std::string utt = reader.Key();
103  const WaveData &wave_data = reader.Value();
104  if (wave_data.Duration() < min_duration) {
105  KALDI_WARN << "File: " << utt << " is too short ("
106  << wave_data.Duration() << " sec): producing no output.";
107  continue;
108  }
109  int32 num_chan = wave_data.Data().NumRows(), this_chan = channel;
110  { // This block works out the channel (0=left, 1=right...)
111  KALDI_ASSERT(num_chan > 0); // should have been caught in
112  // reading code if no channels.
113  if (channel == -1) {
114  this_chan = 0;
115  if (num_chan != 1)
116  KALDI_WARN << "Channel not specified but you have data with "
117  << num_chan << " channels; defaulting to zero";
118  } else {
119  if (this_chan >= num_chan) {
120  KALDI_WARN << "File with id " << utt << " has "
121  << num_chan << " channels but you specified channel "
122  << channel << ", producing no output.";
123  continue;
124  }
125  }
126  }
127  BaseFloat vtln_warp_local; // Work out VTLN warp factor.
128  if (vtln_map_rspecifier != "") {
129  if (!vtln_map_reader.HasKey(utt)) {
130  KALDI_WARN << "No vtln-map entry for utterance-id (or speaker-id) "
131  << utt;
132  continue;
133  }
134  vtln_warp_local = vtln_map_reader.Value(utt);
135  } else {
136  vtln_warp_local = vtln_warp;
137  }
138  if (plp_opts.frame_opts.samp_freq != wave_data.SampFreq())
139  KALDI_ERR << "Sample frequency mismatch: you specified "
140  << plp_opts.frame_opts.samp_freq << " but data has "
141  << wave_data.SampFreq() << " (use --sample-frequency "
142  << "option). Utterance is " << utt;
143 
144  SubVector<BaseFloat> waveform(wave_data.Data(), this_chan);
145  Matrix<BaseFloat> features;
146  try {
147  plp.Compute(waveform, vtln_warp_local, &features, NULL);
148  } catch (...) {
149  KALDI_WARN << "Failed to compute features for utterance "
150  << utt;
151  continue;
152  }
153  if (subtract_mean) {
154  Vector<BaseFloat> mean(features.NumCols());
155  mean.AddRowSumMat(1.0, features);
156  mean.Scale(1.0 / features.NumRows());
157  for (size_t i = 0; i < features.NumRows(); i++)
158  features.Row(i).AddVec(-1.0, mean);
159  }
160  if (output_format == "kaldi") {
161  kaldi_writer.Write(utt, features);
162  } else {
163  std::pair<Matrix<BaseFloat>, HtkHeader> p;
164  p.first.Resize(features.NumRows(), features.NumCols());
165  p.first.CopyFromMat(features);
166  HtkHeader header = {
167  features.NumRows(),
168  100000, // 10ms shift
169  static_cast<int16>(sizeof(float)*features.NumCols()),
170  013 | // PLP
171  020000 // C0 [no option currently to use energy in PLP.
172  };
173  p.second = header;
174  htk_writer.Write(utt, p);
175  }
176  if (num_utts % 10 == 0)
177  KALDI_LOG << "Processed " << num_utts << " utterances";
178  KALDI_VLOG(2) << "Processed features for key " << utt;
179  num_success++;
180  }
181  KALDI_LOG << " Done " << num_success << " out of " << num_utts
182  << " utterances.";
183  return (num_success != 0 ? 0 : 1);
184  } catch(const std::exception &e) {
185  std::cerr << e.what();
186  return -1;
187  }
188 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
bool Open(const std::string &wspecifier)
void AddRowSumMat(Real alpha, const MatrixBase< Real > &M, Real beta=1.0)
Does *this = alpha * (sum of rows of M) + beta * *this.
void Write(const std::string &key, const T &value) const
This class is for when you are reading something in random access, but it may actually be stored per-...
Definition: kaldi-table.h:431
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
const Matrix< BaseFloat > & Data() const
Definition: wave-reader.h:85
BaseFloat SampFreq() const
Definition: wave-reader.h:87
float BaseFloat
Definition: kaldi-types.h:29
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
FrameExtractionOptions frame_opts
Definition: feature-plp.h:43
void Register(OptionsItf *opts)
Definition: feature-plp.h:71
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:127
#define KALDI_WARN
Definition: kaldi-error.h:130
This class's purpose is to read in Wave files.
Definition: wave-reader.h:65
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
#define KALDI_VLOG(v)
Definition: kaldi-error.h:136
PlpOptions contains basic options for computing PLP features.
Definition: feature-plp.h:42
BaseFloat Duration() const
Definition: wave-reader.h:90
This templated class is intended for offline feature extraction, i.e.
#define KALDI_LOG
Definition: kaldi-error.h:133
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...
A structure containing the HTK header.
Definition: kaldi-matrix.h:873
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:479