LinearResample is a special case of ArbitraryResample, where we want to resample a signal at linearly spaced intervals (this means we want to upsample or downsample the signal). More...
#include <resample.h>
Public Member Functions | |
LinearResample (int32 samp_rate_in_hz, int32 samp_rate_out_hz, BaseFloat filter_cutoff_hz, int32 num_zeros) | |
Constructor. More... | |
void | Resample (const VectorBase< BaseFloat > &input, bool flush, Vector< BaseFloat > *output) |
This function does the resampling. More... | |
void | Reset () |
Calling the function Reset() resets the state of the object prior to processing a new signal; it is only necessary if you have called Resample(x, y, false) for some signal, leading to a remainder of the signal being called, but then abandon processing the signal before calling Resample(x, y, true) for the last piece. More... | |
int32 | GetInputSamplingRate () |
int32 | GetOutputSamplingRate () |
Private Member Functions | |
int64 | GetNumOutputSamples (int64 input_num_samp, bool flush) const |
This function outputs the number of output samples we will output for a signal with "input_num_samp" input samples. More... | |
void | GetIndexes (int64 samp_out, int64 *first_samp_in, int32 *samp_out_wrapped) const |
Given an output-sample index, this function outputs to *first_samp_in the first input-sample index that we have a weight on (may be negative), and to *samp_out_wrapped the index into weights_ where we can get the corresponding weights on the input. More... | |
void | SetRemainder (const VectorBase< BaseFloat > &input) |
void | SetIndexesAndWeights () |
BaseFloat | FilterFunc (BaseFloat) 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 | samp_rate_in_ |
int32 | samp_rate_out_ |
BaseFloat | filter_cutoff_ |
int32 | num_zeros_ |
int32 | input_samples_in_unit_ |
The number of input samples in the smallest repeating unit: num_samp_in_ = samp_rate_in_hz / Gcd(samp_rate_in_hz, samp_rate_out_hz) More... | |
int32 | output_samples_in_unit_ |
The number of output samples in the smallest repeating unit: num_samp_out_ = samp_rate_out_hz / Gcd(samp_rate_in_hz, samp_rate_out_hz) More... | |
std::vector< int32 > | first_index_ |
The first input-sample index that we sum over, for this output-sample index. More... | |
std::vector< Vector< BaseFloat > > | weights_ |
Weights on the input samples, for this output-sample index. More... | |
int64 | input_sample_offset_ |
The number of input samples we have already received for this signal (including anything in remainder_) More... | |
int64 | output_sample_offset_ |
The number of samples we have already output for this signal. More... | |
Vector< BaseFloat > | input_remainder_ |
A small trailing part of the previously seen input signal. More... | |
LinearResample is a special case of ArbitraryResample, where we want to resample a signal at linearly spaced intervals (this means we want to upsample or downsample the signal).
It is more efficient than ArbitraryResample because we can construct it just once.
We require that the input and output sampling rate be specified as integers, as this is an easy way to specify that their ratio be rational.
Definition at line 147 of file resample.h.
LinearResample | ( | int32 | samp_rate_in_hz, |
int32 | samp_rate_out_hz, | ||
BaseFloat | filter_cutoff_hz, | ||
int32 | num_zeros | ||
) |
Constructor.
We make the input and output sample rates integers, because we are going to need to find a common divisor. This should just remind you that they need to be integers. The filter cutoff needs to be less than samp_rate_in_hz/2 and less than samp_rate_out_hz/2. num_zeros controls the sharpness of the filter, more == sharper but less efficient. We suggest around 4 to 10 for normal use.
Definition at line 33 of file resample.cc.
References kaldi::Gcd(), LinearResample::input_samples_in_unit_, KALDI_ASSERT, LinearResample::output_samples_in_unit_, LinearResample::Reset(), LinearResample::samp_rate_in_, LinearResample::samp_rate_out_, and LinearResample::SetIndexesAndWeights().
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 246 of file resample.cc.
References LinearResample::filter_cutoff_, M_2PI, M_PI, and LinearResample::num_zeros_.
Referenced by LinearResample::SetIndexesAndWeights().
|
inlineprivate |
Given an output-sample index, this function outputs to *first_samp_in the first input-sample index that we have a weight on (may be negative), and to *samp_out_wrapped the index into weights_ where we can get the corresponding weights on the input.
Definition at line 137 of file resample.cc.
References LinearResample::first_index_, LinearResample::input_samples_in_unit_, and LinearResample::output_samples_in_unit_.
Referenced by LinearResample::Resample().
|
inline |
|
private |
This function outputs the number of output samples we will output for a signal with "input_num_samp" input samples.
If flush == true, we return the largest n such that (n/samp_rate_out_) is in the interval [ 0, input_num_samp/samp_rate_in_ ), and note that the interval is half-open. If flush == false, define window_width as num_zeros / (2.0 * filter_cutoff_); we return the largest n such that (n/samp_rate_out_) is in the interval [ 0, input_num_samp/samp_rate_in_ - window_width ).
Definition at line 58 of file resample.cc.
References LinearResample::filter_cutoff_, kaldi::Lcm(), LinearResample::num_zeros_, LinearResample::samp_rate_in_, and LinearResample::samp_rate_out_.
Referenced by LinearResample::Resample().
|
inline |
void Resample | ( | const VectorBase< BaseFloat > & | input, |
bool | flush, | ||
Vector< BaseFloat > * | output | ||
) |
This function does the resampling.
If you call it with flush == true and you have never called it with flush == false, it just resamples the input signal (it resizes the output to a suitable number of samples).
You can also use this function to process a signal a piece at a time. suppose you break it into piece1, piece2, ... pieceN. You can call
If you call it with flush == false, it won't output the last few samples but will remember them, so that if you later give it a second piece of the input signal it can process it correctly. If your most recent call to the object was with flush == false, it will have internal state; you can remove this by calling Reset(). Empty input is acceptable.
Definition at line 152 of file resample.cc.
References VectorBase< Real >::Dim(), LinearResample::GetIndexes(), LinearResample::GetNumOutputSamples(), rnnlm::i, LinearResample::input_remainder_, LinearResample::input_sample_offset_, KALDI_ASSERT, LinearResample::output_sample_offset_, LinearResample::Reset(), Vector< Real >::Resize(), LinearResample::SetRemainder(), kaldi::VecVec(), and LinearResample::weights_.
Referenced by OnlinePitchFeatureImpl::AcceptWaveform(), kaldi::ResampleWaveform(), UnitTestLinearResample(), and UnitTestLinearResample2().
void Reset | ( | ) |
Calling the function Reset() resets the state of the object prior to processing a new signal; it is only necessary if you have called Resample(x, y, false) for some signal, leading to a remainder of the signal being called, but then abandon processing the signal before calling Resample(x, y, true) for the last piece.
Call it unnecessarily between signals will not do any harm.
Definition at line 235 of file resample.cc.
References LinearResample::input_remainder_, LinearResample::input_sample_offset_, and LinearResample::output_sample_offset_.
Referenced by LinearResample::LinearResample(), and LinearResample::Resample().
|
private |
Definition at line 104 of file resample.cc.
References LinearResample::filter_cutoff_, LinearResample::FilterFunc(), LinearResample::first_index_, rnnlm::i, rnnlm::j, LinearResample::num_zeros_, LinearResample::output_samples_in_unit_, LinearResample::samp_rate_in_, LinearResample::samp_rate_out_, and LinearResample::weights_.
Referenced by LinearResample::LinearResample().
|
private |
Definition at line 212 of file resample.cc.
References VectorBase< Real >::Dim(), LinearResample::filter_cutoff_, LinearResample::input_remainder_, LinearResample::num_zeros_, and LinearResample::samp_rate_in_.
Referenced by LinearResample::Resample().
|
private |
Definition at line 221 of file resample.h.
Referenced by LinearResample::FilterFunc(), LinearResample::GetNumOutputSamples(), LinearResample::SetIndexesAndWeights(), and LinearResample::SetRemainder().
|
private |
The first input-sample index that we sum over, for this output-sample index.
May be negative; any truncation at the beginning is handled separately. This is just for the first few output samples, but we can extrapolate the correct input-sample index for arbitrary output samples.
Definition at line 238 of file resample.h.
Referenced by LinearResample::GetIndexes(), and LinearResample::SetIndexesAndWeights().
A small trailing part of the previously seen input signal.
Definition at line 251 of file resample.h.
Referenced by LinearResample::Resample(), LinearResample::Reset(), and LinearResample::SetRemainder().
|
private |
The number of input samples we have already received for this signal (including anything in remainder_)
Definition at line 246 of file resample.h.
Referenced by LinearResample::Resample(), and LinearResample::Reset().
|
private |
The number of input samples in the smallest repeating unit: num_samp_in_ = samp_rate_in_hz / Gcd(samp_rate_in_hz, samp_rate_out_hz)
Definition at line 224 of file resample.h.
Referenced by LinearResample::GetIndexes(), and LinearResample::LinearResample().
|
private |
Definition at line 222 of file resample.h.
Referenced by LinearResample::FilterFunc(), LinearResample::GetNumOutputSamples(), LinearResample::SetIndexesAndWeights(), and LinearResample::SetRemainder().
|
private |
The number of samples we have already output for this signal.
Definition at line 249 of file resample.h.
Referenced by LinearResample::Resample(), and LinearResample::Reset().
|
private |
The number of output samples in the smallest repeating unit: num_samp_out_ = samp_rate_out_hz / Gcd(samp_rate_in_hz, samp_rate_out_hz)
Definition at line 228 of file resample.h.
Referenced by LinearResample::GetIndexes(), LinearResample::LinearResample(), and LinearResample::SetIndexesAndWeights().
|
private |
Definition at line 219 of file resample.h.
Referenced by LinearResample::GetNumOutputSamples(), LinearResample::LinearResample(), LinearResample::SetIndexesAndWeights(), and LinearResample::SetRemainder().
|
private |
Definition at line 220 of file resample.h.
Referenced by LinearResample::GetNumOutputSamples(), LinearResample::LinearResample(), and LinearResample::SetIndexesAndWeights().
Weights on the input samples, for this output-sample index.
Definition at line 241 of file resample.h.
Referenced by LinearResample::Resample(), and LinearResample::SetIndexesAndWeights().