-
Notifications
You must be signed in to change notification settings - Fork 2
/
feature_extract.cpp
249 lines (171 loc) · 5.21 KB
/
feature_extract.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "feature_extract.h"
#include <cmath>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
/*
Mel Filter creation is based on Sphinx-III
Copyright (c) 2006 Carnegie Mellon University
#
# You may copy and modify this freely under the same terms as
# Sphinx-III
"""Compute MFCC coefficients.
This module provides functions for computing MFCC (mel-frequency
cepstral coefficients) as used in the Sphinx speech recognition
system.
"""
__author__ = "David Huggins-Daines <[email protected]>"
*/
float mel(const float f){
return 2595. * log10(1. + f / 700.);
}
float melinv(const float m){
return 700. * (pow(10., m / 2595.) - 1.);
}
FeatureExtractor::FeatureExtractor(size_t nfft,size_t melcount,size_t sample_rate,size_t lowerf , size_t upperf,float window_len,float shift ) :
nfft(nfft),melcount(melcount),sample_rate(sample_rate),lowerf(lowerf),upperf(upperf),shift(shift*sample_rate),window_len(window_len)
{
this->cfg = pffft_new_setup(this->nfft, pffft_transform_t::PFFFT_REAL);
size_t fft_out_size = (nfft/2)+1;
fft_result = (float*) pffft_aligned_malloc(fft_out_size*2 * sizeof(float));
frame = (float*) pffft_aligned_malloc(nfft * sizeof(float));
this->create_hanning_window();
this->create_mel_filter();
}
FeatureExtractor::~FeatureExtractor()
{
pffft_aligned_free(fft_result);
pffft_aligned_free(frame);
}
size_t FeatureExtractor::get_melcount()
{
return melcount;
}
void FeatureExtractor::print_mel_filter()
{
for(size_t i=0; i < (this->nfft/2+1) ; i++){
for(size_t j = 0; j < melcount ; j++){
if(mel_filters[i][j] != 0){
std::cout << i << ":" << j << " " << mel_filters[i][j] << "\t";
}
}
std::cout << std::endl;
}
}
void FeatureExtractor::create_mel_filter()
{
const float melmax = mel(this->upperf);
const float melmin = mel(this->lowerf);
const float dmelbw = (melmax - melmin) / (this->melcount + 1);
const float dfreq = this->sample_rate / this->nfft;
if (this->upperf > this->sample_rate/2){
std::cerr << "Upper frequency " << upperf << " exceeds Nyquist " << this->sample_rate/2 << std::endl;
}
std::vector<float> filter_edge;
for(size_t i=0; i < this->melcount+2; ++i){
float edge = melinv(melmin + dmelbw * i);
filter_edge.push_back(edge);
}
for(size_t i=0; i < (this->nfft/2+1) ; i++){
for(size_t j = 0; j < melcount ; j++){
mel_filters[i][j] = 0;
}
}
for(size_t i=0; i < this->melcount; ++i){
int leftfr = round(filter_edge[i] / dfreq);
int centerfr = round(filter_edge[i + 1] / dfreq);
int rightfr = round(filter_edge[i + 2] / dfreq);
float fwidth = (rightfr - leftfr) * dfreq;
float height = 2. / fwidth;
float leftslope = 0;
float rightslope = 0;
if (centerfr != leftfr)
leftslope = height / (centerfr - leftfr);
else
leftslope = 0;
int freq = leftfr + 1;
while (freq < centerfr){
mel_filters[freq][i] = (freq - leftfr) * leftslope;
freq++;
}
if (freq == centerfr){ // This is always true
mel_filters[freq][i] = height;
freq++;
}
if (centerfr != rightfr){
rightslope = height / (centerfr - rightfr);
}
while(freq < rightfr){
mel_filters[freq][i] = (freq - rightfr) * rightslope;
freq++;
}
}
}
void FeatureExtractor::create_hanning_window()
{
for(size_t n = 0 ; n < this->nfft ; ++n){
float val = 0.5-0.5*cos( (2*M_PI*n) / (this->nfft-1) );
hann.push_back(val);
}
}
void FeatureExtractor::spectrum(const float*const pcm,float*real,float*imag)
{
/*const size_t fft_out_size = (this->nfft/2)+1;
kiss_fft_cpx fft_result[fft_out_size];
kiss_fftr(this->cfg,pcm,fft_result);
for (size_t i = 0; i < fft_out_size ; i++){
real[i] = fft_result[i].r;
imag[i] = fft_result[i].i;
}*/
}
uint8_t FeatureExtractor::quantize_float(const float value)
{
//The values 12 and 8 are determined empirically
float converted_value = (value + 12) * 8;
if(converted_value > 255){
return 255;
}
if(converted_value < 0){
return 0;
}
return (uint8_t) converted_value;
}
int FeatureExtractor::signal_to_mel(const int16_t * const pcm ,const size_t len, uint8_t*result,const float gain)
{
const float convert = (1.0/32768.0)*gain;
double max = 0;
for(size_t i=0; i < len; ++i){
max += pcm[i];
}
const float mean = max/len;
const size_t number_of_frames = int(len / this->shift);
const size_t fft_out_size = (this->nfft/2)+1;
float power_spectrum[fft_out_size];
for(size_t i=0; i < number_of_frames; ++i){
const int start = i*this->shift;
//Apply Hanning Window
memset(frame, 0, this->nfft*sizeof(float) );
for (size_t pos = 0 ; pos < this->nfft ; ++pos){
if(pos+start < len){
frame[pos] = (pcm[pos+start] - mean) * convert * this->hann[pos];
}
}
pffft_transform_ordered(this->cfg,frame,fft_result,NULL, pffft_direction_t::PFFFT_FORWARD);
//Power Spectrum
for(size_t j=0; j < fft_out_size*2;j +=2 ){
float imag = fft_result[j];
float real = fft_result[j+1];
power_spectrum[j/2] = std::abs(-(imag*-imag) + real*real);
}
//Apply Mel Scale
for(size_t j=0; j < this->melcount; ++j){
float sum = 0;
for (size_t k = 0 ; k < fft_out_size; k++){
sum += power_spectrum[k] * mel_filters[k][j];
}
result[j+this->melcount*i] = quantize_float(log(sum+1e-5));
}
}
return this->melcount*number_of_frames;
}