-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_extract.h
67 lines (57 loc) · 1.7 KB
/
feature_extract.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#pragma once
#ifndef FRONTEND_FEATURE_EXTRACT_H_
#define FRONTEND_FEATURE_EXTRACT_H_
#include <vector>
#include <complex>
#include <iostream>
#include "Eigen/Core"
#include "unsupported/Eigen/FFT"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define WHISPER_CHUNK_SIZE 30
typedef Eigen::Matrix<float, 1, Eigen::Dynamic, Eigen::RowMajor> Vectorf;
typedef Eigen::Matrix<std::complex<float>, 1, Eigen::Dynamic, Eigen::RowMajor> Vectorcf;
typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> Matrixf;
typedef Eigen::Matrix<std::complex<float>, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> Matrixcf;
struct FeatureConfig {
int sample_rate;
int n_fft;
int n_hop;
std::string window;
bool center;
std::string pad_mode;
float power;
int n_mels;
int fmin;
int fmax;
FeatureConfig(int sample_rate = 16000, int n_fft = 400, int n_hop = 160, std::string window = "hann",
bool center = true, std::string pad_mode = "edge", float power = 2.0, int n_mels = 80,
int fmin =0,int fmax=8000)
:sample_rate(sample_rate),
n_fft(n_fft),
n_hop(n_hop),
window(window),
center(center),
pad_mode(pad_mode),
power(power),
n_mels(n_mels),
fmin(fmin),
fmax(fmax){
}
};
class FeatureExtract {
public:
explicit FeatureExtract(const FeatureConfig& config);
std::vector<std::vector<float>> GetFeature(std::vector<float>& x);
private:
Matrixf MelSpectrogram(Vectorf& x);
Vectorf Pad(Vectorf& x, int left, int right, float value);
Matrixcf Stft(Vectorf& x);
Matrixf MelFilter();
Matrixf Spectrogram(Matrixcf& X);
void RemoveRow(Matrixcf& matrix, size_t row_to_remove);
private:
const FeatureConfig& config_;
};
#endif