resample.h
Go to the documentation of this file.
1 // feat/resample.h
2 
3 // Copyright 2013 Pegah Ghahremani
4 // 2014 IMSL, PKU-HKUST (author: Wei Shi)
5 // 2014 Yanqing Sun, Junjie Wang
6 // 2014 Johns Hopkins University (author: Daniel Povey)
7 
8 // See ../../COPYING for clarification regarding multiple authors
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 //
14 // http://www.apache.org/licenses/LICENSE-2.0
15 //
16 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
18 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
19 // MERCHANTABLITY OR NON-INFRINGEMENT.
20 // See the Apache 2 License for the specific language governing permissions and
21 // limitations under the License.
22 
23 
24 #ifndef KALDI_FEAT_RESAMPLE_H_
25 #define KALDI_FEAT_RESAMPLE_H_
26 
27 #include <cassert>
28 #include <cstdlib>
29 #include <string>
30 #include <vector>
31 
32 
33 #include "matrix/matrix-lib.h"
34 #include "util/common-utils.h"
35 #include "base/kaldi-error.h"
36 
37 namespace kaldi {
40 
96  public:
97  ArbitraryResample(int32 num_samples_in,
98  BaseFloat samp_rate_hz,
99  BaseFloat filter_cutoff_hz,
100  const Vector<BaseFloat> &sample_points_secs,
101  int32 num_zeros);
102 
103  int32 NumSamplesIn() const { return num_samples_in_; }
104 
105  int32 NumSamplesOut() const { return weights_.size(); }
106 
112  void Resample(const MatrixBase<BaseFloat> &input,
113  MatrixBase<BaseFloat> *output) const;
114 
117  void Resample(const VectorBase<BaseFloat> &input,
118  VectorBase<BaseFloat> *output) const;
119  private:
120  void SetIndexes(const Vector<BaseFloat> &sample_points);
121 
122  void SetWeights(const Vector<BaseFloat> &sample_points);
123 
124  BaseFloat FilterFunc(BaseFloat t) const;
125 
130 
131  std::vector<int32> first_index_; // The first input-sample index that we sum
132  // over, for this output-sample index.
133  std::vector<Vector<BaseFloat> > weights_;
134 };
135 
136 
148  public:
155  LinearResample(int32 samp_rate_in_hz,
156  int32 samp_rate_out_hz,
157  BaseFloat filter_cutoff_hz,
158  int32 num_zeros);
159 
177  void Resample(const VectorBase<BaseFloat> &input,
178  bool flush,
179  Vector<BaseFloat> *output);
180 
187  void Reset();
188 
191  inline int32 GetOutputSamplingRate() { return samp_rate_out_; }
192  private:
201  int64 GetNumOutputSamples(int64 input_num_samp, bool flush) const;
202 
203 
208  inline void GetIndexes(int64 samp_out,
209  int64 *first_samp_in,
210  int32 *samp_out_wrapped) const;
211 
212  void SetRemainder(const VectorBase<BaseFloat> &input);
213 
214  void SetIndexesAndWeights();
215 
217 
218  // The following variables are provided by the user.
223 
225  int32 output_samples_in_unit_;
229 
233 
238  std::vector<int32> first_index_;
239 
241  std::vector<Vector<BaseFloat> > weights_;
242 
243  // the following variables keep track of where we are in a particular signal,
244  // if it is being provided over multiple calls to Resample().
245 
247  int64 output_sample_offset_;
250  Vector<BaseFloat> input_remainder_;
252 };
254 
273 void ResampleWaveform(BaseFloat orig_freq, const VectorBase<BaseFloat> &wave,
274  BaseFloat new_freq, Vector<BaseFloat> *new_wave);
275 
276 
279 inline void DownsampleWaveForm(BaseFloat orig_freq, const VectorBase<BaseFloat> &wave,
280  BaseFloat new_freq, Vector<BaseFloat> *new_wave) {
281  ResampleWaveform(orig_freq, wave, new_freq, new_wave);
282 }
283 
284 
286 } // namespace kaldi
287 #endif // KALDI_FEAT_RESAMPLE_H_
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
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
Base class which provides matrix operations not involving resizing or allocation. ...
Definition: kaldi-matrix.h:49
int32 NumSamplesOut() const
Definition: resample.h:105
BaseFloat FilterFunc(BaseFloat t) const
Here, t is a time in seconds representing an offset from the center of the windowed filter function...
Definition: resample.cc:353
void DownsampleWaveForm(BaseFloat orig_freq, const VectorBase< BaseFloat > &wave, BaseFloat new_freq, Vector< BaseFloat > *new_wave)
This function is deprecated.
Definition: resample.h:279
kaldi::int32 int32
int64 input_sample_offset_
The number of input samples we have already received for this signal (including anything in remainder...
Definition: resample.h:246
float BaseFloat
Definition: kaldi-types.h:29
std::vector< int32 > first_index_
The first input-sample index that we sum over, for this output-sample index.
Definition: resample.h:238
Class ArbitraryResample allows you to resample a signal (assumed zero outside the sample region...
Definition: resample.h:95
void Resample(const MatrixBase< BaseFloat > &input, MatrixBase< BaseFloat > *output) const
This function does the resampling.
Definition: resample.cc:280
void SetIndexes(const Vector< BaseFloat > &sample_points)
Definition: resample.cc:313
int32 GetOutputSamplingRate()
Definition: resample.h:191
std::vector< Vector< BaseFloat > > weights_
Definition: resample.h:133
BaseFloat filter_cutoff_
Definition: resample.h:128
void SetWeights(const Vector< BaseFloat > &sample_points)
Definition: resample.cc:335
int32 NumSamplesIn() const
Definition: resample.h:103
A class representing a vector.
Definition: kaldi-vector.h:406
LinearResample is a special case of ArbitraryResample, where we want to resample a signal at linearly...
Definition: resample.h:147
BaseFloat filter_cutoff_
Definition: resample.h:221
ArbitraryResample(int32 num_samples_in, BaseFloat samp_rate_hz, BaseFloat filter_cutoff_hz, const Vector< BaseFloat > &sample_points_secs, int32 num_zeros)
Definition: resample.cc:262
int32 GetInputSamplingRate()
Definition: resample.h:190
int32 input_samples_in_unit_
The number of input samples in the smallest repeating unit: num_samp_in_ = samp_rate_in_hz / Gcd(samp...
Definition: resample.h:224
std::vector< Vector< BaseFloat > > weights_
Weights on the input samples, for this output-sample index.
Definition: resample.h:241
Provides a vector abstraction class.
Definition: kaldi-vector.h:41
std::vector< int32 > first_index_
Definition: resample.h:131