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_mel.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(n_filters, mfcc->win_s);
65 aubio_filterbank_set_mel_coeffs(mfcc->fb, samplerate, lowfreq, highfreq);
67 /** allocating space for fft object (used for dct) */
68 mfcc->fft_dct=new_aubio_fft(n_filters, 1);
70 /** allocating buffers */
71 mfcc->in_dct=new_fvec(mfcc->win_s, 1);
73 mfcc->fftgrain_dct=new_cvec(n_filters, 1);
78 void del_aubio_mfcc(aubio_mfcc_t *mf){
79 /** deleting filterbank */
80 del_aubio_filterbank(mf->fb);
81 /** deleting fft object */
82 del_aubio_fft(mf->fft_dct);
83 /** deleting buffers */
85 del_cvec(mf->fftgrain_dct);
87 /** deleting mfcc object */
92 /** intermediate dct involved in aubio_mfcc_do
94 \param mf mfcc object as returned by new_aubio_mfcc
95 \param in input spectrum (n_filters long)
96 \param out output mel coefficients buffer (n_filters/2 +1 long)
99 void aubio_dct_do(aubio_mfcc_t * mf, fvec_t *in, fvec_t *out);
101 void aubio_mfcc_do(aubio_mfcc_t * mf, cvec_t *in, fvec_t *out){
102 // compute filterbank
103 aubio_filterbank_do(mf->fb, in, mf->in_dct);
104 //TODO: check that zero padding
105 // the following line seems useless since the in_dct buffer has the correct size
106 //for(n = filter + 1; n < N; n++) result[n] = 0;
108 aubio_dct_do(mf, mf->in_dct, out);
113 void aubio_dct_do(aubio_mfcc_t * mf, fvec_t *in, fvec_t *out){
115 //compute mag spectrum
116 aubio_fft_do (mf->fft_dct, in, mf->fftgrain_dct);
117 //extract real part of fft grain
118 for(i=0; i<mf->n_coefs ;i++){
119 //for(i=0; i<out->length;i++){
120 out->data[0][i]= mf->fftgrain_dct->norm[0][i]
121 *COS(mf->fftgrain_dct->phas[0][i]);