2 Copyright (C) 2007-2009 Paul Brossier <piem@aubio.org>
3 and Amaury Hazan <ahazan@iua.upf.edu>
5 This file is part of aubio.
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.
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.
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/>.
22 #include "aubio_priv.h"
25 #include "spectral/filterbank.h"
26 #include "mathutils.h"
29 aubio_filterbank_set_mel_coeffs (aubio_filterbank_t * fb, fvec_t * freqs,
33 fvec_t *filters = aubio_filterbank_get_coeffs (fb);
34 uint_t n_filters = filters->channels, win_s = filters->length;
36 uint_t fn; /* filter counter */
37 uint_t bin; /* bin counter */
39 /* freqs define the bands of triangular overlapping windows.
40 throw a warning if filterbank object fb is too short. */
41 if (freqs->length - 2 > n_filters) {
42 AUBIO_WRN ("not enough filters, %d allocated but %d requested\n",
43 n_filters, freqs->length - 2);
46 /* convenience reference to lower/center/upper frequency for each triangle */
47 fvec_t *lower_freqs = new_fvec (n_filters, 1);
48 fvec_t *upper_freqs = new_fvec (n_filters, 1);
49 fvec_t *center_freqs = new_fvec (n_filters, 1);
51 /* height of each triangle */
52 fvec_t *triangle_heights = new_fvec (n_filters, 1);
54 /* lookup table of each bin frequency in hz */
55 fvec_t *fft_freqs = new_fvec (win_s, 1);
57 /* fill up the lower/center/upper */
58 for (fn = 0; fn < n_filters; fn++) {
59 lower_freqs->data[0][fn] = freqs->data[0][fn];
60 center_freqs->data[0][fn] = freqs->data[0][fn + 1];
61 upper_freqs->data[0][fn] = freqs->data[0][fn + 2];
64 /* compute triangle heights so that each triangle has unit area */
65 for (fn = 0; fn < n_filters; fn++) {
66 triangle_heights->data[0][fn] =
67 2. / (upper_freqs->data[0][fn] - lower_freqs->data[0][fn]);
70 /* fill fft_freqs lookup table, which assigns the frequency in hz to each bin */
71 for (bin = 0; bin < win_s; bin++) {
72 fft_freqs->data[0][bin] = aubio_bintofreq (bin, samplerate, (win_s - 1) * 2);
75 /* zeroing of all filters */
78 /* building each filter table */
79 for (fn = 0; fn < n_filters; fn++) {
81 /* skip first elements */
82 for (bin = 0; bin < win_s - 1; bin++) {
83 if (fft_freqs->data[0][bin] <= lower_freqs->data[0][fn] &&
84 fft_freqs->data[0][bin + 1] > lower_freqs->data[0][fn]) {
90 /* compute positive slope step size */
92 triangle_heights->data[0][fn] /
93 (center_freqs->data[0][fn] - lower_freqs->data[0][fn]);
95 /* compute coefficients in positive slope */
96 for (; bin < win_s - 1; bin++) {
97 filters->data[fn][bin] =
98 (fft_freqs->data[0][bin] - lower_freqs->data[0][fn]) * riseInc;
100 if (fft_freqs->data[0][bin + 1] > center_freqs->data[0][fn])
105 /* compute negative slope step size */
107 triangle_heights->data[0][fn] /
108 (upper_freqs->data[0][fn] - center_freqs->data[0][fn]);
110 /* compute coefficents in negative slope */
111 for (; bin < win_s - 1; bin++) {
112 filters->data[fn][bin] +=
113 (upper_freqs->data[0][fn] - fft_freqs->data[0][bin]) * downInc;
115 if (fft_freqs->data[0][bin + 1] > upper_freqs->data[0][fn])
118 /* nothing else to do */
122 /* destroy temporarly allocated vectors */
123 del_fvec (lower_freqs);
124 del_fvec (upper_freqs);
125 del_fvec (center_freqs);
127 del_fvec (triangle_heights);
128 del_fvec (fft_freqs);
133 aubio_filterbank_set_mel_coeffs_slaney (aubio_filterbank_t * fb,
136 /* Malcolm Slaney parameters */
137 smpl_t lowestFrequency = 133.3333;
138 smpl_t linearSpacing = 66.66666666;
139 smpl_t logSpacing = 1.0711703;
141 uint_t linearFilters = 13;
142 uint_t logFilters = 27;
143 uint_t n_filters = linearFilters + logFilters;
145 uint_t fn; /* filter counter */
147 /* buffers to compute filter frequencies */
148 fvec_t *freqs = new_fvec (n_filters + 2, 1);
150 /* first step: fill all the linear filter frequencies */
151 for (fn = 0; fn < linearFilters; fn++) {
152 freqs->data[0][fn] = lowestFrequency + fn * linearSpacing;
154 smpl_t lastlinearCF = freqs->data[0][fn - 1];
156 /* second step: fill all the log filter frequencies */
157 for (fn = 0; fn < logFilters + 2; fn++) {
158 freqs->data[0][fn + linearFilters] =
159 lastlinearCF * (POW (logSpacing, fn + 1));
162 /* now compute the actual coefficients */
163 aubio_filterbank_set_mel_coeffs (fb, freqs, samplerate);
165 /* destroy vector used to store frequency limits */