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/dct.h"
32 #include "spectral/mfcc.h"
35 #define HAVE_SLOW_DCT 1
38 /** Internal structure for mfcc object */
42 uint_t win_s; /** grain length */
43 uint_t samplerate; /** sample rate (needed?) */
44 uint_t n_filters; /** number of filters */
45 uint_t n_coefs; /** number of coefficients (<= n_filters/2 +1) */
46 aubio_filterbank_t *fb; /** filter bank */
47 fvec_t *in_dct; /** input buffer for dct * [fb->n_filters] */
48 #if defined(HAVE_SLOW_DCT)
49 fmat_t *dct_coeffs; /** DCT transform n_filters * n_coeffs */
59 new_aubio_mfcc (uint_t win_s, uint_t n_filters, uint_t n_coefs,
63 /* allocate space for mfcc object */
64 aubio_mfcc_t *mfcc = AUBIO_NEW (aubio_mfcc_t);
65 #if defined(HAVE_SLOW_DCT)
72 mfcc->samplerate = samplerate;
73 mfcc->n_filters = n_filters;
74 mfcc->n_coefs = n_coefs;
76 /* filterbank allocation */
77 mfcc->fb = new_aubio_filterbank (n_filters, mfcc->win_s);
79 aubio_filterbank_set_mel_coeffs_slaney (mfcc->fb, samplerate);
81 aubio_filterbank_set_mel_coeffs(mfcc->fb, samplerate,
84 /* allocating buffers */
85 mfcc->in_dct = new_fvec (n_filters);
87 #if defined(HAVE_SLOW_DCT)
88 mfcc->dct_coeffs = new_fmat (n_coefs, n_filters);
90 /* compute DCT transform dct_coeffs[j][i] as
91 cos ( j * (i+.5) * PI / n_filters ) */
92 scaling = 1. / SQRT (n_filters / 2.);
93 for (i = 0; i < n_filters; i++) {
94 for (j = 0; j < n_coefs; j++) {
95 mfcc->dct_coeffs->data[j][i] =
96 scaling * COS (j * (i + 0.5) * PI / n_filters);
98 mfcc->dct_coeffs->data[0][i] *= SQRT (2.) / 2.;
101 mfcc->dct = new_aubio_dct (n_filters);
102 mfcc->output = new_fvec (n_filters);
111 del_aubio_mfcc (aubio_mfcc_t * mf)
114 /* delete filterbank */
115 del_aubio_filterbank (mf->fb);
118 del_fvec (mf->in_dct);
119 #if defined(HAVE_SLOW_DCT)
120 del_fmat (mf->dct_coeffs);
122 del_aubio_dct (mf->dct);
123 del_fvec (mf->output);
126 /* delete mfcc object */
132 aubio_mfcc_do (aubio_mfcc_t * mf, const cvec_t * in, fvec_t * out)
134 #ifndef HAVE_SLOW_DCT
138 /* compute filterbank */
139 aubio_filterbank_do (mf->fb, in, mf->in_dct);
142 fvec_log10 (mf->in_dct);
144 if (mf->scale != 1) fvec_mul (mf->in_dct, mf->scale);
147 #if defined(HAVE_SLOW_DCT)
148 fmat_vecmul(mf->dct_coeffs, mf->in_dct, out);
150 aubio_dct_do(mf->dct, mf->in_dct, mf->output);
151 // copy only first n_coeffs elements
152 // TODO assert mf->output->length == n_coeffs
153 tmp.data = mf->output->data;
154 tmp.length = out->length;
155 fvec_copy(&tmp, out);
161 uint_t aubio_mfcc_set_power (aubio_mfcc_t *mf, smpl_t power)
163 return aubio_filterbank_set_power(mf->fb, power);
166 uint_t aubio_mfcc_get_power (aubio_mfcc_t *mf)
168 return aubio_filterbank_get_power(mf->fb);
171 uint_t aubio_mfcc_set_scale (aubio_mfcc_t *mf, smpl_t scale)
177 uint_t aubio_mfcc_get_scale (aubio_mfcc_t *mf)
182 uint_t aubio_mfcc_set_mel_coeffs (aubio_mfcc_t *mf, smpl_t freq_min,
185 return aubio_filterbank_set_mel_coeffs(mf->fb, mf->samplerate,
189 uint_t aubio_mfcc_set_mel_coeffs_htk (aubio_mfcc_t *mf, smpl_t freq_min,
192 return aubio_filterbank_set_mel_coeffs_htk(mf->fb, mf->samplerate,
196 uint_t aubio_mfcc_set_mel_coeffs_slaney (aubio_mfcc_t *mf)
198 return aubio_filterbank_set_mel_coeffs_slaney (mf->fb, mf->samplerate);