kaldi-native-fbank C++ API
Kaldi-compatible feature extraction C++ API
Loading...
Searching...
No Matches
feature-window.h
Go to the documentation of this file.
1// kaldi-native-fbank/csrc/feature-window.h
2//
3// Copyright 2009-2011 Karel Vesely; Petr Motlicek; Saarland University
4// 2014-2016 Johns Hopkins University (author: Daniel Povey)
5// Copyright (c) 2022 Xiaomi Corporation (authors: Fangjun Kuang)
6//
7// This file is copied/modified from kaldi/src/feat/feature-window.h
8
17
18#ifndef KALDI_NATIVE_FBANK_CSRC_FEATURE_WINDOW_H_
19#define KALDI_NATIVE_FBANK_CSRC_FEATURE_WINDOW_H_
20
21#include <cstdint>
22#include <sstream>
23#include <stdexcept>
24#include <string>
25#include <vector>
26
27namespace knf {
28
32inline int32_t RoundUpToNearestPowerOfTwo(int32_t n) {
33 // copied from kaldi/src/base/kaldi-math.cc
34 if (n <= 0) {
35 throw std::invalid_argument("n must be > 0");
36 }
37 n--;
38 n |= n >> 1;
39 n |= n >> 2;
40 n |= n >> 4;
41 n |= n >> 8;
42 n |= n >> 16;
43 return n + 1;
44}
45
60 float samp_freq = 16000;
61
63 float frame_shift_ms = 10.0f;
64
66 float frame_length_ms = 25.0f;
67
73 float dither = 0.00003f;
74
76 float preemph_coeff = 0.97f;
77
79 bool remove_dc_offset = true;
80
87 std::string window_type = "povey";
88
92
94 float blackman_coeff = 0.42f;
95
100 bool snip_edges = true;
101
102 // bool allow_downsample = false;
103 // bool allow_upsample = false;
104
106 int32_t WindowShift() const {
107 return static_cast<int32_t>(samp_freq * 0.001f * frame_shift_ms);
108 }
109
111 int32_t WindowSize() const {
112 return static_cast<int32_t>(samp_freq * 0.001f * frame_length_ms);
113 }
114
121
123 std::string ToString() const {
124 std::ostringstream os;
125#define KNF_PRINT(x) os << #x << ": " << x << "\n"
136 // KNF_PRINT(allow_downsample);
137 // KNF_PRINT(allow_upsample);
138#undef KNF_PRINT
139 return os.str();
140 }
141};
142
144std::ostream &operator<<(std::ostream &os, const FrameExtractionOptions &opts);
145
157 public:
161
165
175 FeatureWindowFunction(const std::string &window_type, int32_t window_size,
176 float blackman_coeff = 0.42);
177
179 explicit FeatureWindowFunction(const std::vector<float> &window);
180
187 void Apply(float *wave) const;
188
190 const std::vector<float> &GetWindow() const { return window_; }
191
192 private:
193 std::vector<float> window_; // of size opts.WindowSize()
194};
195
203int64_t FirstSampleOfFrame(int32_t frame, const FrameExtractionOptions &opts);
204
225int32_t NumFrames(int64_t num_samples, const FrameExtractionOptions &opts,
226 bool flush = true);
227
254void ExtractWindow(int64_t sample_offset, const std::vector<float> &wave,
255 int32_t f, const FrameExtractionOptions &opts,
256 const FeatureWindowFunction &window_function,
257 std::vector<float> *window,
258 float *log_energy_pre_window = nullptr);
259
280 const FeatureWindowFunction &window_function, float *window,
281 float *log_energy_pre_window = nullptr);
282
284float InnerProduct(const float *a, const float *b, int32_t n);
285
315std::vector<float> GetWindow(const std::string &window_type,
316 int32_t window_size, float blackman_coeff = 0.42);
317
318} // namespace knf
319
320#endif // KALDI_NATIVE_FBANK_CSRC_FEATURE_WINDOW_H_
Stores the windowing coefficients and applies them to a frame.
Definition feature-window.h:156
FeatureWindowFunction(const FrameExtractionOptions &opts)
Construct the windowing function implied by opts.window_type, with a size of opts....
FeatureWindowFunction(const std::string &window_type, int32_t window_size, float blackman_coeff=0.42)
Construct the windowing function of the given type and size.
FeatureWindowFunction()=default
Construct an empty window function.
FeatureWindowFunction(const std::vector< float > &window)
Construct from explicit window coefficients.
void Apply(float *wave) const
Apply the window to a frame, in place.
const std::vector< float > & GetWindow() const
Return the window coefficients.
Definition feature-window.h:190
#define KNF_PRINT(x)
This file is part of kaldi-native-fbank.
Definition feature-cmvn.h:42
void ProcessWindow(const FrameExtractionOptions &opts, const FeatureWindowFunction &window_function, float *window, float *log_energy_pre_window=nullptr)
This function does all the windowing steps after actually extracting the windowed signal: depending o...
int32_t RoundUpToNearestPowerOfTwo(int32_t n)
Round n up to the nearest power of two.
Definition feature-window.h:32
int32_t NumFrames(int64_t num_samples, const FrameExtractionOptions &opts, bool flush=true)
This function returns the number of frames that we can extract from a wave file with the given number...
float InnerProduct(const float *a, const float *b, int32_t n)
Compute the inner product of two vectors of size n.
std::vector< float > GetWindow(const std::string &window_type, int32_t window_size, float blackman_coeff=0.42)
Return the window coefficients of the given window type.
void ExtractWindow(int64_t sample_offset, const std::vector< float > &wave, int32_t f, const FrameExtractionOptions &opts, const FeatureWindowFunction &window_function, std::vector< float > *window, float *log_energy_pre_window=nullptr)
ExtractWindow() extracts a windowed frame of waveform (possibly with a power-of-two,...
std::ostream & operator<<(std::ostream &os, const FbankOptions &opts)
Write the options to the stream, one option per line.
int64_t FirstSampleOfFrame(int32_t frame, const FrameExtractionOptions &opts)
Return the index of the first sample of the frame indexed frame.
Options for extracting frames from a waveform.
Definition feature-window.h:57
int32_t WindowShift() const
Return the window shift in samples.
Definition feature-window.h:106
float blackman_coeff
Constant coefficient for the generalized Blackman window.
Definition feature-window.h:94
float dither
Amount of dithering, 0.0 means no dither.
Definition feature-window.h:73
int32_t PaddedWindowSize() const
Return the window size after zero-padding to a power of two, if round_to_power_of_two is true; otherw...
Definition feature-window.h:117
float frame_shift_ms
Frame shift in milliseconds.
Definition feature-window.h:63
int32_t WindowSize() const
Return the window size in samples.
Definition feature-window.h:111
bool remove_dc_offset
Subtract mean of wave before FFT.
Definition feature-window.h:79
bool snip_edges
If true, end effects are handled by outputting only frames that completely fit in the file,...
Definition feature-window.h:100
float frame_length_ms
Frame length in milliseconds.
Definition feature-window.h:66
float preemph_coeff
Preemphasis coefficient.
Definition feature-window.h:76
std::string window_type
Type of the windowing function; may be "hamming", "rectangular", "povey", "hanning",...
Definition feature-window.h:87
bool round_to_power_of_two
If true, round the window size to a power of two by zero-padding the input to the FFT.
Definition feature-window.h:91
std::string ToString() const
Return the options as a string.
Definition feature-window.h:123
float samp_freq
Waveform data sample frequency (must match the waveform file, if specified there).
Definition feature-window.h:60