-
Notifications
You must be signed in to change notification settings - Fork 0
/
mfcc.c
436 lines (393 loc) · 12 KB
/
mfcc.c
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* Copyright 2014 Douglas Bagnall <douglas@halo.gen.nz> LGPL */
#include "mfcc.h"
#include "pgm_dump.h"
#include "badmaths.h"
#include <gst/fft/gstfftf32.h>
#define POWER(x) (x.r * x.r + x.i * x.i)
static float *
recur_bin_complex(RecurAudioBinner *ab, GstFFTF32Complex *f)
{
int i, j;
float mul;
float sum_left = 0.0f;
float sum_right;
float power;
RecurAudioBinSlope *slope;
/*first slope is left side only, last slope is right only*/
for (i = 0; i <= ab->n_bins; i++){
slope = &ab->slopes[i];
j = slope->left;
/*left fractional part*/
mul = slope->slope * slope->left_fraction;
power = POWER(f[j]) * slope->left_fraction;
sum_right = sum_left + (1.0f - mul) * power;
/*Note sum_right is old sum_left */
sum_left = mul * power;
if (slope->left != slope->right){
/*centre */
for (j = slope->left + 1; j < slope->right; j++){
mul += slope->slope;
power = POWER(f[j]);
sum_left += mul * power;
sum_right += (1.0f - mul) * power;
}
}
/*right fraction */
mul += slope->slope * slope->right_fraction;
power = POWER(f[j]) * slope->right_fraction;
sum_left += mul * power;
sum_right += (1.0f - mul) * power;
if (i){
ab->fft_bins[i - 1] = logf(sum_right + 1);// - slope->log_scale;
}
}
return ab->fft_bins;
}
/* Apply the cached window function, returning the actually used destination.
*/
static const float *
recur_apply_window(RecurAudioBinner *ab, const float *src, float *dest)
{
int i;
if (ab->window_type == RECUR_WINDOW_NONE){
if (dest != src){
memmove(dest, src, ab->window_size * sizeof(float));
}
else {
return src;
}
}
else {
for (i = 0; i < ab->window_size; i++){
dest[i] = src[i] * ab->mask[i];
}
}
return dest;
}
/* extract log scaled, mel-shaped, frequency bins */
float *
recur_extract_log_freq_bins(RecurAudioBinner *ab, float *data){
/* XXX assumes ab->value_size is 2 */
const float *windowed_data = recur_apply_window(ab, data, data);
gst_fft_f32_fft(ab->fft,
windowed_data,
ab->freq_data
);
return recur_bin_complex(ab, ab->freq_data);
}
float *
recur_extract_mfccs(RecurAudioBinner *ab, float *data){
float *fft_bins = recur_extract_log_freq_bins(ab, data);
recur_dct_cached(fft_bins, ab->dct_bins, ab->n_bins);
return ab->dct_bins;
}
/*usual scale is 1127, but it really doesn't matter */
#define MEL_SCALE 1127.0f
static inline float
hz_to_mel(float hz, float knee, float focus){
float mel = MEL_SCALE * logf(1.0f + hz / knee);
if (focus){
mel /= 1.0f + expf(3.0f * (1.0f - hz / focus));
}
return mel;
}
/*Calculating the inverse of the hz_to_mel function is not simple when focus
is non-zero. Since it is only needed at set-up time, we just make iterative
approximations.
*/
static inline float
mel_to_hz(float mel, float knee, float focus){
float hz = (mel / 34) * (mel / 34);
float approx;
float prev = hz_to_mel(hz, knee, focus) - 1;
float mul = 2.0f;
for (;;){
approx = hz_to_mel(hz, knee, focus);
MAYBE_DEBUG("mel %f approx %f prev %f diff %g mul %f hz %f",
mel, approx, prev, approx - mel, mul, hz);
if (fabs(mel - approx) < 0.0001 || prev == approx){
return hz;
}
hz = MAX(hz + mul * (mel - approx), 0);
if ((prev > mel) != (approx > mel)){
mul *= 0.5;
}
prev = approx;
}
}
RecurAudioBinSlope * __attribute__((malloc))
recur_bin_slopes_new(const int n_bins, const int fft_len,
const float fmin, const float fmax,
const float fknee, const float ffocus,
const float audio_rate){
const int n_slopes = n_bins + 1;
RecurAudioBinSlope * slopes = malloc_aligned_or_die(n_slopes *
sizeof(RecurAudioBinSlope));
int i;
const float mmin = hz_to_mel(fmin, fknee, ffocus);
const float mmax = hz_to_mel(fmax, fknee, ffocus);
const float step = (mmax - mmin) / n_slopes;
float hz_to_samples = fft_len * 2 / audio_rate;
float hz = fmin;
float mel = mmin;
float right = hz * hz_to_samples;
MAYBE_DEBUG("mmin %f mmax %f, fmin %f, fmax %f fknee %f ffocus %f",
mmin, mmax, fmin, fmax, fknee, ffocus);
for (i = 0; i < n_slopes; i++){
RecurAudioBinSlope *s = &slopes[i];
float left = right;
s->left = (int)left;
s->left_fraction = 1.0 - (left - s->left);
mel += step;
hz = mel_to_hz(mel, fknee, ffocus);
right = hz * hz_to_samples;
s->right = (int)right;
s->right_fraction = right - s->right;
s->slope = 1.0 / (right - left);
if (s->left == s->right){
/*triangle is too little! */
s->left_fraction = (right - left);
s->right_fraction = 0;
}
s->log_scale = logf(1.0f + right - left);
MAYBE_DEBUG("slope %d: left %d+%.2f right %d+%.2f log_scale %f hz %f"
" mel %f mmin %f mmax %f",
i, s->left, s->left_fraction, s->right, s->right_fraction, s->log_scale,
hz, mel, mmin, mmax);
}
return slopes;
}
static void
mfcc_slopes_dump2(RecurAudioBinner *ab){
int i;
int wsize = ab->window_size / ab->value_size;
TemporalPPM *ppm = temporal_ppm_alloc(ab->n_bins, wsize, "mfcc", 0,
PGM_DUMP_GREY, NULL);
GstFFTF32Complex *f = calloc(sizeof(float), ab->window_size + 2);
for (i = 0; i <= wsize; i++){
f[i].r = 1.0f;
f[i].i = 1.0f;
float *row = recur_bin_complex(ab, f);
for (int j = 0; j < ab->n_bins; j++){
row[j] = fast_expf(row[j]);
}
temporal_ppm_add_row(ppm, row);
f[i].r = 0.0f;
f[i].i = 0.0f;
}
free(f);
temporal_ppm_free(ppm);
}
/*mfcc_slopes_dump draws a PGM showing whether the slope calculations worked*/
static void
mfcc_slopes_dump(RecurAudioBinner *ab){
int i, j;
int wsize = ab->window_size / ab->value_size;
u8 *img = malloc_aligned_or_die(ab->n_bins * wsize);
memset(img, 0, ab->n_bins * wsize);
float mul;
RecurAudioBinSlope *slope;
/*first slope is left side only, last slope is right only*/
for (i = 0; i <= ab->n_bins; i++){
u8 *left = (i < ab->n_bins) ? img + i * wsize : NULL;
u8 *right = (i) ? img + (i - 1) * wsize : NULL;
slope = &ab->slopes[i];
float sum_left = 0.0;
float sum_right = 0.0;
/*left fractional part*/
mul = slope->slope * slope->left_fraction;
if (left){
left[slope->left] += 255 * mul * slope->left_fraction;
sum_left += mul * slope->left_fraction;
}
if (right){
right[slope->left] += 255 * (1.0 - mul) * slope->left_fraction;
sum_right += (1.0 - mul) * slope->left_fraction;
}
if (slope->left != slope->right){
/*centre */
for (j = slope->left + 1; j < slope->right; j++){
mul += slope->slope;
if (left){
left[j] += mul * 255;
sum_left += mul;
}
if (right){
right[j] += (1.0 - mul) * 255;
sum_right += (1.0 - mul);
}
}
}
/*right fraction */
mul += slope->slope * slope->right_fraction;
if (left){
left[slope->right] += 255 * mul * slope->right_fraction;
sum_left += mul * slope->right_fraction;
}
if (right){
right[slope->right] += 255 * (1.0f - mul) * slope->right_fraction;
sum_right += (1.0f - mul) * slope->right_fraction;
}
MAYBE_DEBUG("%2d. left%3d right%3d slope %.3f fractions: L %.3f R %.3f mul at end %.3f"
" sum_L %.3f sum_R %.3f sum %.3f",
i, slope->left, slope->right, slope->slope, slope->left_fraction,
slope->right_fraction, mul, sum_left, sum_right, sum_left + sum_right);
}
pgm_dump(img, wsize, ab->n_bins, IMAGE_DIR "/mfcc-bins.pgm");
free(img);
}
void
recur_window_init(float *mask, int len, int type, float scale){
int i;
const double half_pi = G_PI * 0.5;
const double pi_norm = G_PI / len;
switch (type){
case RECUR_WINDOW_HANN:
for (i = 0; i < len; i++){
mask[i] = (0.5 - 0.5 * cos(2.0 * pi_norm * i)) * scale;
}
break;
case RECUR_WINDOW_MP3:
for (i = 0; i < len; i++){
mask[i] = sin(pi_norm * (i + 0.5f)) * scale;
}
break;
case RECUR_WINDOW_VORBIS:
for (i = 0; i < len; i++){
double z = pi_norm * (i + 0.5);
mask[i] = sin(half_pi * sin(z) * sin(z)) * scale;
}
break;
case RECUR_WINDOW_NONE:
default:
for (i = 0; i < len; i++){
mask[i] = 1.0f;
}
break;
}
}
RecurAudioBinner * __attribute__((malloc))
recur_audio_binner_new(int window_size, int window_type,
int n_bins,
float min_freq,
float max_freq,
float knee_freq,
float focus_freq,
float audio_rate,
float scale,
int value_size /*1 for real, 2 for complex*/
){
RecurAudioBinner *ab = calloc(1, sizeof(*ab));
ab->window_size = window_size;
ab->window_type = window_type;
ab->n_bins = n_bins;
ab->pcm_data = malloc_aligned_or_die((window_size + 2) * sizeof(float));
ab->freq_data = malloc_aligned_or_die((window_size + 2) * sizeof(float));
ab->fft = gst_fft_f32_new(window_size, FALSE);
float *mask = malloc_aligned_or_die((window_size + 2) * sizeof(float));
recur_window_init(mask, window_size, window_type, scale);
ab->mask = mask;
ab->value_size = value_size;
ab->slopes = recur_bin_slopes_new(n_bins,
window_size / value_size,
min_freq,
max_freq,
knee_freq,
focus_freq,
audio_rate
);
mfcc_slopes_dump(ab);
ab->fft_bins = malloc_aligned_or_die((n_bins + 3) * sizeof(float));
ab->dct_bins = malloc_aligned_or_die((n_bins + 2) * sizeof(float));
mfcc_slopes_dump2(ab);
return ab;
}
void
recur_audio_binner_delete(RecurAudioBinner *ab){
free(ab->slopes);
free(ab->pcm_data);
free(ab->freq_data);
free((void *)ab->mask);
free(ab->fft_bins);
free(ab->dct_bins);
gst_fft_f32_free(ab->fft);
free(ab);
}
/* dct/idct based on recur/test/pydct.py, originally from Mimetic TV*/
/* XXX see ffmpeg's dct32.c for a relatively optimal dct32 (LGPL) */
void
recur_dct(const float *restrict input, float *restrict output, int len){
int j, k;
float pin = G_PI / len;
for (j = 0; j < len; j++){
float a = 0.0f;
for (k = 0; k < len; k++){
a += input[k] * cosf(pin * j * (k + 0.5f));
}
output[j] = a;
}
output[0] *= 0.7071067811865476f;
}
void
recur_idct(const float *restrict input, float *restrict output, int len){
int j, k;
float pin = G_PI / len;
float scale = 2.0f / len;
for (j = 0; j < len; j++){
float a = 0.7071067811865476f * input[0];
for (k = 1; k < len; k++){
a += input[k] * cosf(pin * k * (j + 0.5f));
}
output[j] = a * scale;
}
}
/*recur_dct_cached is still a quadratic DCT, but it exploits the fact that the
cos() calls are all using a small set of arguments, and caches the results
in memory. Presumably because it can use double precision to calculate the
cache, the results seem to be more accurate as well as faster.
It uses a simple caching strategy: the calculated cosines are saved between
consecutive transforms (the overwhelmingly common case so far) -- if the
size of the transforms changes frequently the cache will be repeatedly
recalculated, but this is still faster than the naive method.
A proper DCT with butterflies and so on will be quicker, but needs to be
tuned for its particular size.
The indexing strategy has been determined experimentally -- other sequences
may well be simpler.
XXX a passed in cache pointer would be the thing if ever two DCT sizes are
being used alternately.
*/
void
recur_dct_cached(const float *restrict input, float *restrict output, int len){
int j, k;
static float *cos_lut = NULL;
static int cos_len = 0;
if (cos_len != len * 2){
if (cos_lut){
free(cos_lut);
}
cos_len = len * 2;
cos_lut = malloc_aligned_or_die((cos_len + 1) * sizeof(float));
for (j = 0; j <= cos_len; j++){
cos_lut[j] = cos(G_PI / cos_len * j);
}
}
for (j = 0; j < len; j++){
float a = 0.0f;
int step = j * 2;
int i = j;
for (k = 0; k < len; k++){
a += input[k] * cos_lut[i];
i += step;
if (i > cos_len){ /*bounce off the top */
i = 2 * cos_len - i;
step = -step;
}
else if (i < 0){
i = -i;
step = -step;
}
}
output[j] = a;
}
output[0] *= 0.7071067811865476f;
}