source: src/spectral/filterbank_mel.c @ 05a8c18

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

src/spectral/: add const qualifiers

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