src/spectral/filterbank_mel.c: move Slaney's specific code to _slaney function
[aubio.git] / src / spectral / filterbank_mel.c
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 "fvec.h"
24 #include "cvec.h"
25 #include "spectral/filterbank.h"
26 #include "mathutils.h"
27
28 void
29 aubio_filterbank_set_mel_coeffs (aubio_filterbank_t * fb, fvec_t * freqs,
30     smpl_t samplerate)
31 {
32
33   fvec_t *filters = aubio_filterbank_get_coeffs (fb);
34   uint_t n_filters = filters->channels, win_s = filters->length;
35
36   uint_t fn;                    /* filter counter */
37   uint_t bin;                   /* bin counter */
38
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);
44   }
45
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);
50
51   /* height of each triangle */
52   fvec_t *triangle_heights = new_fvec (n_filters, 1);
53
54   /* lookup table of each bin frequency in hz */
55   fvec_t *fft_freqs = new_fvec (win_s, 1);
56
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];
62   }
63
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]);
68   }
69
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);
73   }
74
75   /* zeroing of all filters */
76   fvec_zeros (filters);
77
78   /* building each filter table */
79   for (fn = 0; fn < n_filters; fn++) {
80
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]) {
85         break;
86       }
87     }
88     bin++;
89
90     /* compute positive slope step size */
91     smpl_t riseInc =
92         triangle_heights->data[0][fn] /
93         (center_freqs->data[0][fn] - lower_freqs->data[0][fn]);
94
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;
99
100       if (fft_freqs->data[0][bin + 1] > center_freqs->data[0][fn])
101         break;
102     }
103     bin++;
104
105     /* compute negative slope step size */
106     smpl_t downInc =
107         triangle_heights->data[0][fn] /
108         (upper_freqs->data[0][fn] - center_freqs->data[0][fn]);
109
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;
114
115       if (fft_freqs->data[0][bin + 1] > upper_freqs->data[0][fn])
116         break;
117     }
118     /* nothing else to do */
119
120   }
121
122   /* destroy temporarly allocated vectors */
123   del_fvec (lower_freqs);
124   del_fvec (upper_freqs);
125   del_fvec (center_freqs);
126
127   del_fvec (triangle_heights);
128   del_fvec (fft_freqs);
129
130 }
131
132 void
133 aubio_filterbank_set_mel_coeffs_slaney (aubio_filterbank_t * fb,
134     smpl_t samplerate)
135 {
136   /* Malcolm Slaney parameters */
137   smpl_t lowestFrequency = 133.3333;
138   smpl_t linearSpacing = 66.66666666;
139   smpl_t logSpacing = 1.0711703;
140
141   uint_t linearFilters = 13;
142   uint_t logFilters = 27;
143   uint_t n_filters = linearFilters + logFilters;
144
145   uint_t fn;                    /* filter counter */
146   uint_t bin;                   /* bin counter */
147
148   /* buffers to compute filter frequencies */
149   fvec_t *freqs = new_fvec (n_filters + 2, 1);
150
151   /* first step: fill all the linear filter frequencies */
152   for (fn = 0; fn < linearFilters; fn++) {
153     freqs->data[0][fn] = lowestFrequency + fn * linearSpacing;
154   }
155   smpl_t lastlinearCF = freqs->data[0][fn - 1];
156
157   /* second step: fill all the log filter frequencies */
158   for (fn = 0; fn < logFilters + 2; fn++) {
159     freqs->data[0][fn + linearFilters] =
160         lastlinearCF * (POW (logSpacing, fn + 1));
161   }
162
163   /* now compute the actual coefficients */
164   aubio_filterbank_set_mel_coeffs (fb, freqs, samplerate);
165
166   /* destroy vector used to store frequency limits */
167   del_fvec (freqs);
168
169 }