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