source: src/spectral/filterbank_mel.c @ 1c2e186

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

src/spectral/ update license headers

  • Property mode set to 100644
File size: 5.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 "fvec.h"
24#include "cvec.h"
25#include "spectral/filterbank.h"
26#include "mathutils.h"
27
28void
29aubio_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 - 1) * 2);
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
132void
133aubio_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
147  /* buffers to compute filter frequencies */
148  fvec_t *freqs = new_fvec (n_filters + 2, 1);
149
150  /* first step: fill all the linear filter frequencies */
151  for (fn = 0; fn < linearFilters; fn++) {
152    freqs->data[0][fn] = lowestFrequency + fn * linearSpacing;
153  }
154  smpl_t lastlinearCF = freqs->data[0][fn - 1];
155
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));
160  }
161
162  /* now compute the actual coefficients */
163  aubio_filterbank_set_mel_coeffs (fb, freqs, samplerate);
164
165  /* destroy vector used to store frequency limits */
166  del_fvec (freqs);
167
168}
Note: See TracBrowser for help on using the repository browser.