Changeset f72ceeb for src/filterbank.c


Ignore:
Timestamp:
Sep 11, 2007, 9:25:43 PM (17 years ago)
Author:
Amaury Hazan <mahmoudax@gmail.com>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
83d5abf
Parents:
ef1c3b7
Message:

aubiomfcc.c: added a dump filterbank funcion
filterbank: written mfcc filterbank init according to slaney auditory toolbox (90%)
plotfb.py: added x log scale option
#some hacks and debug test

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/filterbank.c

    ref1c3b7 rf72ceeb  
    5555}
    5656
    57 aubio_filterbank_t * new_aubio_filterbank_mfcc(uint_t n_filters, uint_t win_s, smpl_t samplerate, smpl_t freq_min, smpl_t freq_max){
     57aubio_filterbank_t * new_aubio_filterbank_mfcc(uint_t n_filters, uint_t win_s, uint_t samplerate, smpl_t freq_min, smpl_t freq_max){
    5858 
    5959  smpl_t nyquist = samplerate/2.;
     
    160160
    161161
    162 aubio_filterbank_t * new_aubio_filterbank_mfcc2(uint_t n_filters, uint_t win_s, smpl_t samplerate, smpl_t freq_min, smpl_t freq_max){
     162aubio_filterbank_t * new_aubio_filterbank_mfcc2(uint_t n_filters, uint_t win_s, uint_t samplerate, smpl_t freq_min, smpl_t freq_max){
     163 
     164  aubio_filterbank_t * fb = new_aubio_filterbank(n_filters, win_s);
     165 
    163166 
    164167  //slaney params
     
    178181  fvec_t * triangle_heights=new_fvec( allFilters, 1);
    179182  //lookup table of each bin frequency in hz
    180   fvec_t * fft_freqs=(win_s, 1);
     183  fvec_t * fft_freqs=new_fvec(win_s, 1);
    181184
    182185  uint_t filter_cnt, bin_cnt;
     
    184187  //first: filling all the linear filter frequencies
    185188  for(filter_cnt=0; filter_cnt<linearFilters; filter_cnt++){
    186     freqs[0][filter_cnt]=lowestFrequency+ filter_cnt*linearSpacing;
    187   }
    188   smpl_t lastlinearCF=freqs[0][filter_cnt-1];
     189    freqs->data[0][filter_cnt]=lowestFrequency+ filter_cnt*linearSpacing;
     190  }
     191  smpl_t lastlinearCF=freqs->data[0][filter_cnt-1];
    189192 
    190193  //second: filling all the log filter frequencies
    191194  for(filter_cnt=0; filter_cnt<logFilters+2; filter_cnt++){
    192     freqs[filter_cnt+linearFilters]=lastlinearCF*(pow(logSpacing,filter_cnt+1));
    193   }
    194   //TODO: check if the referencing above works!
     195    freqs->data[0][filter_cnt+linearFilters]=lastlinearCF*(pow(logSpacing,filter_cnt+1));
     196  }
     197 
     198 
     199  //TODO: Check how these f_vec will be freed
    195200  lower_freqs->data=freqs->data;
    196   center_freqs->data=&(freqs->data[1]);
    197   upper_freqs->data=&(freqs->data[2]);
     201  center_freqs->data[0]=&(freqs->data[0][1]);
     202  upper_freqs->data[0]=&(freqs->data[0][2]);
     203
    198204 
    199205  //computing triangle heights so that each triangle has unit area
    200206  for(filter_cnt=0; filter_cnt<allFilters; filter_cnt++){
    201     triangle_heights[filter_cnt]=2./(upper_freqs[filter_cnt]-lower_freqs[filter_cnt]);
    202   }
    203 
    204   //filling the lookup table, which assign the frequency in hz to each bin
     207    triangle_heights->data[0][filter_cnt]=2./(upper_freqs->data[0][filter_cnt]-lower_freqs->data[0][filter_cnt]);
     208  }
     209
     210  //debug
     211  for(filter_cnt=0; filter_cnt<allFilters; filter_cnt++)
     212    //printf("filter n. %d %f %f %f %f\n",filter_cnt, lower_freqs->data[0][filter_cnt], center_freqs->data[0][filter_cnt], upper_freqs->data[0][filter_cnt], triangle_heights->data[0][filter_cnt]);
     213 
     214 
     215  //filling the fft_freqs lookup table, which assigns the frequency in hz to each bin
     216 
    205217  for(bin_cnt=0; bin_cnt<win_s; bin_cnt++){
     218   
    206219    //TODO: check the formula!
    207     fft_freqs[bin_cnt]=((smpl_t) bin_cnt/(smpl_t) win_s)* (smpl_t) samplerate;
    208   }
    209  
    210   //building each filter
     220   
     221    fft_freqs->data[0][bin_cnt]= (smpl_t)samplerate* (smpl_t)bin_cnt/ (smpl_t)win_s;
     222
     223  }
     224 
     225 
     226  //building each filter table
    211227  for(filter_cnt=0; filter_cnt<allFilters; filter_cnt++){
    212     //finding bins corresponding to lower, center, and upper frequencies
    213 
    214     for(bin_cnt=0; bin_cnt<; bin_cnt++)
     228
     229    //TODO:check special case : lower freq =0
     230   
     231    //calculating rise increment in mag/Hz
     232    smpl_t riseInc= triangle_heights->data[0][filter_cnt]/(center_freqs->data[0][filter_cnt]-lower_freqs->data[0][filter_cnt]);
     233   
     234    //zeroing begining of filter
     235    //printf("\nfilter %d",filter_cnt);
     236
     237    //printf("\nzero begin\n");
     238   
     239    for(bin_cnt=0; bin_cnt<win_s-1; bin_cnt++){
     240      //zeroing beigining of array
    215241      fb->filters[filter_cnt]->data[0][bin_cnt]=0.f;
    216   }
    217  
    218   // xtract
    219   smpl_t nyquist = samplerate/2.;
    220   uint_t style = 1;
    221   aubio_filterbank_t * fb = new_aubio_filterbank(n_filters, win_s);
    222 
    223   uint_t n, i, k, *fft_peak, M, next_peak;
    224   smpl_t norm, mel_freq_max, mel_freq_min, norm_fact, height, inc, val,
    225          freq_bw_mel, *mel_peak, *height_norm, *lin_peak;
    226 
    227   mel_peak = height_norm = lin_peak = NULL;
    228   fft_peak = NULL;
    229   norm = 1;
    230 
    231   mel_freq_max = 1127 * log(1 + freq_max / 700);
    232   mel_freq_min = 1127 * log(1 + freq_min / 700);
    233   freq_bw_mel = (mel_freq_max - mel_freq_min) / fb->n_filters;
    234 
    235   mel_peak = (smpl_t *)malloc((fb->n_filters + 2) * sizeof(smpl_t));
    236   /* +2 for zeros at start and end */
    237   lin_peak = (smpl_t *)malloc((fb->n_filters + 2) * sizeof(smpl_t));
    238   fft_peak = (uint_t *)malloc((fb->n_filters + 2) * sizeof(uint_t));
    239   height_norm = (smpl_t *)malloc(fb->n_filters * sizeof(smpl_t));
    240 
    241   if(mel_peak == NULL || height_norm == NULL ||
    242       lin_peak == NULL || fft_peak == NULL)
    243     return NULL;
    244 
    245   M = fb->win_s >> 1;
    246 
    247   mel_peak[0] = mel_freq_min;
    248   lin_peak[0] = 700 * (exp(mel_peak[0] / 1127) - 1);
    249   fft_peak[0] = lin_peak[0] / nyquist * M;
    250 
    251   for (n = 1; n <= fb->n_filters; n++){ 
    252     /*roll out peak locations - mel, linear and linear on fft window scale */
    253     mel_peak[n] = mel_peak[n - 1] + freq_bw_mel;
    254     lin_peak[n] = 700 * (exp(mel_peak[n] / 1127) -1);
    255     fft_peak[n] = lin_peak[n] / nyquist * M;
    256   }
    257 
    258   for (n = 0; n < fb->n_filters; n++){
    259     /*roll out normalised gain of each peak*/
    260     if (style == USE_EQUAL_GAIN){
    261       height = 1;
    262       norm_fact = norm;
    263     }
    264     else{
    265       height = 2 / (lin_peak[n + 2] - lin_peak[n]);
    266       norm_fact = norm / (2 / (lin_peak[2] - lin_peak[0]));
    267     }
    268     height_norm[n] = height * norm_fact;
    269   }
    270 
    271   i = 0;
    272 
    273   for(n = 0; n < fb->n_filters; n++){
    274 
    275     /*calculate the rise increment*/
    276     if(n > 0)
    277       inc = height_norm[n] / (fft_peak[n] - fft_peak[n - 1]);
    278     else
    279       inc = height_norm[n] / fft_peak[n];
    280     val = 0; 
    281 
    282     /*zero the start of the array*/
    283     for(k = 0; k < i; k++)
    284       fb->filters[n]->data[0][k]=0.f;
    285 
    286     /*fill in the rise */
    287     for(; i <= fft_peak[n]; i++){
    288       fb->filters[n]->data[0][k]=val;
    289       val += inc;
    290     }
    291 
    292     /*calculate the fall increment */
    293     inc = height_norm[n] / (fft_peak[n + 1] - fft_peak[n]);
    294 
    295     val = 0;
    296     next_peak = fft_peak[n + 1];
    297 
    298     /*reverse fill the 'fall' */
    299     for(i = next_peak; i > fft_peak[n]; i--){
    300       fb->filters[n]->data[0][k]=val;
    301       val += inc;
    302     }
    303 
    304     /*zero the rest of the array*/
    305     for(k = next_peak + 1; k < fb->win_s; k++)
    306       fb->filters[n]->data[0][k]=0.f;
    307 
    308 
    309   }
    310 
    311   free(mel_peak);
    312   free(lin_peak);
    313   free(height_norm);
    314   free(fft_peak);
    315 
     242      //printf(".");
     243      //printf("%f %f %f\n", fft_freqs->data[0][bin_cnt], fft_freqs->data[0][bin_cnt+1], lower_freqs->data[0][filter_cnt]);
     244      if(fft_freqs->data[0][bin_cnt]<= lower_freqs->data[0][filter_cnt] && fft_freqs->data[0][bin_cnt+1]> lower_freqs->data[0][filter_cnt]){
     245        break;
     246      }
     247    }
     248    bin_cnt++;
     249   
     250    //printf("\npos slope\n");
     251    //positive slope
     252    for(; bin_cnt<win_s-1; bin_cnt++){
     253      //printf(".");
     254      fb->filters[filter_cnt]->data[0][bin_cnt]=(fft_freqs->data[0][bin_cnt]-lower_freqs->data[0][filter_cnt])*riseInc;
     255      //if(fft_freqs->data[0][bin_cnt]<= center_freqs->data[0][filter_cnt] && fft_freqs->data[0][bin_cnt+1]> center_freqs->data[0][filter_cnt])
     256      if(fft_freqs->data[0][bin_cnt+1]> center_freqs->data[0][filter_cnt])
     257        break;
     258    }
     259    //bin_cnt++;
     260   
     261    //printf("\nneg slope\n");
     262    //negative slope
     263    for(; bin_cnt<win_s-1; bin_cnt++){
     264      //printf(".");
     265     
     266      //checking whether last value is less than 0...
     267      smpl_t val=triangle_heights->data[0][filter_cnt]-(fft_freqs->data[0][bin_cnt]-center_freqs->data[0][filter_cnt])*riseInc;
     268      if(val>=0)
     269        fb->filters[filter_cnt]->data[0][bin_cnt]=val;
     270      else fb->filters[filter_cnt]->data[0][bin_cnt]=0.f;
     271     
     272      //if(fft_freqs->data[0][bin_cnt]<= upper_freqs->data[0][bin_cnt] && fft_freqs->data[0][bin_cnt+1]> upper_freqs->data[0][filter_cnt])
     273      //TODO: CHECK whether bugfix correct
     274      if(fft_freqs->data[0][bin_cnt+1]> upper_freqs->data[0][filter_cnt])
     275        break;
     276    }
     277    //bin_cnt++;
     278   
     279    //printf("\nzero end\n");
     280    //zeroing tail
     281    for(; bin_cnt<win_s; bin_cnt++)
     282      //printf(".");
     283      fb->filters[filter_cnt]->data[0][bin_cnt]=0.f;
     284
     285  }
     286 
     287 
     288  del_fvec(freqs);
     289  //TODO: Check how to do a proper free for the following f_vec
     290
     291  //del_fvec(lower_freqs);
     292  //del_fvec(upper_freqs);
     293  //del_fvec(center_freqs);
     294  del_fvec(triangle_heights);
     295  del_fvec(fft_freqs);
     296
     297 
    316298
    317299  return fb;
Note: See TracChangeset for help on using the changeset viewer.