[88199ce] | 1 | /* |
---|
[7a84b21] | 2 | Copyright (C) 2007-2009 Paul Brossier <piem@aubio.org> |
---|
| 3 | and Amaury Hazan <ahazan@iua.upf.edu> |
---|
[88199ce] | 4 | |
---|
[7a84b21] | 5 | This file is part of Aubio. |
---|
[88199ce] | 6 | |
---|
[7a84b21] | 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. |
---|
[88199ce] | 11 | |
---|
[7a84b21] | 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. |
---|
| 16 | |
---|
| 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/>. |
---|
[88199ce] | 19 | |
---|
| 20 | */ |
---|
| 21 | |
---|
[7c6c806d] | 22 | #include "aubio_priv.h" |
---|
[6c7d49b] | 23 | #include "fvec.h" |
---|
| 24 | #include "cvec.h" |
---|
[32d6958] | 25 | #include "spectral/fft.h" |
---|
[06cae6c] | 26 | #include "spectral/filterbank_mel.h" |
---|
[32d6958] | 27 | #include "spectral/mfcc.h" |
---|
[88199ce] | 28 | |
---|
[7a84b21] | 29 | /** Internal structure for mfcc object */ |
---|
[88199ce] | 30 | |
---|
[d99d819] | 31 | struct _aubio_mfcc_t |
---|
[7a84b21] | 32 | { |
---|
[34d72f0] | 33 | uint_t win_s; /** grain length */ |
---|
| 34 | uint_t samplerate; /** sample rate (needed?) */ |
---|
[7a46bf6] | 35 | uint_t n_filters; /** number of *filters */ |
---|
| 36 | uint_t n_coefs; /** number of coefficients (<= n_filters/2 +1) */ |
---|
[7a84b21] | 37 | aubio_filterbank_t *fb; /** filter bank */ |
---|
| 38 | fvec_t *in_dct; /** input buffer for dct * [fb->n_filters] */ |
---|
| 39 | fvec_t *dct_coeffs; /** DCT transform n_filters * n_coeffs */ |
---|
[8708556] | 40 | }; |
---|
| 41 | |
---|
| 42 | |
---|
[7a84b21] | 43 | aubio_mfcc_t * |
---|
| 44 | new_aubio_mfcc (uint_t win_s, uint_t samplerate, uint_t n_filters, |
---|
| 45 | uint_t n_coefs) |
---|
| 46 | { |
---|
| 47 | |
---|
| 48 | /* allocate space for mfcc object */ |
---|
| 49 | aubio_mfcc_t *mfcc = AUBIO_NEW (aubio_mfcc_t); |
---|
[c185ebb] | 50 | |
---|
| 51 | uint_t i, j; |
---|
| 52 | |
---|
[7a84b21] | 53 | mfcc->win_s = win_s; |
---|
| 54 | mfcc->samplerate = samplerate; |
---|
| 55 | mfcc->n_filters = n_filters; |
---|
| 56 | mfcc->n_coefs = n_coefs; |
---|
[cfe4038] | 57 | |
---|
[7a84b21] | 58 | /* filterbank allocation */ |
---|
| 59 | mfcc->fb = new_aubio_filterbank (n_filters, mfcc->win_s); |
---|
| 60 | aubio_filterbank_set_mel_coeffs_slaney (mfcc->fb, samplerate); |
---|
[8708556] | 61 | |
---|
[7a84b21] | 62 | /* allocating buffers */ |
---|
| 63 | mfcc->in_dct = new_fvec (n_filters, 1); |
---|
[8708556] | 64 | |
---|
[7a84b21] | 65 | mfcc->dct_coeffs = new_fvec (n_coefs, n_filters); |
---|
[c185ebb] | 66 | |
---|
| 67 | /* compute DCT transform dct_coeffs[i][j] as |
---|
[7a84b21] | 68 | cos ( j * (i+.5) * PI / n_filters ) */ |
---|
| 69 | smpl_t scaling = 1. / SQRT (n_filters / 2.); |
---|
| 70 | for (i = 0; i < n_filters; i++) { |
---|
[c185ebb] | 71 | for (j = 0; j < n_coefs; j++) { |
---|
| 72 | mfcc->dct_coeffs->data[i][j] = |
---|
[7a84b21] | 73 | scaling * COS (j * (i + 0.5) * PI / n_filters); |
---|
[c185ebb] | 74 | } |
---|
[7a84b21] | 75 | mfcc->dct_coeffs->data[i][0] *= SQRT (2.) / 2.; |
---|
[c185ebb] | 76 | } |
---|
[8708556] | 77 | |
---|
| 78 | return mfcc; |
---|
| 79 | }; |
---|
| 80 | |
---|
[7a84b21] | 81 | void |
---|
| 82 | del_aubio_mfcc (aubio_mfcc_t * mf) |
---|
| 83 | { |
---|
| 84 | |
---|
| 85 | /* delete filterbank */ |
---|
| 86 | del_aubio_filterbank (mf->fb); |
---|
| 87 | |
---|
| 88 | /* delete buffers */ |
---|
| 89 | del_fvec (mf->in_dct); |
---|
| 90 | |
---|
| 91 | /* delete mfcc object */ |
---|
| 92 | AUBIO_FREE (mf); |
---|
[88199ce] | 93 | } |
---|
| 94 | |
---|
[177f09a] | 95 | |
---|
[7a84b21] | 96 | void |
---|
| 97 | aubio_mfcc_do (aubio_mfcc_t * mf, cvec_t * in, fvec_t * out) |
---|
| 98 | { |
---|
[c185ebb] | 99 | uint_t i, j; |
---|
[c218821] | 100 | |
---|
[7a84b21] | 101 | /* compute filterbank */ |
---|
| 102 | aubio_filterbank_do (mf->fb, in, mf->in_dct); |
---|
[c185ebb] | 103 | |
---|
[c218821] | 104 | /* zeros output */ |
---|
| 105 | fvec_zeros(out); |
---|
| 106 | |
---|
| 107 | /* compute discrete cosine transform */ |
---|
[7a84b21] | 108 | for (i = 0; i < mf->n_filters; i++) { |
---|
[c185ebb] | 109 | for (j = 0; j < mf->n_coefs; j++) { |
---|
| 110 | out->data[0][j] += mf->in_dct->data[0][i] |
---|
[7a84b21] | 111 | * mf->dct_coeffs->data[i][j]; |
---|
[97886fa] | 112 | } |
---|
[c185ebb] | 113 | } |
---|
[cfe4038] | 114 | |
---|
[c185ebb] | 115 | return; |
---|
| 116 | } |
---|