source: src/spectral/mfcc.c @ 10fafc2

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since 10fafc2 was 10fafc2, checked in by Paul Brossier <piem@piem.org>, 5 years ago

[mfcc] add slaney (default) and documentation

  • Property mode set to 100644
File size: 4.4 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/dct.h"
32#include "spectral/mfcc.h"
33
34#ifdef HAVE_NOOPT
35#define HAVE_SLOW_DCT 1
36#endif
37
38/** Internal structure for mfcc object */
39
40struct _aubio_mfcc_t
41{
42  uint_t win_s;             /** grain length */
43  uint_t samplerate;        /** sample rate (needed?) */
44  uint_t n_filters;         /** number of filters */
45  uint_t n_coefs;           /** number of coefficients (<= n_filters/2 +1) */
46  aubio_filterbank_t *fb;   /** filter bank */
47  fvec_t *in_dct;           /** input buffer for dct * [fb->n_filters] */
48#if defined(HAVE_SLOW_DCT)
49  fmat_t *dct_coeffs;       /** DCT transform n_filters * n_coeffs */
50#else
51  aubio_dct_t *dct;
52  fvec_t *output;
53#endif
54};
55
56
57aubio_mfcc_t *
58new_aubio_mfcc (uint_t win_s, uint_t n_filters, uint_t n_coefs,
59    uint_t samplerate)
60{
61
62  /* allocate space for mfcc object */
63  aubio_mfcc_t *mfcc = AUBIO_NEW (aubio_mfcc_t);
64#if defined(HAVE_SLOW_DCT)
65  smpl_t scaling;
66
67  uint_t i, j;
68#endif
69
70  mfcc->win_s = win_s;
71  mfcc->samplerate = samplerate;
72  mfcc->n_filters = n_filters;
73  mfcc->n_coefs = n_coefs;
74
75  /* filterbank allocation */
76  mfcc->fb = new_aubio_filterbank (n_filters, mfcc->win_s);
77  aubio_filterbank_set_mel_coeffs_slaney (mfcc->fb, samplerate);
78
79  /* allocating buffers */
80  mfcc->in_dct = new_fvec (n_filters);
81
82#if defined(HAVE_SLOW_DCT)
83  mfcc->dct_coeffs = new_fmat (n_coefs, n_filters);
84
85  /* compute DCT transform dct_coeffs[j][i] as
86     cos ( j * (i+.5) * PI / n_filters ) */
87  scaling = 1. / SQRT (n_filters / 2.);
88  for (i = 0; i < n_filters; i++) {
89    for (j = 0; j < n_coefs; j++) {
90      mfcc->dct_coeffs->data[j][i] =
91          scaling * COS (j * (i + 0.5) * PI / n_filters);
92    }
93    mfcc->dct_coeffs->data[0][i] *= SQRT (2.) / 2.;
94  }
95#else
96  mfcc->dct = new_aubio_dct (n_filters);
97  mfcc->output = new_fvec (n_filters);
98#endif
99
100  return mfcc;
101}
102
103void
104del_aubio_mfcc (aubio_mfcc_t * mf)
105{
106
107  /* delete filterbank */
108  del_aubio_filterbank (mf->fb);
109
110  /* delete buffers */
111  del_fvec (mf->in_dct);
112#if defined(HAVE_SLOW_DCT)
113  del_fmat (mf->dct_coeffs);
114#else
115  del_aubio_dct (mf->dct);
116  del_fvec (mf->output);
117#endif
118
119  /* delete mfcc object */
120  AUBIO_FREE (mf);
121}
122
123
124void
125aubio_mfcc_do (aubio_mfcc_t * mf, const cvec_t * in, fvec_t * out)
126{
127#ifndef HAVE_SLOW_DCT
128  fvec_t tmp;
129#endif
130  /* compute filterbank */
131  aubio_filterbank_do (mf->fb, in, mf->in_dct);
132
133  /* compute log10 */
134  fvec_log10 (mf->in_dct);
135
136  /* raise power */
137  //fvec_pow (mf->in_dct, 3.);
138
139  /* compute mfccs */
140#if defined(HAVE_SLOW_DCT)
141  fmat_vecmul(mf->dct_coeffs, mf->in_dct, out);
142#else
143  aubio_dct_do(mf->dct, mf->in_dct, mf->output);
144  // copy only first n_coeffs elements
145  // TODO assert mf->output->length == n_coeffs
146  tmp.data = mf->output->data;
147  tmp.length = out->length;
148  fvec_copy(&tmp, out);
149#endif
150
151  return;
152}
153
154uint_t aubio_mfcc_set_power (aubio_mfcc_t *mf, smpl_t power)
155{
156  return aubio_filterbank_set_power(mf->fb, power);
157}
158
159uint_t aubio_mfcc_get_power (aubio_mfcc_t *mf)
160{
161  return aubio_filterbank_get_power(mf->fb);
162}
163
164uint_t aubio_mfcc_set_mel_coeffs (aubio_mfcc_t *mf, smpl_t freq_min,
165    smpl_t freq_max)
166{
167  return aubio_filterbank_set_mel_coeffs(mf->fb, mf->samplerate,
168      freq_min, freq_max);
169}
170
171uint_t aubio_mfcc_set_mel_coeffs_htk (aubio_mfcc_t *mf, smpl_t freq_min,
172    smpl_t freq_max)
173{
174  return aubio_filterbank_set_mel_coeffs_htk(mf->fb, mf->samplerate,
175      freq_min, freq_max);
176}
177
178uint_t aubio_mfcc_set_mel_coeffs_slaney (aubio_mfcc_t *mf, smpl_t freq_min,
179    smpl_t freq_max)
180{
181  return aubio_filterbank_set_mel_coeffs_slaney (mf->fb, mf->samplerate);
182}
Note: See TracBrowser for help on using the repository browser.