source: src/spectral/mfcc.c @ 32d6958

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

src/: more moving and splitting

  • Property mode set to 100644
File size: 3.8 KB
Line 
1/*
2   Copyright (C) 2006 Amaury Hazan
3   Ported to aubio from LibXtract
4   http://libxtract.sourceforge.net/
5   
6
7   This program 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 2 of the License, or
10   (at your option) any later version.
11
12   This program 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 this program; if not, write to the Free Software
19   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21*/
22
23#include "aubio_priv.h"
24#include "sample.h"
25#include "spectral/fft.h"
26#include "spectral/filterbank.h"
27#include "spectral/mfcc.h"
28
29/** Internal structure for mfcc object **/
30
31struct aubio_mfcc_t_{
32  uint_t win_s;             /** grain length */
33  uint_t samplerate;        /** sample rate (needed?) */
34  uint_t channels;          /** number of channels */
35  uint_t n_filters;         /** number of  *filters */
36  uint_t n_coefs;           /** number of coefficients (<= n_filters/2 +1) */
37  smpl_t lowfreq;           /** lowest frequency for filters */ 
38  smpl_t highfreq;          /** highest frequency for filters */
39  aubio_filterbank_t * fb;  /** filter bank */
40  fvec_t * in_dct;          /** input buffer for dct * [fb->n_filters] */
41  aubio_fft_t * fft_dct;   /** fft object for dct */
42  cvec_t * fftgrain_dct;    /** output buffer for dct */
43};
44
45
46aubio_mfcc_t * new_aubio_mfcc (uint_t win_s, uint_t samplerate, uint_t n_filters, uint_t n_coefs, smpl_t lowfreq, smpl_t highfreq, uint_t channels){
47  /** allocating space for mfcc object */
48  aubio_mfcc_t * mfcc = AUBIO_NEW(aubio_mfcc_t);
49
50  //we need (n_coefs-1)*2 filters to obtain n_coefs coefficients after dct
51  //uint_t n_filters = (n_coefs-1)*2;
52 
53  mfcc->win_s=win_s;
54  mfcc->samplerate=samplerate;
55  mfcc->channels=channels;
56  mfcc->n_filters=n_filters;
57  mfcc->n_coefs=n_coefs;
58  mfcc->lowfreq=lowfreq;
59  mfcc->highfreq=highfreq;
60
61 
62  /** filterbank allocation */
63  mfcc->fb = new_aubio_filterbank_mfcc(n_filters, mfcc->win_s, samplerate, lowfreq, highfreq);
64
65  /** allocating space for fft object (used for dct) */
66  mfcc->fft_dct=new_aubio_fft(n_filters, 1);
67
68  /** allocating buffers */
69  mfcc->in_dct=new_fvec(mfcc->win_s, 1);
70 
71  mfcc->fftgrain_dct=new_cvec(n_filters, 1);
72
73  return mfcc;
74};
75
76void del_aubio_mfcc(aubio_mfcc_t *mf){
77  /** deleting filterbank */
78  del_aubio_filterbank(mf->fb);
79  /** deleting fft object */
80  del_aubio_fft(mf->fft_dct);
81  /** deleting buffers */
82  del_fvec(mf->in_dct);
83  del_cvec(mf->fftgrain_dct);
84 
85  /** deleting mfcc object */
86  AUBIO_FREE(mf);
87}
88
89
90/** intermediate dct involved in aubio_mfcc_do
91
92  \param mf mfcc object as returned by new_aubio_mfcc
93  \param in input spectrum (n_filters long)
94  \param out output mel coefficients buffer (n_filters/2 +1 long)
95
96*/
97void aubio_dct_do(aubio_mfcc_t * mf, fvec_t *in, fvec_t *out);
98
99void aubio_mfcc_do(aubio_mfcc_t * mf, cvec_t *in, fvec_t *out){
100    // compute filterbank
101    aubio_filterbank_do(mf->fb, in, mf->in_dct);
102    //TODO: check that zero padding
103    // the following line seems useless since the in_dct buffer has the correct size
104    //for(n = filter + 1; n < N; n++) result[n] = 0;
105   
106    aubio_dct_do(mf, mf->in_dct, out);
107
108    return;
109}
110
111void aubio_dct_do(aubio_mfcc_t * mf, fvec_t *in, fvec_t *out){
112    uint_t i;
113    //compute mag spectrum
114    aubio_fft_do (mf->fft_dct, in, mf->fftgrain_dct);
115    //extract real part of fft grain
116    for(i=0; i<mf->n_coefs ;i++){
117    //for(i=0; i<out->length;i++){
118      out->data[0][i]= mf->fftgrain_dct->norm[0][i]
119        *COS(mf->fftgrain_dct->phas[0][i]);
120    }
121    return;
122}
123
Note: See TracBrowser for help on using the repository browser.