ArbitraryResample Class Reference

Class ArbitraryResample allows you to resample a signal (assumed zero outside the sample region, not periodic) at arbitrary specified time values, which don't have to be linearly spaced. More...

#include <resample.h>

Collaboration diagram for ArbitraryResample:

Public Member Functions

 ArbitraryResample (int32 num_samples_in, BaseFloat samp_rate_hz, BaseFloat filter_cutoff_hz, const Vector< BaseFloat > &sample_points_secs, int32 num_zeros)
 
int32 NumSamplesIn () const
 
int32 NumSamplesOut () const
 
void Resample (const MatrixBase< BaseFloat > &input, MatrixBase< BaseFloat > *output) const
 This function does the resampling. More...
 
void Resample (const VectorBase< BaseFloat > &input, VectorBase< BaseFloat > *output) const
 This version of the Resample function processes just one vector. More...
 

Private Member Functions

void SetIndexes (const Vector< BaseFloat > &sample_points)
 
void SetWeights (const Vector< BaseFloat > &sample_points)
 
BaseFloat FilterFunc (BaseFloat t) const
 Here, t is a time in seconds representing an offset from the center of the windowed filter function, and FilterFunction(t) returns the windowed filter function, described in the header as h(t) = f(t)g(t), evaluated at t. More...
 

Private Attributes

int32 num_samples_in_
 
BaseFloat samp_rate_in_
 
BaseFloat filter_cutoff_
 
int32 num_zeros_
 
std::vector< int32first_index_
 
std::vector< Vector< BaseFloat > > weights_
 

Detailed Description

Class ArbitraryResample allows you to resample a signal (assumed zero outside the sample region, not periodic) at arbitrary specified time values, which don't have to be linearly spaced.

The low-pass filter cutoff "filter_cutoff_hz" should be less than half the sample rate; "num_zeros" should probably be at least two preferably more; higher numbers give sharper filters but will be less efficient.

Definition at line 95 of file resample.h.

Constructor & Destructor Documentation

◆ ArbitraryResample()

ArbitraryResample ( int32  num_samples_in,
BaseFloat  samp_rate_hz,
BaseFloat  filter_cutoff_hz,
const Vector< BaseFloat > &  sample_points_secs,
int32  num_zeros 
)

Definition at line 262 of file resample.cc.

References KALDI_ASSERT, ArbitraryResample::SetIndexes(), and ArbitraryResample::SetWeights().

265  :
266  num_samples_in_(num_samples_in),
267  samp_rate_in_(samp_rate_in),
268  filter_cutoff_(filter_cutoff),
269  num_zeros_(num_zeros) {
270  KALDI_ASSERT(num_samples_in > 0 && samp_rate_in > 0.0 &&
271  filter_cutoff > 0.0 &&
272  filter_cutoff * 2.0 <= samp_rate_in
273  && num_zeros > 0);
274  // set up weights_ and indices_. Please try to keep all functions short and
275  SetIndexes(sample_points);
276  SetWeights(sample_points);
277 }
void SetIndexes(const Vector< BaseFloat > &sample_points)
Definition: resample.cc:313
BaseFloat filter_cutoff_
Definition: resample.h:128
void SetWeights(const Vector< BaseFloat > &sample_points)
Definition: resample.cc:335
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

Member Function Documentation

◆ FilterFunc()

BaseFloat FilterFunc ( BaseFloat  t) const
private

Here, t is a time in seconds representing an offset from the center of the windowed filter function, and FilterFunction(t) returns the windowed filter function, described in the header as h(t) = f(t)g(t), evaluated at t.

Definition at line 353 of file resample.cc.

References ArbitraryResample::filter_cutoff_, M_2PI, M_PI, and ArbitraryResample::num_zeros_.

Referenced by LinearResample::GetOutputSamplingRate(), ArbitraryResample::NumSamplesOut(), and ArbitraryResample::SetWeights().

353  {
354  BaseFloat window, // raised-cosine (Hanning) window of width
355  // num_zeros_/2*filter_cutoff_
356  filter; // sinc filter function
357  if (fabs(t) < num_zeros_ / (2.0 * filter_cutoff_))
358  window = 0.5 * (1 + cos(M_2PI * filter_cutoff_ / num_zeros_ * t));
359  else
360  window = 0.0; // outside support of window function
361  if (t != 0.0)
362  filter = sin(M_2PI * filter_cutoff_ * t) / (M_PI * t);
363  else
364  filter = 2.0 * filter_cutoff_; // limit of the function at zero.
365  return filter * window;
366 }
#define M_PI
Definition: kaldi-math.h:44
float BaseFloat
Definition: kaldi-types.h:29
BaseFloat filter_cutoff_
Definition: resample.h:128
#define M_2PI
Definition: kaldi-math.h:52

◆ NumSamplesIn()

int32 NumSamplesIn ( ) const
inline

Definition at line 103 of file resample.h.

References ArbitraryResample::num_samples_in_.

103 { return num_samples_in_; }

◆ NumSamplesOut()

int32 NumSamplesOut ( ) const
inline

◆ Resample() [1/2]

void Resample ( const MatrixBase< BaseFloat > &  input,
MatrixBase< BaseFloat > *  output 
) const

This function does the resampling.

input.NumRows() and output.NumRows() should be equal and nonzero. input.NumCols() should equal NumSamplesIn() and output.NumCols() should equal NumSamplesOut().

Definition at line 280 of file resample.cc.

References MatrixBase< Real >::CopyColFromVec(), ArbitraryResample::first_index_, rnnlm::i, KALDI_ASSERT, kaldi::kNoTrans, ArbitraryResample::num_samples_in_, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), ArbitraryResample::NumSamplesOut(), and ArbitraryResample::weights_.

Referenced by OnlinePitchFeatureImpl::AcceptWaveform(), ArbitraryResample::NumSamplesOut(), UnitTestArbitraryResample(), and UnitTestLinearResample().

281  {
282  // each row of "input" corresponds to the data to resample;
283  // the corresponding row of "output" is the resampled data.
284 
285  KALDI_ASSERT(input.NumRows() == output->NumRows() &&
286  input.NumCols() == num_samples_in_ &&
287  output->NumCols() == weights_.size());
288 
289  Vector<BaseFloat> output_col(output->NumRows());
290  for (int32 i = 0; i < NumSamplesOut(); i++) {
291  SubMatrix<BaseFloat> input_part(input, 0, input.NumRows(),
292  first_index_[i],
293  weights_[i].Dim());
294  const Vector<BaseFloat> &weight_vec(weights_[i]);
295  output_col.AddMatVec(1.0, input_part,
296  kNoTrans, weight_vec, 0.0);
297  output->CopyColFromVec(output_col, i);
298  }
299 }
int32 NumSamplesOut() const
Definition: resample.h:105
kaldi::int32 int32
std::vector< Vector< BaseFloat > > weights_
Definition: resample.h:133
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::vector< int32 > first_index_
Definition: resample.h:131

◆ Resample() [2/2]

void Resample ( const VectorBase< BaseFloat > &  input,
VectorBase< BaseFloat > *  output 
) const

This version of the Resample function processes just one vector.

Definition at line 301 of file resample.cc.

References VectorBase< Real >::Dim(), ArbitraryResample::first_index_, rnnlm::i, KALDI_ASSERT, ArbitraryResample::num_samples_in_, kaldi::VecVec(), and ArbitraryResample::weights_.

302  {
303  KALDI_ASSERT(input.Dim() == num_samples_in_ &&
304  output->Dim() == weights_.size());
305 
306  int32 output_dim = output->Dim();
307  for (int32 i = 0; i < output_dim; i++) {
308  SubVector<BaseFloat> input_part(input, first_index_[i], weights_[i].Dim());
309  (*output)(i) = VecVec(input_part, weights_[i]);
310  }
311 }
kaldi::int32 int32
std::vector< Vector< BaseFloat > > weights_
Definition: resample.h:133
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
Definition: kaldi-vector.cc:37
std::vector< int32 > first_index_
Definition: resample.h:131

◆ SetIndexes()

void SetIndexes ( const Vector< BaseFloat > &  sample_points)
private

Definition at line 313 of file resample.cc.

References VectorBase< Real >::Dim(), ArbitraryResample::filter_cutoff_, ArbitraryResample::first_index_, rnnlm::i, ArbitraryResample::num_samples_in_, ArbitraryResample::num_zeros_, ArbitraryResample::samp_rate_in_, and ArbitraryResample::weights_.

Referenced by ArbitraryResample::ArbitraryResample(), and ArbitraryResample::NumSamplesOut().

313  {
314  int32 num_samples = sample_points.Dim();
315  first_index_.resize(num_samples);
316  weights_.resize(num_samples);
317  BaseFloat filter_width = num_zeros_ / (2.0 * filter_cutoff_);
318  for (int32 i = 0; i < num_samples; i++) {
319  // the t values are in seconds.
320  BaseFloat t = sample_points(i),
321  t_min = t - filter_width, t_max = t + filter_width;
322  int32 index_min = ceil(samp_rate_in_ * t_min),
323  index_max = floor(samp_rate_in_ * t_max);
324  // the ceil on index min and the floor on index_max are because there
325  // is no point using indices just outside the window (coeffs would be zero).
326  if (index_min < 0)
327  index_min = 0;
328  if (index_max >= num_samples_in_)
329  index_max = num_samples_in_ - 1;
330  first_index_[i] = index_min;
331  weights_[i].Resize(index_max - index_min + 1);
332  }
333 }
kaldi::int32 int32
float BaseFloat
Definition: kaldi-types.h:29
std::vector< Vector< BaseFloat > > weights_
Definition: resample.h:133
BaseFloat filter_cutoff_
Definition: resample.h:128
std::vector< int32 > first_index_
Definition: resample.h:131

◆ SetWeights()

void SetWeights ( const Vector< BaseFloat > &  sample_points)
private

Definition at line 335 of file resample.cc.

References ArbitraryResample::FilterFunc(), ArbitraryResample::first_index_, rnnlm::i, rnnlm::j, ArbitraryResample::NumSamplesOut(), ArbitraryResample::samp_rate_in_, and ArbitraryResample::weights_.

Referenced by ArbitraryResample::ArbitraryResample(), and ArbitraryResample::NumSamplesOut().

335  {
336  int32 num_samples_out = NumSamplesOut();
337  for (int32 i = 0; i < num_samples_out; i++) {
338  for (int32 j = 0 ; j < weights_[i].Dim(); j++) {
339  BaseFloat delta_t = sample_points(i) -
340  (first_index_[i] + j) / samp_rate_in_;
341  // Include at this point the factor of 1.0 / samp_rate_in_ which
342  // appears in the math.
343  weights_[i](j) = FilterFunc(delta_t) / samp_rate_in_;
344  }
345  }
346 }
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
kaldi::int32 int32
float BaseFloat
Definition: kaldi-types.h:29
std::vector< Vector< BaseFloat > > weights_
Definition: resample.h:133
std::vector< int32 > first_index_
Definition: resample.h:131

Member Data Documentation

◆ filter_cutoff_

BaseFloat filter_cutoff_
private

Definition at line 128 of file resample.h.

Referenced by ArbitraryResample::FilterFunc(), and ArbitraryResample::SetIndexes().

◆ first_index_

std::vector<int32> first_index_
private

◆ num_samples_in_

int32 num_samples_in_
private

◆ num_zeros_

int32 num_zeros_
private

Definition at line 129 of file resample.h.

Referenced by ArbitraryResample::FilterFunc(), and ArbitraryResample::SetIndexes().

◆ samp_rate_in_

◆ weights_


The documentation for this class was generated from the following files: