|
kaldi-native-fbank C++ API
Kaldi-compatible feature extraction C++ API
|
kaldi-native-fbank/kaldi-native-fbank/csrc/resample.h More...
#include <stdint.h>#include <vector>#include "Eigen/Core"#include "kaldi-native-fbank/csrc/feature-matrix.h"
Go to the source code of this file.
Classes | |
| class | knf::ArbitraryResample |
| Resample a signal at arbitrary (e.g. More... | |
| class | knf::LinearResample |
| Efficient up/downsampling at linearly spaced output times. More... | |
Namespaces | |
| namespace | knf |
| This file is part of kaldi-native-fbank. | |
Functions | |
| void | knf::ResampleWaveform (float orig_freq, const VectorF &wave, float new_freq, VectorF *new_wave) |
| Downsample or upsample a waveform (convenience wrapper). | |
kaldi-native-fbank/kaldi-native-fbank/csrc/resample.h
This file is part of kaldi-native-fbank.
Classes for resampling signals (ported from kaldi's src/feat/resample.h).
This header contains declarations of classes for resampling signals. The normal cases of resampling a signal are upsampling and downsampling (increasing and decreasing the sample rate of a signal, respectively), although the ArbitraryResample class allows a more generic case where we want to get samples of a signal at uneven intervals (for instance, log-spaced).
The input signal is always evenly spaced, say sampled with frequency \( S \), and we assume the original signal was band-limited to \( S/2 \) or lower. The \( n \)'th input sample \( x_n \) (with \( n = 0, 1, \ldots \)) is interpreted as the original signal's value at time \( n/S \).
For resampling, it is convenient to view the input signal as a continuous function \( x(t) \) of \( t \), where each sample \( x_n \) becomes a delta function with magnitude \( x_n/S \), at time \( n/S \). If we band limit this to the Nyquist frequency \( S/2 \), we can show that this is the same as the original signal that was sampled [assuming the original signal was periodic and band limited]. In general we want to bandlimit to lower than \( S/2 \), because we don't have a perfect filter and also because if we want to resample at a lower frequency than \( S \), we need to bandlimit to below half of that.
Anyway, suppose we want to bandlimit to \( C \), with \( 0 < C < S/2 \). The perfect rectangular filter with cutoff \( C \) is the sinc function,
\[ f(t) = 2C \, \mathrm{sinc}(2Ct), \]
where sinc is the normalized sinc function \( \mathrm{sinc}(t) = \sin(\pi t) / (\pi t) \), with \( \mathrm{sinc}(0) = 1 \). This is not a practical filter, though, because it has infinite support. At the cost of less-than-perfect rolloff, we can choose a suitable windowing function \( g(t) \), and use \( f(t) g(t) \) as the filter. For a windowing function we choose raised-cosine (Hanning) window with support on \( [-w/2C, w/2C] \), where \( w \ge 2 \) is an integer chosen by the user. \( w = 1 \) means we window the sinc function out to its first zero on the left and right, \( w = 2 \) means the second zero, and so on; we normally choose \( w \) to be at least two. We call this num_zeros, not \( w \), in the code.
Convolving the signal \( x(t) \) with this windowed filter \( h(t) = f(t)g(t) \) and evaluating the resulting signal \( s(t) \) at an arbitrary time \( t \) is easy: we have
\[ s(t) = 1/S \sum_n x_n h(t - n/S). \]
This is true for arbitrary values of \( t \). What the class ArbitraryResample does is to allow you to evaluate the signal for specified values of \( t \).