feature-common-inl.h
Go to the documentation of this file.
1 // feat/feature-common-inl.h
2 
3 // Copyright 2016 Johns Hopkins University (author: Daniel Povey)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #ifndef KALDI_FEAT_FEATURE_COMMON_INL_H_
21 #define KALDI_FEAT_FEATURE_COMMON_INL_H_
22 
23 #include "feat/resample.h"
24 // Do not include this file directly. It is included by feat/feature-common.h
25 
26 namespace kaldi {
27 
28 template <class F>
30  const VectorBase<BaseFloat> &wave,
31  BaseFloat sample_freq,
32  BaseFloat vtln_warp,
33  Matrix<BaseFloat> *output) {
34  KALDI_ASSERT(output != NULL);
35  BaseFloat new_sample_freq = computer_.GetFrameOptions().samp_freq;
36  if (sample_freq == new_sample_freq) {
37  Compute(wave, vtln_warp, output);
38  } else {
39  if (new_sample_freq < sample_freq &&
40  ! computer_.GetFrameOptions().allow_downsample)
41  KALDI_ERR << "Waveform and config sample Frequency mismatch: "
42  << sample_freq << " .vs " << new_sample_freq
43  << " (use --allow-downsample=true to allow "
44  << " downsampling the waveform).";
45  else if (new_sample_freq > sample_freq &&
46  ! computer_.GetFrameOptions().allow_upsample)
47  KALDI_ERR << "Waveform and config sample Frequency mismatch: "
48  << sample_freq << " .vs " << new_sample_freq
49  << " (use --allow-upsample=true option to allow "
50  << " upsampling the waveform).";
51  // Resample the waveform.
52  Vector<BaseFloat> resampled_wave(wave);
53  ResampleWaveform(sample_freq, wave,
54  new_sample_freq, &resampled_wave);
55  Compute(resampled_wave, vtln_warp, output);
56  }
57 }
58 
59 template <class F>
61  const VectorBase<BaseFloat> &wave,
62  BaseFloat vtln_warp,
63  Matrix<BaseFloat> *output) {
64  KALDI_ASSERT(output != NULL);
65  int32 rows_out = NumFrames(wave.Dim(), computer_.GetFrameOptions()),
66  cols_out = computer_.Dim();
67  if (rows_out == 0) {
68  output->Resize(0, 0);
69  return;
70  }
71  output->Resize(rows_out, cols_out);
72  Vector<BaseFloat> window; // windowed waveform.
73  bool use_raw_log_energy = computer_.NeedRawLogEnergy();
74  for (int32 r = 0; r < rows_out; r++) { // r is frame index.
75  BaseFloat raw_log_energy = 0.0;
76  ExtractWindow(0, wave, r, computer_.GetFrameOptions(),
77  feature_window_function_, &window,
78  (use_raw_log_energy ? &raw_log_energy : NULL));
79 
80  SubVector<BaseFloat> output_row(*output, r);
81  computer_.Compute(raw_log_energy, vtln_warp, &window, &output_row);
82  }
83 }
84 
85 template <class F>
87  const VectorBase<BaseFloat> &wave,
88  BaseFloat vtln_warp,
89  Matrix<BaseFloat> *output) const {
90  OfflineFeatureTpl<F> temp(*this);
91  // call the non-const version of Compute() on a temporary copy of this object.
92  // This is a workaround for const-ness that may sometimes be useful in
93  // multi-threaded code, although it's not optimally efficient.
94  temp.Compute(wave, vtln_warp, output);
95 }
96 
97 } // end namespace kaldi
98 
99 #endif
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void Compute(const VectorBase< BaseFloat > &wave, BaseFloat vtln_warp, Matrix< BaseFloat > *output)
void ResampleWaveform(BaseFloat orig_freq, const VectorBase< BaseFloat > &wave, BaseFloat new_freq, Vector< BaseFloat > *new_wave)
Downsample or upsample a waveform.
Definition: resample.cc:368
void ComputeFeatures(const VectorBase< BaseFloat > &wave, BaseFloat sample_freq, BaseFloat vtln_warp, Matrix< BaseFloat > *output)
Computes the features for one file (one sequence of features).
kaldi::int32 int32
void ExtractWindow(int64 sample_offset, const VectorBase< BaseFloat > &wave, int32 f, const FrameExtractionOptions &opts, const FeatureWindowFunction &window_function, Vector< BaseFloat > *window, BaseFloat *log_energy_pre_window)
#define KALDI_ERR
Definition: kaldi-error.h:147
int32 NumFrames(int64 num_samples, const FrameExtractionOptions &opts, bool flush)
This function returns the number of frames that we can extract from a wave file with the given number...
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
A class representing a vector.
Definition: kaldi-vector.h:406
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
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).
This templated class is intended for offline feature extraction, i.e.
Provides a vector abstraction class.
Definition: kaldi-vector.h:41
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501