source: src/filterbank.c @ 5dd22a9

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchpitchshiftsamplertimestretchyinfft+
Last change on this file since 5dd22a9 was 5dd22a9, checked in by Paul Brossier <piem@piem.org>, 13 years ago

filterbank.c: avoid useless usigned comparison

  • Property mode set to 100644
File size: 7.3 KB
Line 
1/*
2   Copyright (C) 2007 Amaury Hazan <ahazan@iua.upf.edu>
3                  and Paul Brossier <piem@piem.org>
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19*/
20
21
22#include "aubio_priv.h"
23#include "sample.h"
24#include "filterbank.h"
25#include "mathutils.h"
26
27#define VERY_SMALL_NUMBER 2e-42
28
29/** \brief A structure to store a set of n_filters filters of lenghts win_s */
30struct aubio_filterbank_t_ {
31    uint_t win_s;
32    uint_t n_filters;
33    fvec_t **filters;
34};
35
36aubio_filterbank_t * new_aubio_filterbank(uint_t n_filters, uint_t win_s){
37  /** allocating space for filterbank object */
38  aubio_filterbank_t * fb = AUBIO_NEW(aubio_filterbank_t);
39  uint_t filter_cnt;
40  fb->win_s=win_s;
41  fb->n_filters=n_filters;
42
43  /** allocating filter tables */
44  fb->filters=AUBIO_ARRAY(fvec_t*,n_filters);
45  for (filter_cnt=0; filter_cnt<n_filters; filter_cnt++)
46    /* considering one-channel filters */
47    fb->filters[filter_cnt]=new_fvec(win_s, 1);
48
49  return fb;
50}
51
52/*
53FB initialization based on Slaney's auditory toolbox
54TODO:
55  *solve memory leak problems while
56  *solve quantization issues when constructing signal:
57    *bug for win_s=512
58    *corrections for win_s=1024 -> why even filters with smaller amplitude
59
60*/
61
62aubio_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){
63 
64  aubio_filterbank_t * fb = new_aubio_filterbank(n_filters, win_s);
65 
66 
67  //slaney params
68  smpl_t lowestFrequency = 133.3333;
69  smpl_t linearSpacing = 66.66666666;
70  smpl_t logSpacing = 1.0711703;
71
72  uint_t linearFilters = 13;
73  uint_t logFilters = 27;
74  uint_t allFilters = linearFilters + logFilters;
75 
76  //buffers for computing filter frequencies
77  fvec_t * freqs=new_fvec(allFilters+2 , 1);
78 
79  fvec_t * lower_freqs=new_fvec( allFilters, 1);
80  fvec_t * upper_freqs=new_fvec( allFilters, 1);
81  fvec_t * center_freqs=new_fvec( allFilters, 1);
82
83  fvec_t * triangle_heights=new_fvec( allFilters, 1);
84  //lookup table of each bin frequency in hz
85  fvec_t * fft_freqs=new_fvec(win_s, 1);
86
87  uint_t filter_cnt, bin_cnt;
88 
89  //first step: filling all the linear filter frequencies
90  for(filter_cnt=0; filter_cnt<linearFilters; filter_cnt++){
91    freqs->data[0][filter_cnt]=lowestFrequency+ filter_cnt*linearSpacing;
92  }
93  smpl_t lastlinearCF=freqs->data[0][filter_cnt-1];
94 
95  //second step: filling all the log filter frequencies
96  for(filter_cnt=0; filter_cnt<logFilters+2; filter_cnt++){
97    freqs->data[0][filter_cnt+linearFilters] = 
98      lastlinearCF*(pow(logSpacing,filter_cnt+1));
99  }
100
101  //Option 1. copying interesting values to lower_freqs, center_freqs and upper freqs arrays
102  //TODO: would be nicer to have a reference to freqs->data, anyway we do not care in this init step
103   
104  for(filter_cnt=0; filter_cnt<allFilters; filter_cnt++){
105    lower_freqs->data[0][filter_cnt]=freqs->data[0][filter_cnt];
106    center_freqs->data[0][filter_cnt]=freqs->data[0][filter_cnt+1];
107    upper_freqs->data[0][filter_cnt]=freqs->data[0][filter_cnt+2];
108  }
109
110  //computing triangle heights so that each triangle has unit area
111  for(filter_cnt=0; filter_cnt<allFilters; filter_cnt++){
112    triangle_heights->data[0][filter_cnt] = 2./(upper_freqs->data[0][filter_cnt] 
113      - lower_freqs->data[0][filter_cnt]);
114  }
115 
116  //AUBIO_DBG("filter tables frequencies\n");
117  //for(filter_cnt=0; filter_cnt<allFilters; filter_cnt++)
118  //  AUBIO_DBG("filter n. %d %f %f %f %f\n",
119  //    filter_cnt, lower_freqs->data[0][filter_cnt],
120  //    center_freqs->data[0][filter_cnt], upper_freqs->data[0][filter_cnt],
121  //    triangle_heights->data[0][filter_cnt]);
122
123  //filling the fft_freqs lookup table, which assigns the frequency in hz to each bin
124  for(bin_cnt=0; bin_cnt<win_s; bin_cnt++){
125    fft_freqs->data[0][bin_cnt]= aubio_bintofreq(bin_cnt, samplerate, win_s);
126  }
127
128  //building each filter table
129  for(filter_cnt=0; filter_cnt<allFilters; filter_cnt++){
130
131    //TODO:check special case : lower freq =0
132    //calculating rise increment in mag/Hz
133    smpl_t riseInc= triangle_heights->data[0][filter_cnt]/(center_freqs->data[0][filter_cnt]-lower_freqs->data[0][filter_cnt]);
134   
135    //zeroing begining of filter
136    for(bin_cnt=0; bin_cnt<win_s-1; bin_cnt++){
137      fb->filters[filter_cnt]->data[0][bin_cnt]=0.f;
138      if( fft_freqs->data[0][bin_cnt]  <= lower_freqs->data[0][filter_cnt] &&
139          fft_freqs->data[0][bin_cnt+1] > lower_freqs->data[0][filter_cnt]) {
140        break;
141      }
142    }
143    bin_cnt++;
144   
145    //positive slope
146    for(; bin_cnt<win_s-1; bin_cnt++){
147      fb->filters[filter_cnt]->data[0][bin_cnt]=(fft_freqs->data[0][bin_cnt]-lower_freqs->data[0][filter_cnt])*riseInc;
148      //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])
149      if(fft_freqs->data[0][bin_cnt+1]> center_freqs->data[0][filter_cnt])
150        break;
151    }
152    //bin_cnt++;
153   
154    //negative slope
155    for(; bin_cnt<win_s-1; bin_cnt++){
156     
157      //checking whether last value is less than 0...
158      smpl_t val=triangle_heights->data[0][filter_cnt]-(fft_freqs->data[0][bin_cnt]-center_freqs->data[0][filter_cnt])*riseInc;
159      if(val>=0)
160        fb->filters[filter_cnt]->data[0][bin_cnt]=val;
161      else fb->filters[filter_cnt]->data[0][bin_cnt]=0.f;
162     
163      //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])
164      //TODO: CHECK whether bugfix correct
165      if(fft_freqs->data[0][bin_cnt+1]> upper_freqs->data[0][filter_cnt])
166        break;
167    }
168    //bin_cnt++;
169   
170    //zeroing tail
171    for(; bin_cnt<win_s; bin_cnt++)
172      fb->filters[filter_cnt]->data[0][bin_cnt]=0.f;
173
174  }
175 
176 
177  del_fvec(freqs);
178  del_fvec(lower_freqs);
179  del_fvec(upper_freqs);
180  del_fvec(center_freqs);
181
182  del_fvec(triangle_heights);
183  del_fvec(fft_freqs);
184
185  return fb;
186
187}
188
189void del_aubio_filterbank(aubio_filterbank_t * fb){
190  uint_t filter_cnt;
191  /** deleting filter tables first */
192  for (filter_cnt=0; filter_cnt<fb->n_filters; filter_cnt++)
193    del_fvec(fb->filters[filter_cnt]);
194  AUBIO_FREE(fb->filters);
195  AUBIO_FREE(fb);
196}
197
198void aubio_filterbank_do(aubio_filterbank_t * f, cvec_t * in, fvec_t *out) {
199  uint_t n, filter_cnt;
200  for(filter_cnt = 0; (filter_cnt < f->n_filters)
201    && (filter_cnt < out->length); filter_cnt++){
202      out->data[0][filter_cnt] = 0.f;
203      for(n = 0; n < in->length; n++){
204          out->data[0][filter_cnt] += in->norm[0][n] 
205            * f->filters[filter_cnt]->data[0][n];
206      }
207      out->data[0][filter_cnt] =
208        LOG(out->data[0][filter_cnt] < VERY_SMALL_NUMBER ? 
209            VERY_SMALL_NUMBER : out->data[0][filter_cnt]);
210  }
211
212  return;
213}
214
215fvec_t * aubio_filterbank_getchannel(aubio_filterbank_t * f, uint_t channel) {
216  if ( (channel < f->n_filters) ) { return f->filters[channel]; }
217  else { return NULL; }
218}
Note: See TracBrowser for help on using the repository browser.