source: src/spectral/mfcc.c @ f3bee79

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

src/spectral/: remove include from filterbank_mel.h, modify mfcc.c to include filterbank.h

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