source: src/spectral/filterbank_mel.c @ c5de692

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since c5de692 was c5de692, checked in by Paul Brossier <piem@piem.org>, 5 years ago

[filterbank] add const qualifiers

  • Property mode set to 100644
File size: 8.5 KB
RevLine 
[06cae6c]1/*
2  Copyright (C) 2007-2009 Paul Brossier <piem@aubio.org>
3                      and Amaury Hazan <ahazan@iua.upf.edu>
4
[1c2e186]5  This file is part of aubio.
[06cae6c]6
[1c2e186]7  aubio is free software: you can redistribute it and/or modify
[06cae6c]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
[1c2e186]12  aubio is distributed in the hope that it will be useful,
[06cae6c]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
[1c2e186]18  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
[06cae6c]19
20*/
21
22#include "aubio_priv.h"
[d95ff38]23#include "fmat.h"
[06cae6c]24#include "fvec.h"
25#include "cvec.h"
26#include "spectral/filterbank.h"
[db354e0]27#include "spectral/filterbank_mel.h"
[06cae6c]28#include "mathutils.h"
29
[3c18f9e]30uint_t
31aubio_filterbank_set_triangle_bands (aubio_filterbank_t * fb,
[feb694b]32    const fvec_t * freqs, smpl_t samplerate)
[cfd35db]33{
[06cae6c]34
[d95ff38]35  fmat_t *filters = aubio_filterbank_get_coeffs (fb);
36  uint_t n_filters = filters->height, win_s = filters->length;
[ddf04fd]37  fvec_t *lower_freqs, *upper_freqs, *center_freqs;
38  fvec_t *triangle_heights, *fft_freqs;
[cfd35db]39
[1e37ade]40  uint_t fn;                    /* filter counter */
41  uint_t bin;                   /* bin counter */
[cfd35db]42
[ddf04fd]43  smpl_t riseInc, downInc;
44
[1e37ade]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);
[b507607]50  }
51
[3c18f9e]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
[edd3dac]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 "
[9ef3c6e]63          "values < samplerate / 2.\n");
[edd3dac]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;
[9ef3c6e]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]);
[edd3dac]71    }
[3c18f9e]72  }
73
[4e0fbe6]74  /* convenience reference to lower/center/upper frequency for each triangle */
[ddf04fd]75  lower_freqs = new_fvec (n_filters);
76  upper_freqs = new_fvec (n_filters);
77  center_freqs = new_fvec (n_filters);
[cfd35db]78
[4e0fbe6]79  /* height of each triangle */
[ddf04fd]80  triangle_heights = new_fvec (n_filters);
[4e0fbe6]81
82  /* lookup table of each bin frequency in hz */
[ddf04fd]83  fft_freqs = new_fvec (win_s);
[06cae6c]84
[4e0fbe6]85  /* fill up the lower/center/upper */
[1e37ade]86  for (fn = 0; fn < n_filters; fn++) {
[d95ff38]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];
[06cae6c]90  }
91
[4e0fbe6]92  /* compute triangle heights so that each triangle has unit area */
[831f702]93  if (aubio_filterbank_get_norm(fb)) {
94    for (fn = 0; fn < n_filters; fn++) {
95      triangle_heights->data[fn] =
96          2. / (upper_freqs->data[fn] - lower_freqs->data[fn]);
97    }
98  } else {
99    fvec_ones (triangle_heights);
[06cae6c]100  }
[cfd35db]101
[4e0fbe6]102  /* fill fft_freqs lookup table, which assigns the frequency in hz to each bin */
103  for (bin = 0; bin < win_s; bin++) {
[d95ff38]104    fft_freqs->data[bin] =
[3c18f9e]105        aubio_bintofreq (bin, samplerate, (win_s - 1) * 2);
[06cae6c]106  }
107
[4e0fbe6]108  /* zeroing of all filters */
[d95ff38]109  fmat_zeros (filters);
[b507607]110
111  /* building each filter table */
[4e0fbe6]112  for (fn = 0; fn < n_filters; fn++) {
[cfd35db]113
[b507607]114    /* skip first elements */
[4e0fbe6]115    for (bin = 0; bin < win_s - 1; bin++) {
[d95ff38]116      if (fft_freqs->data[bin] <= lower_freqs->data[fn] &&
117          fft_freqs->data[bin + 1] > lower_freqs->data[fn]) {
[3c18f9e]118        bin++;
[06cae6c]119        break;
120      }
121    }
[cfd35db]122
[b507607]123    /* compute positive slope step size */
[831f702]124    riseInc = triangle_heights->data[fn]
125      / (center_freqs->data[fn] - lower_freqs->data[fn]);
[b507607]126
127    /* compute coefficients in positive slope */
[4e0fbe6]128    for (; bin < win_s - 1; bin++) {
129      filters->data[fn][bin] =
[d95ff38]130          (fft_freqs->data[bin] - lower_freqs->data[fn]) * riseInc;
[4e0fbe6]131
[d95ff38]132      if (fft_freqs->data[bin + 1] >= center_freqs->data[fn]) {
[3c18f9e]133        bin++;
[06cae6c]134        break;
[3c18f9e]135      }
[06cae6c]136    }
[b507607]137
138    /* compute negative slope step size */
[831f702]139    downInc = triangle_heights->data[fn]
140      / (upper_freqs->data[fn] - center_freqs->data[fn]);
[cfd35db]141
[b507607]142    /* compute coefficents in negative slope */
[4e0fbe6]143    for (; bin < win_s - 1; bin++) {
144      filters->data[fn][bin] +=
[d95ff38]145          (upper_freqs->data[fn] - fft_freqs->data[bin]) * downInc;
[cfd35db]146
[3c18f9e]147      if (filters->data[fn][bin] < 0.) {
148        filters->data[fn][bin] = 0.;
149      }
150
[d95ff38]151      if (fft_freqs->data[bin + 1] >= upper_freqs->data[fn])
[06cae6c]152        break;
153    }
[b507607]154    /* nothing else to do */
[06cae6c]155
156  }
157
[cfd35db]158  /* destroy temporarly allocated vectors */
159  del_fvec (lower_freqs);
160  del_fvec (upper_freqs);
161  del_fvec (center_freqs);
[06cae6c]162
[cfd35db]163  del_fvec (triangle_heights);
164  del_fvec (fft_freqs);
[06cae6c]165
[edd3dac]166  return AUBIO_OK;
[cfd35db]167}
[1e37ade]168
[3c18f9e]169uint_t
[1e37ade]170aubio_filterbank_set_mel_coeffs_slaney (aubio_filterbank_t * fb,
171    smpl_t samplerate)
172{
173  /* Malcolm Slaney parameters */
[c5de692]174  const smpl_t lowestFrequency = 133.3333;
175  const smpl_t linearSpacing = 66.66666666;
176  const smpl_t logSpacing = 1.0711703;
[1e37ade]177
[c5de692]178  const uint_t linearFilters = 13;
179  const uint_t logFilters = 27;
180  const uint_t n_filters = linearFilters + logFilters;
[1e37ade]181
[c5de692]182  uint_t fn, retval;
[ddf04fd]183  smpl_t lastlinearCF;
184
[1e37ade]185  /* buffers to compute filter frequencies */
[a5c6182]186  fvec_t *freqs;
187
188  if (samplerate <= 0) {
189    AUBIO_ERR("filterbank: set_mel_coeffs_slaney samplerate should be > 0\n");
190    return AUBIO_FAIL;
191  }
192
193  freqs = new_fvec (n_filters + 2);
[1e37ade]194
195  /* first step: fill all the linear filter frequencies */
196  for (fn = 0; fn < linearFilters; fn++) {
[d95ff38]197    freqs->data[fn] = lowestFrequency + fn * linearSpacing;
[1e37ade]198  }
[ddf04fd]199  lastlinearCF = freqs->data[fn - 1];
[1e37ade]200
201  /* second step: fill all the log filter frequencies */
202  for (fn = 0; fn < logFilters + 2; fn++) {
[d95ff38]203    freqs->data[fn + linearFilters] =
[1e37ade]204        lastlinearCF * (POW (logSpacing, fn + 1));
205  }
206
207  /* now compute the actual coefficients */
[59c046d]208  retval = aubio_filterbank_set_triangle_bands (fb, freqs, samplerate);
[1e37ade]209
210  /* destroy vector used to store frequency limits */
211  del_fvec (freqs);
212
[3c18f9e]213  return retval;
[1e37ade]214}
[fa713bd]215
[6d41dac]216static uint_t aubio_filterbank_check_freqs (aubio_filterbank_t *fb UNUSED,
217    smpl_t samplerate, smpl_t *freq_min, smpl_t *freq_max)
[fa713bd]218{
[0316feb]219  if (samplerate <= 0) {
220    AUBIO_ERR("filterbank: set_mel_coeffs samplerate should be > 0\n");
221    return AUBIO_FAIL;
222  }
[6d41dac]223  if (*freq_max < 0) {
[fa713bd]224    AUBIO_ERR("filterbank: set_mel_coeffs freq_max should be > 0\n");
225    return AUBIO_FAIL;
[6d41dac]226  } else if (*freq_max == 0) {
227    *freq_max = samplerate / 2.;
[fa713bd]228  }
[6d41dac]229  if (*freq_min < 0) {
[fa713bd]230    AUBIO_ERR("filterbank: set_mel_coeffs freq_min should be > 0\n");
231    return AUBIO_FAIL;
232  }
[6d41dac]233  return AUBIO_OK;
234}
235
236uint_t
237aubio_filterbank_set_mel_coeffs (aubio_filterbank_t * fb, smpl_t samplerate,
238    smpl_t freq_min, smpl_t freq_max)
239{
240  uint_t m, retval;
241  smpl_t start = freq_min, end = freq_max, step;
242  fvec_t *freqs;
243  fmat_t *coeffs = aubio_filterbank_get_coeffs(fb);
244  uint_t n_bands = coeffs->height;
245
[69dbe0a]246  if (aubio_filterbank_check_freqs(fb, samplerate, &start, &end)) {
[6d41dac]247    return AUBIO_FAIL;
248  }
249
250  start = aubio_hztomel(start);
251  end = aubio_hztomel(end);
[fa713bd]252
253  freqs = new_fvec(n_bands + 2);
254  step = (end - start) / (n_bands + 1);
255
256  for (m = 0; m < n_bands + 2; m++)
257  {
258    freqs->data[m] = MIN(aubio_meltohz(start + step * m), samplerate/2.);
259  }
260
261  retval = aubio_filterbank_set_triangle_bands (fb, freqs, samplerate);
262
263  /* destroy vector used to store frequency limits */
264  del_fvec (freqs);
265  return retval;
266}
267
268uint_t
269aubio_filterbank_set_mel_coeffs_htk (aubio_filterbank_t * fb, smpl_t samplerate,
270    smpl_t freq_min, smpl_t freq_max)
271{
272  uint_t m, retval;
[6d41dac]273  smpl_t start = freq_min, end = freq_max, step;
[fa713bd]274  fvec_t *freqs;
275  fmat_t *coeffs = aubio_filterbank_get_coeffs(fb);
276  uint_t n_bands = coeffs->height;
277
[69dbe0a]278  if (aubio_filterbank_check_freqs(fb, samplerate, &start, &end)) {
[0316feb]279    return AUBIO_FAIL;
280  }
[6d41dac]281
282  start = aubio_hztomel_htk(start);
283  end = aubio_hztomel_htk(end);
[fa713bd]284
285  freqs = new_fvec (n_bands + 2);
286  step = (end - start) / (n_bands + 1);
287
288  for (m = 0; m < n_bands + 2; m++)
289  {
[1d51820]290    freqs->data[m] = MIN(aubio_meltohz_htk(start + step * m), samplerate/2.);
[fa713bd]291  }
292
293  retval = aubio_filterbank_set_triangle_bands (fb, freqs, samplerate);
294
295  /* destroy vector used to store frequency limits */
296  del_fvec (freqs);
297  return retval;
298}
Note: See TracBrowser for help on using the repository browser.