source: src/spectral/filterbank_mel.c @ 693de50

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

[filterbank] remove warning when list starts with 0, add some sanity checks

  • Property mode set to 100644
File size: 6.0 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    }
69  }
70
71  /* convenience reference to lower/center/upper frequency for each triangle */
72  lower_freqs = new_fvec (n_filters);
73  upper_freqs = new_fvec (n_filters);
74  center_freqs = new_fvec (n_filters);
75
76  /* height of each triangle */
77  triangle_heights = new_fvec (n_filters);
78
79  /* lookup table of each bin frequency in hz */
80  fft_freqs = new_fvec (win_s);
81
82  /* fill up the lower/center/upper */
83  for (fn = 0; fn < n_filters; fn++) {
84    lower_freqs->data[fn] = freqs->data[fn];
85    center_freqs->data[fn] = freqs->data[fn + 1];
86    upper_freqs->data[fn] = freqs->data[fn + 2];
87  }
88
89  /* compute triangle heights so that each triangle has unit area */
90  for (fn = 0; fn < n_filters; fn++) {
91    triangle_heights->data[fn] =
92        2. / (upper_freqs->data[fn] - lower_freqs->data[fn]);
93  }
94
95  /* fill fft_freqs lookup table, which assigns the frequency in hz to each bin */
96  for (bin = 0; bin < win_s; bin++) {
97    fft_freqs->data[bin] =
98        aubio_bintofreq (bin, samplerate, (win_s - 1) * 2);
99  }
100
101  /* zeroing of all filters */
102  fmat_zeros (filters);
103
104  /* building each filter table */
105  for (fn = 0; fn < n_filters; fn++) {
106
107    /* skip first elements */
108    for (bin = 0; bin < win_s - 1; bin++) {
109      if (fft_freqs->data[bin] <= lower_freqs->data[fn] &&
110          fft_freqs->data[bin + 1] > lower_freqs->data[fn]) {
111        bin++;
112        break;
113      }
114    }
115
116    /* compute positive slope step size */
117    riseInc =
118        triangle_heights->data[fn] /
119        (center_freqs->data[fn] - lower_freqs->data[fn]);
120
121    /* compute coefficients in positive slope */
122    for (; bin < win_s - 1; bin++) {
123      filters->data[fn][bin] =
124          (fft_freqs->data[bin] - lower_freqs->data[fn]) * riseInc;
125
126      if (fft_freqs->data[bin + 1] >= center_freqs->data[fn]) {
127        bin++;
128        break;
129      }
130    }
131
132    /* compute negative slope step size */
133    downInc =
134        triangle_heights->data[fn] /
135        (upper_freqs->data[fn] - center_freqs->data[fn]);
136
137    /* compute coefficents in negative slope */
138    for (; bin < win_s - 1; bin++) {
139      filters->data[fn][bin] +=
140          (upper_freqs->data[fn] - fft_freqs->data[bin]) * downInc;
141
142      if (filters->data[fn][bin] < 0.) {
143        filters->data[fn][bin] = 0.;
144      }
145
146      if (fft_freqs->data[bin + 1] >= upper_freqs->data[fn])
147        break;
148    }
149    /* nothing else to do */
150
151  }
152
153  /* destroy temporarly allocated vectors */
154  del_fvec (lower_freqs);
155  del_fvec (upper_freqs);
156  del_fvec (center_freqs);
157
158  del_fvec (triangle_heights);
159  del_fvec (fft_freqs);
160
161  return AUBIO_OK;
162}
163
164uint_t
165aubio_filterbank_set_mel_coeffs_slaney (aubio_filterbank_t * fb,
166    smpl_t samplerate)
167{
168  uint_t retval;
169
170  /* Malcolm Slaney parameters */
171  smpl_t lowestFrequency = 133.3333;
172  smpl_t linearSpacing = 66.66666666;
173  smpl_t logSpacing = 1.0711703;
174
175  uint_t linearFilters = 13;
176  uint_t logFilters = 27;
177  uint_t n_filters = linearFilters + logFilters;
178
179  uint_t fn;                    /* filter counter */
180
181  smpl_t lastlinearCF;
182
183  /* buffers to compute filter frequencies */
184  fvec_t *freqs = new_fvec (n_filters + 2);
185
186  /* first step: fill all the linear filter frequencies */
187  for (fn = 0; fn < linearFilters; fn++) {
188    freqs->data[fn] = lowestFrequency + fn * linearSpacing;
189  }
190  lastlinearCF = freqs->data[fn - 1];
191
192  /* second step: fill all the log filter frequencies */
193  for (fn = 0; fn < logFilters + 2; fn++) {
194    freqs->data[fn + linearFilters] =
195        lastlinearCF * (POW (logSpacing, fn + 1));
196  }
197
198  /* now compute the actual coefficients */
199  retval = aubio_filterbank_set_triangle_bands (fb, freqs, samplerate);
200
201  /* destroy vector used to store frequency limits */
202  del_fvec (freqs);
203
204  return retval;
205}
Note: See TracBrowser for help on using the repository browser.