source: src/spectral/mfcc.c @ 385a06e2

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 385a06e2 was 78c21cf, checked in by Paul Brossier <piem@piem.org>, 11 years ago

src/spectral/mfcc.c: improve build with -Wdeclaration-after-statement

  • Property mode set to 100644
File size: 3.2 KB
Line 
1/*
2  Copyright (C) 2007-2009 Paul Brossier <piem@aubio.org>
3                      and Amaury Hazan <ahazan@iua.upf.edu>
4
5  This file is part of aubio.
6
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.
11
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/>.
19
20*/
21
22#include "aubio_priv.h"
23#include "fvec.h"
24#include "fmat.h"
25#include "cvec.h"
26#include "mathutils.h"
27#include "vecutils.h"
28#include "spectral/fft.h"
29#include "spectral/filterbank.h"
30#include "spectral/filterbank_mel.h"
31#include "spectral/mfcc.h"
32
33/** Internal structure for mfcc object */
34
35struct _aubio_mfcc_t
36{
37  uint_t win_s;             /** grain length */
38  uint_t samplerate;        /** sample rate (needed?) */
39  uint_t n_filters;         /** number of  *filters */
40  uint_t n_coefs;           /** number of coefficients (<= n_filters/2 +1) */
41  aubio_filterbank_t *fb;   /** filter bank */
42  fvec_t *in_dct;           /** input buffer for dct * [fb->n_filters] */
43  fmat_t *dct_coeffs;       /** DCT transform n_filters * n_coeffs */
44};
45
46
47aubio_mfcc_t *
48new_aubio_mfcc (uint_t win_s, uint_t n_filters, uint_t n_coefs,
49    uint_t samplerate)
50{
51
52  /* allocate space for mfcc object */
53  aubio_mfcc_t *mfcc = AUBIO_NEW (aubio_mfcc_t);
54  smpl_t scaling;
55
56  uint_t i, j;
57
58  mfcc->win_s = win_s;
59  mfcc->samplerate = samplerate;
60  mfcc->n_filters = n_filters;
61  mfcc->n_coefs = n_coefs;
62
63  /* filterbank allocation */
64  mfcc->fb = new_aubio_filterbank (n_filters, mfcc->win_s);
65  aubio_filterbank_set_mel_coeffs_slaney (mfcc->fb, samplerate);
66
67  /* allocating buffers */
68  mfcc->in_dct = new_fvec (n_filters);
69
70  mfcc->dct_coeffs = new_fmat (n_filters, n_coefs);
71
72  /* compute DCT transform dct_coeffs[i][j] as
73     cos ( j * (i+.5) * PI / n_filters ) */
74  scaling = 1. / SQRT (n_filters / 2.);
75  for (i = 0; i < n_filters; i++) {
76    for (j = 0; j < n_coefs; j++) {
77      mfcc->dct_coeffs->data[i][j] =
78          scaling * COS (j * (i + 0.5) * PI / n_filters);
79    }
80    mfcc->dct_coeffs->data[i][0] *= SQRT (2.) / 2.;
81  }
82
83  return mfcc;
84};
85
86void
87del_aubio_mfcc (aubio_mfcc_t * mf)
88{
89
90  /* delete filterbank */
91  del_aubio_filterbank (mf->fb);
92
93  /* delete buffers */
94  del_fvec (mf->in_dct);
95  del_fmat (mf->dct_coeffs);
96
97  /* delete mfcc object */
98  AUBIO_FREE (mf);
99}
100
101
102void
103aubio_mfcc_do (aubio_mfcc_t * mf, cvec_t * in, fvec_t * out)
104{
105  uint_t j, k;
106
107  /* compute filterbank */
108  aubio_filterbank_do (mf->fb, in, mf->in_dct);
109
110  /* compute log10 */
111  fvec_log10 (mf->in_dct);
112
113  /* raise power */
114  //fvec_pow (mf->in_dct, 3.);
115
116  /* zeros output */
117  fvec_zeros(out);
118
119  /* compute discrete cosine transform */
120  for (j = 0; j < mf->n_filters; j++) {
121    for (k = 0; k < mf->n_coefs; k++) {
122      out->data[k] += mf->in_dct->data[j]
123          * mf->dct_coeffs->data[j][k];
124    }
125  }
126
127  return;
128}
Note: See TracBrowser for help on using the repository browser.