concat-feats.cc
Go to the documentation of this file.
1 // featbin/concat-feats.cc
2 
3 // Copyright 2013 Johns Hopkins University (Author: Daniel Povey)
4 // 2015 Tom Ko
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 
22 #include "base/kaldi-common.h"
23 #include "util/common-utils.h"
24 #include "matrix/kaldi-matrix.h"
25 
26 namespace kaldi {
27 
28 /*
29  This function concatenates several sets of feature vectors
30  to form a longer set. The length of the output will be equal
31  to the sum of lengths of the inputs but the dimension will be
32  the same to the inputs.
33 */
34 
35 void ConcatFeats(const std::vector<Matrix<BaseFloat> > &in,
36  Matrix<BaseFloat> *out) {
37  KALDI_ASSERT(in.size() >= 1);
38  int32 tot_len = in[0].NumRows(),
39  dim = in[0].NumCols();
40  for (int32 i = 1; i < in.size(); i++) {
41  KALDI_ASSERT(in[i].NumCols() == dim);
42  tot_len += in[i].NumRows();
43  }
44  out->Resize(tot_len, dim);
45  int32 len_offset = 0;
46  for (int32 i = 0; i < in.size(); i++) {
47  int32 this_len = in[i].NumRows();
48  out->Range(len_offset, this_len, 0, dim).CopyFromMat(
49  in[i]);
50  len_offset += this_len;
51  }
52 }
53 
54 
55 }
56 
57 int main(int argc, char *argv[]) {
58  try {
59  using namespace kaldi;
60  using namespace std;
61 
62  const char *usage =
63  "Concatenate feature files (assuming they have the same dimensions),\n"
64  "so the output file has the sum of the num-frames of the inputs.\n"
65  "Usage: concat-feats <in-rxfilename1> <in-rxfilename2> [<in-rxfilename3> ...] <out-wxfilename>\n"
66  " e.g. concat-feats mfcc/foo.ark:12343 mfcc/foo.ark:56789 -\n"
67  "See also: copy-feats, append-vector-to-feats, paste-feats\n";
68 
69  ParseOptions po(usage);
70 
71  bool binary = true;
72  po.Register("binary", &binary, "If true, output files in binary "
73  "(only relevant for single-file operation, i.e. no tables)");
74 
75  po.Read(argc, argv);
76 
77  if (po.NumArgs() < 3) {
78  po.PrintUsage();
79  exit(1);
80  }
81 
82  std::vector<Matrix<BaseFloat> > feats(po.NumArgs() - 1);
83  for (int32 i = 1; i < po.NumArgs(); i++)
84  ReadKaldiObject(po.GetArg(i), &(feats[i-1]));
85  Matrix<BaseFloat> output;
86  ConcatFeats(feats, &output);
87  std::string output_wxfilename = po.GetArg(po.NumArgs());
88  WriteKaldiObject(output, output_wxfilename, binary);
89 
90  // This will tend to produce too much output if we have a logging mesage.
91  // KALDI_LOG << "Wrote concatenated features to " << output_wxfilename;
92  return 0;
93  } catch(const std::exception &e) {
94  std::cerr << e.what();
95  return -1;
96  }
97 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void ConcatFeats(const std::vector< Matrix< BaseFloat > > &in, Matrix< BaseFloat > *out)
Definition: concat-feats.cc:35
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
kaldi::int32 int32
void Register(const std::string &name, bool *ptr, const std::string &doc)
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
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
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).
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
SubMatrix< Real > Range(const MatrixIndexT row_offset, const MatrixIndexT num_rows, const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Return a sub-part of matrix.
Definition: kaldi-matrix.h:202
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
int main(int argc, char *argv[])
Definition: concat-feats.cc:57