Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mfcc.c

    r7c6c806d rb276dee  
    2424#include "sample.h"
    2525#include "fft.h"
     26#include "filterbank.h"
    2627#include "mfcc.h"
    2728#include "math.h"
    2829
    29 /*
    30 new_aubio_mfcc
    31 aubio_mfcc_do
    32 del_aubio_mfcc
    33 */
     30/** Internal structure for mfcc object **/
    3431
    35 // Computation
    36 // Added last two arguments to be able to pass from example
     32struct aubio_mfcc_t_{
     33  uint_t win_s;             /** grain length */
     34  uint_t samplerate;        /** sample rate (needed?) */
     35  uint_t channels;          /** number of channels */
     36  uint_t n_coefs;           /** number of coefficients (= fb->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_mfft_t * fft_dct;   /** fft object for dct */
     42  cvec_t * fftgrain_dct;    /** output buffer for dct */
     43};
    3744
    3845
     46aubio_mfcc_t * new_aubio_mfcc (uint_t win_s, uint_t samplerate ,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);
    3949
    40 int aubio_mfcc_do(const float *data, const int N, const void *argv, float *result, aubio_mfft_t * fft_dct, cvec_t * fftgrain_dct){
     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_coefs=n_coefs;
     57  mfcc->lowfreq=lowfreq;
     58  mfcc->highfreq=highfreq;
    4159
    42     aubio_mel_filter *f;
    43     int n, filter;
     60  /** filterbank allocation */
     61  mfcc->fb = new_aubio_filterbank_mfcc(n_filters, mfcc->win_s, samplerate, lowfreq, highfreq);
    4462
    45     f = (aubio_mel_filter *)argv;
    46    
    47     for(filter = 0; filter < f->n_filters; filter++){
    48         result[filter] = 0.f;
    49         for(n = 0; n < N; n++){
    50             result[filter] += data[n] * f->filters[filter][n];
    51         }
    52         result[filter] = LOG(result[filter] < XTRACT_LOG_LIMIT ? XTRACT_LOG_LIMIT : result[filter]);
    53     }
     63  /** allocating space for fft object (used for dct) */
     64  mfcc->fft_dct=new_aubio_mfft(n_filters, 1);
    5465
    55     //TODO: check that zero padding
    56     for(n = filter + 1; n < N; n++) result[n] = 0;
    57    
    58     aubio_dct_do(result, f->n_filters, NULL, result, fft_dct, fftgrain_dct);
    59    
    60     return XTRACT_SUCCESS;
     66  /** allocating buffers */
     67  mfcc->in_dct=new_fvec(mfcc->win_s, 1);
     68 
     69  mfcc->fftgrain_dct=new_cvec(n_filters, 1);
     70
     71  return mfcc;
     72};
     73
     74void del_aubio_mfcc(aubio_mfcc_t *mf){
     75  /** deleting filterbank */
     76  del_aubio_filterbank(mf->fb);
     77  /** deleting mfft object */
     78  del_aubio_mfft(mf->fft_dct);
     79  /** deleting buffers */
     80  del_fvec(mf->in_dct);
     81  del_cvec(mf->fftgrain_dct);
     82 
     83  /** deleting mfcc object */
     84  AUBIO_FREE(mf);
    6185}
    6286
    63 // Added last two arguments to be able to pass from example
     87void aubio_mfcc_do(aubio_mfcc_t * mf, cvec_t *in, fvec_t *out){
     88    // compute filterbank
     89    aubio_filterbank_do(mf->fb, in, mf->in_dct);
     90    //TODO: check that zero padding
     91    // the following line seems useless since the in_dct buffer has the correct size
     92    //for(n = filter + 1; n < N; n++) result[n] = 0;
     93   
     94    aubio_dct_do(mf, mf->in_dct, out);
    6495
    65 int aubio_dct_do(const float *data, const int N, const void *argv, float *result, aubio_mfft_t * fft_dct, cvec_t * fftgrain_dct){
    66    
    67    
    68     //call aubio p_voc in dct setting
     96    return;
     97}
    6998
    70     //TODO: fvec as input? Remove data length, N?
     99void aubio_dct_do(aubio_mfcc_t * mf, fvec_t *in, fvec_t *out){
     100    uint_t i;
     101    //compute mag spectrum
     102    aubio_mfft_do (mf->fft_dct, in, mf->fftgrain_dct);
     103    //extract real part of fft grain
     104    //for(i=0; i<mf->n_coefs ;i++){
     105    for(i=0; i<out->length;i++){
     106      out->data[0][i]= mf->fftgrain_dct->norm[0][i]
     107        *COS(mf->fftgrain_dct->phas[0][i]);
     108    }
     109    return;
     110}
    71111
    72     fvec_t * momo = new_fvec(20, 1);
    73     momo->data = data;
    74    
    75     //compute mag spectrum
    76     aubio_mfft_do (fft_dct, data, fftgrain_dct);
    77 
    78     int i;
    79     //extract real part of fft grain
    80     for(i=0; i<N ;i++){
    81       result[i]= fftgrain_dct->norm[0][i]*COS(fftgrain_dct->phas[0][i]);
    82     }
    83 
    84 
    85     return XTRACT_SUCCESS;
    86 }
Note: See TracChangeset for help on using the changeset viewer.