source: src/spectral/filterbank.c @ 6c7d49b

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

remove src/sample.h

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