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