2 Copyright (C) 2006 Amaury Hazan
3 Ported to aubio from LibXtract
4 http://libxtract.sourceforge.net/
7 This program 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 2 of the License, or
10 (at your option) any later version.
12 This program 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 this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "aubio_priv.h"
26 #include "spectral/fft.h"
27 #include "spectral/filterbank.h"
28 #include "spectral/mfcc.h"
30 /** Internal structure for mfcc object **/
33 uint_t win_s; /** grain length */
34 uint_t samplerate; /** sample rate (needed?) */
35 uint_t channels; /** number of channels */
36 uint_t n_filters; /** number of *filters */
37 uint_t n_coefs; /** number of coefficients (<= n_filters/2 +1) */
38 smpl_t lowfreq; /** lowest frequency for filters */
39 smpl_t highfreq; /** highest frequency for filters */
40 aubio_filterbank_t * fb; /** filter bank */
41 fvec_t * in_dct; /** input buffer for dct * [fb->n_filters] */
42 aubio_fft_t * fft_dct; /** fft object for dct */
43 cvec_t * fftgrain_dct; /** output buffer for dct */
47 aubio_mfcc_t * new_aubio_mfcc (uint_t win_s, uint_t samplerate, uint_t n_filters, uint_t n_coefs, smpl_t lowfreq, smpl_t highfreq, uint_t channels){
48 /** allocating space for mfcc object */
49 aubio_mfcc_t * mfcc = AUBIO_NEW(aubio_mfcc_t);
51 //we need (n_coefs-1)*2 filters to obtain n_coefs coefficients after dct
52 //uint_t n_filters = (n_coefs-1)*2;
55 mfcc->samplerate=samplerate;
56 mfcc->channels=channels;
57 mfcc->n_filters=n_filters;
58 mfcc->n_coefs=n_coefs;
59 mfcc->lowfreq=lowfreq;
60 mfcc->highfreq=highfreq;
63 /** filterbank allocation */
64 mfcc->fb = new_aubio_filterbank_mfcc(n_filters, mfcc->win_s, samplerate, lowfreq, highfreq);
66 /** allocating space for fft object (used for dct) */
67 mfcc->fft_dct=new_aubio_fft(n_filters, 1);
69 /** allocating buffers */
70 mfcc->in_dct=new_fvec(mfcc->win_s, 1);
72 mfcc->fftgrain_dct=new_cvec(n_filters, 1);
77 void del_aubio_mfcc(aubio_mfcc_t *mf){
78 /** deleting filterbank */
79 del_aubio_filterbank(mf->fb);
80 /** deleting fft object */
81 del_aubio_fft(mf->fft_dct);
82 /** deleting buffers */
84 del_cvec(mf->fftgrain_dct);
86 /** deleting mfcc object */
91 /** intermediate dct involved in aubio_mfcc_do
93 \param mf mfcc object as returned by new_aubio_mfcc
94 \param in input spectrum (n_filters long)
95 \param out output mel coefficients buffer (n_filters/2 +1 long)
98 void aubio_dct_do(aubio_mfcc_t * mf, fvec_t *in, fvec_t *out);
100 void aubio_mfcc_do(aubio_mfcc_t * mf, cvec_t *in, fvec_t *out){
101 // compute filterbank
102 aubio_filterbank_do(mf->fb, in, mf->in_dct);
103 //TODO: check that zero padding
104 // the following line seems useless since the in_dct buffer has the correct size
105 //for(n = filter + 1; n < N; n++) result[n] = 0;
107 aubio_dct_do(mf, mf->in_dct, out);
112 void aubio_dct_do(aubio_mfcc_t * mf, fvec_t *in, fvec_t *out){
114 //compute mag spectrum
115 aubio_fft_do (mf->fft_dct, in, mf->fftgrain_dct);
116 //extract real part of fft grain
117 for(i=0; i<mf->n_coefs ;i++){
118 //for(i=0; i<out->length;i++){
119 out->data[0][i]= mf->fftgrain_dct->norm[0][i]
120 *COS(mf->fftgrain_dct->phas[0][i]);