[88199ce] | 1 | /* |
---|
[f918cb9] | 2 | Copyright (C) 2007 Amaury Hazan <ahazan@iua.upf.edu> |
---|
| 3 | and Paul Brossier <piem@piem.org> |
---|
[88199ce] | 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 | |
---|
[f918cb9] | 21 | |
---|
[7c6c806d] | 22 | #include "aubio_priv.h" |
---|
[6c7d49b] | 23 | #include "fvec.h" |
---|
| 24 | #include "cvec.h" |
---|
[32d6958] | 25 | #include "spectral/filterbank.h" |
---|
[17961b0] | 26 | #include "mathutils.h" |
---|
[88199ce] | 27 | |
---|
[b276dee] | 28 | #define VERY_SMALL_NUMBER 2e-42 |
---|
[7c6c806d] | 29 | |
---|
[8708556] | 30 | /** \brief A structure to store a set of n_filters filters of lenghts win_s */ |
---|
| 31 | struct aubio_filterbank_t_ { |
---|
| 32 | uint_t win_s; |
---|
| 33 | uint_t n_filters; |
---|
[b276dee] | 34 | fvec_t **filters; |
---|
[7c6c806d] | 35 | }; |
---|
| 36 | |
---|
[8708556] | 37 | aubio_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); |
---|
[f918cb9] | 40 | uint_t filter_cnt; |
---|
[8708556] | 41 | fb->win_s=win_s; |
---|
| 42 | fb->n_filters=n_filters; |
---|
[88199ce] | 43 | |
---|
[8708556] | 44 | /** allocating filter tables */ |
---|
[b276dee] | 45 | fb->filters=AUBIO_ARRAY(fvec_t*,n_filters); |
---|
[8708556] | 46 | for (filter_cnt=0; filter_cnt<n_filters; filter_cnt++) |
---|
| 47 | /* considering one-channel filters */ |
---|
[b276dee] | 48 | fb->filters[filter_cnt]=new_fvec(win_s, 1); |
---|
[88199ce] | 49 | |
---|
[b276dee] | 50 | return fb; |
---|
| 51 | } |
---|
| 52 | |
---|
[83d5abf] | 53 | /* |
---|
| 54 | FB initialization based on Slaney's auditory toolbox |
---|
| 55 | TODO: |
---|
| 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 | */ |
---|
[ef1c3b7] | 62 | |
---|
[aa83433] | 63 | aubio_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){ |
---|
[f72ceeb] | 64 | |
---|
| 65 | aubio_filterbank_t * fb = new_aubio_filterbank(n_filters, win_s); |
---|
| 66 | |
---|
[ef1c3b7] | 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; |
---|
[7a46bf6] | 76 | |
---|
[ef1c3b7] | 77 | //buffers for computing filter frequencies |
---|
[95a64c7] | 78 | fvec_t * freqs=new_fvec(allFilters+2 , 1); |
---|
[787f1f3] | 79 | |
---|
[ef1c3b7] | 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); |
---|
[95a64c7] | 83 | |
---|
[ef1c3b7] | 84 | fvec_t * triangle_heights=new_fvec( allFilters, 1); |
---|
| 85 | //lookup table of each bin frequency in hz |
---|
[f72ceeb] | 86 | fvec_t * fft_freqs=new_fvec(win_s, 1); |
---|
[ef1c3b7] | 87 | |
---|
| 88 | uint_t filter_cnt, bin_cnt; |
---|
| 89 | |
---|
[95a64c7] | 90 | //first step: filling all the linear filter frequencies |
---|
[ef1c3b7] | 91 | for(filter_cnt=0; filter_cnt<linearFilters; filter_cnt++){ |
---|
[f72ceeb] | 92 | freqs->data[0][filter_cnt]=lowestFrequency+ filter_cnt*linearSpacing; |
---|
[ef1c3b7] | 93 | } |
---|
[f72ceeb] | 94 | smpl_t lastlinearCF=freqs->data[0][filter_cnt-1]; |
---|
[ef1c3b7] | 95 | |
---|
[95a64c7] | 96 | //second step: filling all the log filter frequencies |
---|
[ef1c3b7] | 97 | for(filter_cnt=0; filter_cnt<logFilters+2; filter_cnt++){ |
---|
[aa83433] | 98 | freqs->data[0][filter_cnt+linearFilters] = |
---|
| 99 | lastlinearCF*(pow(logSpacing,filter_cnt+1)); |
---|
[ef1c3b7] | 100 | } |
---|
[f72ceeb] | 101 | |
---|
[95a64c7] | 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 | |
---|
[ef1c3b7] | 111 | //computing triangle heights so that each triangle has unit area |
---|
| 112 | for(filter_cnt=0; filter_cnt<allFilters; filter_cnt++){ |
---|
[aa83433] | 113 | triangle_heights->data[0][filter_cnt] = 2./(upper_freqs->data[0][filter_cnt] |
---|
| 114 | - lower_freqs->data[0][filter_cnt]); |
---|
[ef1c3b7] | 115 | } |
---|
[95a64c7] | 116 | |
---|
[aa83433] | 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]); |
---|
[bc4ba75] | 123 | |
---|
[f72ceeb] | 124 | //filling the fft_freqs lookup table, which assigns the frequency in hz to each bin |
---|
[ef1c3b7] | 125 | for(bin_cnt=0; bin_cnt<win_s; bin_cnt++){ |
---|
[17961b0] | 126 | fft_freqs->data[0][bin_cnt]= aubio_bintofreq(bin_cnt, samplerate, win_s); |
---|
[ef1c3b7] | 127 | } |
---|
[bc4ba75] | 128 | |
---|
[f72ceeb] | 129 | //building each filter table |
---|
[ef1c3b7] | 130 | for(filter_cnt=0; filter_cnt<allFilters; filter_cnt++){ |
---|
| 131 | |
---|
[f72ceeb] | 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 | |
---|
[bc4ba75] | 136 | //zeroing begining of filter |
---|
[f72ceeb] | 137 | for(bin_cnt=0; bin_cnt<win_s-1; bin_cnt++){ |
---|
[ef1c3b7] | 138 | fb->filters[filter_cnt]->data[0][bin_cnt]=0.f; |
---|
[aa83433] | 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]) { |
---|
[f72ceeb] | 141 | break; |
---|
| 142 | } |
---|
[88199ce] | 143 | } |
---|
[f72ceeb] | 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; |
---|
[b276dee] | 152 | } |
---|
[f72ceeb] | 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; |
---|
[b276dee] | 168 | } |
---|
[f72ceeb] | 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; |
---|
[88199ce] | 174 | |
---|
[b276dee] | 175 | } |
---|
[f72ceeb] | 176 | |
---|
| 177 | |
---|
| 178 | del_fvec(freqs); |
---|
[95a64c7] | 179 | del_fvec(lower_freqs); |
---|
| 180 | del_fvec(upper_freqs); |
---|
| 181 | del_fvec(center_freqs); |
---|
[b276dee] | 182 | |
---|
[f72ceeb] | 183 | del_fvec(triangle_heights); |
---|
| 184 | del_fvec(fft_freqs); |
---|
[b276dee] | 185 | |
---|
| 186 | return fb; |
---|
[88199ce] | 187 | |
---|
[8708556] | 188 | } |
---|
| 189 | |
---|
| 190 | void del_aubio_filterbank(aubio_filterbank_t * fb){ |
---|
[b276dee] | 191 | uint_t filter_cnt; |
---|
[8708556] | 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 | } |
---|
[88199ce] | 198 | |
---|
[b276dee] | 199 | void aubio_filterbank_do(aubio_filterbank_t * f, cvec_t * in, fvec_t *out) { |
---|
| 200 | uint_t n, filter_cnt; |
---|
[f14a78d] | 201 | for(filter_cnt = 0; (filter_cnt < f->n_filters) |
---|
| 202 | && (filter_cnt < out->length); filter_cnt++){ |
---|
[b276dee] | 203 | out->data[0][filter_cnt] = 0.f; |
---|
[f14a78d] | 204 | for(n = 0; n < in->length; n++){ |
---|
[b276dee] | 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; |
---|
[fe28ff3] | 214 | } |
---|
[17961b0] | 215 | |
---|
| 216 | fvec_t * aubio_filterbank_getchannel(aubio_filterbank_t * f, uint_t channel) { |
---|
[5dd22a9] | 217 | if ( (channel < f->n_filters) ) { return f->filters[channel]; } |
---|
[17961b0] | 218 | else { return NULL; } |
---|
| 219 | } |
---|