source: src/spectral/filterbank_mel.c @ fa713bd

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretch
Last change on this file since fa713bd was fa713bd, checked in by Paul Brossier <piem@piem.org>, 23 months ago

[filterbank] add set_mel_coeffs

  • Property mode set to 100644
File size: 8.4 KB
Line 
1/*
2  Copyright (C) 2007-2009 Paul Brossier <piem@aubio.org>
3                      and Amaury Hazan <ahazan@iua.upf.edu>
4
5  This file is part of aubio.
6
7  aubio is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11
12  aubio is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  GNU General Public License for more details.
16
17  You should have received a copy of the GNU General Public License
18  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
19
20*/
21
22#include "aubio_priv.h"
23#include "fmat.h"
24#include "fvec.h"
25#include "cvec.h"
26#include "spectral/filterbank.h"
27#include "spectral/filterbank_mel.h"
28#include "mathutils.h"
29
30uint_t
31aubio_filterbank_set_triangle_bands (aubio_filterbank_t * fb,
32    const fvec_t * freqs, smpl_t samplerate)
33{
34
35  fmat_t *filters = aubio_filterbank_get_coeffs (fb);
36  uint_t n_filters = filters->height, win_s = filters->length;
37  fvec_t *lower_freqs, *upper_freqs, *center_freqs;
38  fvec_t *triangle_heights, *fft_freqs;
39
40  uint_t fn;                    /* filter counter */
41  uint_t bin;                   /* bin counter */
42
43  smpl_t riseInc, downInc;
44
45  /* freqs define the bands of triangular overlapping windows.
46     throw a warning if filterbank object fb is too short. */
47  if (freqs->length - 2 > n_filters) {
48    AUBIO_WRN ("not enough filters, %d allocated but %d requested\n",
49        n_filters, freqs->length - 2);
50  }
51
52  if (freqs->length - 2 < n_filters) {
53    AUBIO_WRN ("too many filters, %d allocated but %d requested\n",
54        n_filters, freqs->length - 2);
55  }
56
57  for (fn = 0; fn < freqs->length; fn++) {
58    if (freqs->data[fn] < 0) {
59      AUBIO_ERR("filterbank_mel: freqs must contain only positive values.\n");
60      return AUBIO_FAIL;
61    } else if (freqs->data[fn] > samplerate / 2) {
62      AUBIO_WRN("filterbank_mel: freqs should contain only "
63          "values < samplerate / 2.\n");
64    } else if (fn > 0 && freqs->data[fn] < freqs->data[fn-1]) {
65      AUBIO_ERR("filterbank_mel: freqs should be a list of frequencies "
66          "sorted from low to high, but freq[%d] < freq[%d-1]\n", fn, fn);
67      return AUBIO_FAIL;
68    } else if (fn > 0 && freqs->data[fn] == freqs->data[fn-1]) {
69      AUBIO_WRN("filterbank_mel: set_triangle_bands received a list "
70          "with twice the frequency %f\n", freqs->data[fn]);
71    }
72  }
73
74  /* convenience reference to lower/center/upper frequency for each triangle */
75  lower_freqs = new_fvec (n_filters);
76  upper_freqs = new_fvec (n_filters);
77  center_freqs = new_fvec (n_filters);
78
79  /* height of each triangle */
80  triangle_heights = new_fvec (n_filters);
81
82  /* lookup table of each bin frequency in hz */
83  fft_freqs = new_fvec (win_s);
84
85  /* fill up the lower/center/upper */
86  for (fn = 0; fn < n_filters; fn++) {
87    lower_freqs->data[fn] = freqs->data[fn];
88    center_freqs->data[fn] = freqs->data[fn + 1];
89    upper_freqs->data[fn] = freqs->data[fn + 2];
90  }
91
92  /* compute triangle heights so that each triangle has unit area */
93  for (fn = 0; fn < n_filters; fn++) {
94    triangle_heights->data[fn] =
95        2. / (upper_freqs->data[fn] - lower_freqs->data[fn]);
96  }
97
98  /* fill fft_freqs lookup table, which assigns the frequency in hz to each bin */
99  for (bin = 0; bin < win_s; bin++) {
100    fft_freqs->data[bin] =
101        aubio_bintofreq (bin, samplerate, (win_s - 1) * 2);
102  }
103
104  /* zeroing of all filters */
105  fmat_zeros (filters);
106
107  /* building each filter table */
108  for (fn = 0; fn < n_filters; fn++) {
109
110    /* skip first elements */
111    for (bin = 0; bin < win_s - 1; bin++) {
112      if (fft_freqs->data[bin] <= lower_freqs->data[fn] &&
113          fft_freqs->data[bin + 1] > lower_freqs->data[fn]) {
114        bin++;
115        break;
116      }
117    }
118
119    /* compute positive slope step size */
120    riseInc =
121        triangle_heights->data[fn] /
122        (center_freqs->data[fn] - lower_freqs->data[fn]);
123
124    /* compute coefficients in positive slope */
125    for (; bin < win_s - 1; bin++) {
126      filters->data[fn][bin] =
127          (fft_freqs->data[bin] - lower_freqs->data[fn]) * riseInc;
128
129      if (fft_freqs->data[bin + 1] >= center_freqs->data[fn]) {
130        bin++;
131        break;
132      }
133    }
134
135    /* compute negative slope step size */
136    downInc =
137        triangle_heights->data[fn] /
138        (upper_freqs->data[fn] - center_freqs->data[fn]);
139
140    /* compute coefficents in negative slope */
141    for (; bin < win_s - 1; bin++) {
142      filters->data[fn][bin] +=
143          (upper_freqs->data[fn] - fft_freqs->data[bin]) * downInc;
144
145      if (filters->data[fn][bin] < 0.) {
146        filters->data[fn][bin] = 0.;
147      }
148
149      if (fft_freqs->data[bin + 1] >= upper_freqs->data[fn])
150        break;
151    }
152    /* nothing else to do */
153
154  }
155
156  /* destroy temporarly allocated vectors */
157  del_fvec (lower_freqs);
158  del_fvec (upper_freqs);
159  del_fvec (center_freqs);
160
161  del_fvec (triangle_heights);
162  del_fvec (fft_freqs);
163
164  return AUBIO_OK;
165}
166
167uint_t
168aubio_filterbank_set_mel_coeffs_slaney (aubio_filterbank_t * fb,
169    smpl_t samplerate)
170{
171  uint_t retval;
172
173  /* Malcolm Slaney parameters */
174  smpl_t lowestFrequency = 133.3333;
175  smpl_t linearSpacing = 66.66666666;
176  smpl_t logSpacing = 1.0711703;
177
178  uint_t linearFilters = 13;
179  uint_t logFilters = 27;
180  uint_t n_filters = linearFilters + logFilters;
181
182  uint_t fn;                    /* filter counter */
183
184  smpl_t lastlinearCF;
185
186  /* buffers to compute filter frequencies */
187  fvec_t *freqs = new_fvec (n_filters + 2);
188
189  /* first step: fill all the linear filter frequencies */
190  for (fn = 0; fn < linearFilters; fn++) {
191    freqs->data[fn] = lowestFrequency + fn * linearSpacing;
192  }
193  lastlinearCF = freqs->data[fn - 1];
194
195  /* second step: fill all the log filter frequencies */
196  for (fn = 0; fn < logFilters + 2; fn++) {
197    freqs->data[fn + linearFilters] =
198        lastlinearCF * (POW (logSpacing, fn + 1));
199  }
200
201  /* now compute the actual coefficients */
202  retval = aubio_filterbank_set_triangle_bands (fb, freqs, samplerate);
203
204  /* destroy vector used to store frequency limits */
205  del_fvec (freqs);
206
207  return retval;
208}
209
210uint_t
211aubio_filterbank_set_mel_coeffs (aubio_filterbank_t * fb, smpl_t samplerate,
212    smpl_t freq_min, smpl_t freq_max)
213{
214  uint_t m, retval;
215  smpl_t start, end, step;
216  fvec_t *freqs;
217  fmat_t *coeffs = aubio_filterbank_get_coeffs(fb);
218  uint_t n_bands = coeffs->height;
219
220  if (freq_max < 0) {
221    AUBIO_ERR("filterbank: set_mel_coeffs freq_max should be > 0\n");
222    return AUBIO_FAIL;
223  } else if (freq_max == 0) {
224    end = aubio_hztomel(samplerate / 2.);
225  } else {
226    end = aubio_hztomel(freq_max);
227  }
228  if (freq_min < 0) {
229    AUBIO_ERR("filterbank: set_mel_coeffs freq_min should be > 0\n");
230    return AUBIO_FAIL;
231  } else {
232    start = aubio_hztomel(freq_min);
233  }
234  if (n_bands <= 0) {
235    AUBIO_ERR("filterbank: set_mel_coeffs n_bands should be > 0\n");
236    return AUBIO_FAIL;
237  }
238
239  freqs = new_fvec(n_bands + 2);
240  step = (end - start) / (n_bands + 1);
241
242  for (m = 0; m < n_bands + 2; m++)
243  {
244    freqs->data[m] = MIN(aubio_meltohz(start + step * m), samplerate/2.);
245  }
246
247  retval = aubio_filterbank_set_triangle_bands (fb, freqs, samplerate);
248
249  /* destroy vector used to store frequency limits */
250  del_fvec (freqs);
251  return retval;
252}
253
254uint_t
255aubio_filterbank_set_mel_coeffs_htk (aubio_filterbank_t * fb, smpl_t samplerate,
256    smpl_t freq_min, smpl_t freq_max)
257{
258  uint_t m, retval;
259  smpl_t start, end, step;
260  fvec_t *freqs;
261  fmat_t *coeffs = aubio_filterbank_get_coeffs(fb);
262  uint_t n_bands = coeffs->height;
263
264  if (freq_max < 0) {
265    AUBIO_ERR("filterbank: set_mel_coeffs freq_max should be > 0\n");
266    return AUBIO_FAIL;
267  } else if (freq_max == 0) {
268    end = aubio_hztomel_htk(samplerate / 2.);
269  } else {
270    end = aubio_hztomel_htk(freq_max);
271  }
272  if (freq_min < 0) {
273    AUBIO_ERR("filterbank: set_mel_coeffs freq_min should be > 0\n");
274    return AUBIO_FAIL;
275  } else {
276    start = aubio_hztomel_htk(freq_min);
277  }
278  if (n_bands <= 0) {
279    AUBIO_ERR("filterbank: set_mel_coeffs n_bands should be > 0\n");
280    return AUBIO_FAIL;
281  }
282
283  freqs = new_fvec (n_bands + 2);
284  step = (end - start) / (n_bands + 1);
285
286  for (m = 0; m < n_bands + 2; m++)
287  {
288    freqs->data[m] = MIN(aubio_meltohz_htk(step * m), samplerate/2.);
289  }
290
291  retval = aubio_filterbank_set_triangle_bands (fb, freqs, samplerate);
292
293  /* destroy vector used to store frequency limits */
294  del_fvec (freqs);
295  return retval;
296}
Note: See TracBrowser for help on using the repository browser.