2 Copyright (C) 2007-2009 Paul Brossier <piem@aubio.org>
3 and Amaury Hazan <ahazan@iua.upf.edu>
5 This file is part of aubio.
7 aubio is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 aubio is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with aubio. If not, see <http://www.gnu.org/licenses/>.
22 #include "aubio_priv.h"
26 #include "mathutils.h"
28 #include "spectral/fft.h"
29 #include "spectral/filterbank.h"
30 #include "spectral/filterbank_mel.h"
31 #include "spectral/mfcc.h"
33 /** Internal structure for mfcc object */
37 uint_t win_s; /** grain length */
38 uint_t samplerate; /** sample rate (needed?) */
39 uint_t n_filters; /** number of *filters */
40 uint_t n_coefs; /** number of coefficients (<= n_filters/2 +1) */
41 aubio_filterbank_t *fb; /** filter bank */
42 fvec_t *in_dct; /** input buffer for dct * [fb->n_filters] */
43 fmat_t *dct_coeffs; /** DCT transform n_filters * n_coeffs */
48 new_aubio_mfcc (uint_t win_s, uint_t n_filters, uint_t n_coefs,
52 /* allocate space for mfcc object */
53 aubio_mfcc_t *mfcc = AUBIO_NEW (aubio_mfcc_t);
58 mfcc->samplerate = samplerate;
59 mfcc->n_filters = n_filters;
60 mfcc->n_coefs = n_coefs;
62 /* filterbank allocation */
63 mfcc->fb = new_aubio_filterbank (n_filters, mfcc->win_s);
64 aubio_filterbank_set_mel_coeffs_slaney (mfcc->fb, samplerate);
66 /* allocating buffers */
67 mfcc->in_dct = new_fvec (n_filters);
69 mfcc->dct_coeffs = new_fmat (n_coefs, n_filters);
71 /* compute DCT transform dct_coeffs[i][j] as
72 cos ( j * (i+.5) * PI / n_filters ) */
73 smpl_t scaling = 1. / SQRT (n_filters / 2.);
74 for (i = 0; i < n_filters; i++) {
75 for (j = 0; j < n_coefs; j++) {
76 mfcc->dct_coeffs->data[i][j] =
77 scaling * COS (j * (i + 0.5) * PI / n_filters);
79 mfcc->dct_coeffs->data[i][0] *= SQRT (2.) / 2.;
86 del_aubio_mfcc (aubio_mfcc_t * mf)
89 /* delete filterbank */
90 del_aubio_filterbank (mf->fb);
93 del_fvec (mf->in_dct);
95 /* delete mfcc object */
101 aubio_mfcc_do (aubio_mfcc_t * mf, cvec_t * in, fvec_t * out)
105 /* compute filterbank */
106 aubio_filterbank_do (mf->fb, in, mf->in_dct);
109 fvec_log10 (mf->in_dct);
112 //fvec_pow (mf->in_dct, 3.);
117 /* compute discrete cosine transform */
118 for (j = 0; j < mf->n_filters; j++) {
119 for (k = 0; k < mf->n_coefs; k++) {
120 out->data[k] += mf->in_dct->data[j]
121 * mf->dct_coeffs->data[j][k];