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
RevLine 
[06cae6c]1/*
2  Copyright (C) 2007-2009 Paul Brossier <piem@aubio.org>
3                      and Amaury Hazan <ahazan@iua.upf.edu>
4
[1c2e186]5  This file is part of aubio.
[06cae6c]6
[1c2e186]7  aubio is free software: you can redistribute it and/or modify
[06cae6c]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
[1c2e186]12  aubio is distributed in the hope that it will be useful,
[06cae6c]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
[1c2e186]18  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
[06cae6c]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
[cfd35db]28void
[1e37ade]29aubio_filterbank_set_mel_coeffs (aubio_filterbank_t * fb, fvec_t * freqs,
30    smpl_t samplerate)
[cfd35db]31{
[06cae6c]32
[cfd35db]33  fvec_t *filters = aubio_filterbank_get_coeffs (fb);
[06cae6c]34  uint_t n_filters = filters->channels, win_s = filters->length;
[cfd35db]35
[1e37ade]36  uint_t fn;                    /* filter counter */
37  uint_t bin;                   /* bin counter */
[cfd35db]38
[1e37ade]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);
[b507607]44  }
45
[4e0fbe6]46  /* convenience reference to lower/center/upper frequency for each triangle */
[1e37ade]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);
[cfd35db]50
[4e0fbe6]51  /* height of each triangle */
[1e37ade]52  fvec_t *triangle_heights = new_fvec (n_filters, 1);
[4e0fbe6]53
54  /* lookup table of each bin frequency in hz */
[cfd35db]55  fvec_t *fft_freqs = new_fvec (win_s, 1);
[06cae6c]56
[4e0fbe6]57  /* fill up the lower/center/upper */
[1e37ade]58  for (fn = 0; fn < n_filters; fn++) {
[4e0fbe6]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];
[06cae6c]62  }
63
[4e0fbe6]64  /* compute triangle heights so that each triangle has unit area */
[1e37ade]65  for (fn = 0; fn < n_filters; fn++) {
[4e0fbe6]66    triangle_heights->data[0][fn] =
67        2. / (upper_freqs->data[0][fn] - lower_freqs->data[0][fn]);
[06cae6c]68  }
[cfd35db]69
[4e0fbe6]70  /* fill fft_freqs lookup table, which assigns the frequency in hz to each bin */
71  for (bin = 0; bin < win_s; bin++) {
[2062e48]72    fft_freqs->data[0][bin] = aubio_bintofreq (bin, samplerate, (win_s - 1) * 2);
[06cae6c]73  }
74
[4e0fbe6]75  /* zeroing of all filters */
76  fvec_zeros (filters);
[b507607]77
78  /* building each filter table */
[4e0fbe6]79  for (fn = 0; fn < n_filters; fn++) {
[cfd35db]80
[b507607]81    /* skip first elements */
[4e0fbe6]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]) {
[06cae6c]85        break;
86      }
87    }
[4e0fbe6]88    bin++;
[cfd35db]89
[b507607]90    /* compute positive slope step size */
91    smpl_t riseInc =
[4e0fbe6]92        triangle_heights->data[0][fn] /
93        (center_freqs->data[0][fn] - lower_freqs->data[0][fn]);
[b507607]94
95    /* compute coefficients in positive slope */
[4e0fbe6]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])
[06cae6c]101        break;
102    }
[4e0fbe6]103    bin++;
[b507607]104
105    /* compute negative slope step size */
106    smpl_t downInc =
[4e0fbe6]107        triangle_heights->data[0][fn] /
108        (upper_freqs->data[0][fn] - center_freqs->data[0][fn]);
[cfd35db]109
[b507607]110    /* compute coefficents in negative slope */
[4e0fbe6]111    for (; bin < win_s - 1; bin++) {
112      filters->data[fn][bin] +=
113          (upper_freqs->data[0][fn] - fft_freqs->data[0][bin]) * downInc;
[cfd35db]114
[4e0fbe6]115      if (fft_freqs->data[0][bin + 1] > upper_freqs->data[0][fn])
[06cae6c]116        break;
117    }
[b507607]118    /* nothing else to do */
[06cae6c]119
120  }
121
[cfd35db]122  /* destroy temporarly allocated vectors */
123  del_fvec (lower_freqs);
124  del_fvec (upper_freqs);
125  del_fvec (center_freqs);
[06cae6c]126
[cfd35db]127  del_fvec (triangle_heights);
128  del_fvec (fft_freqs);
[06cae6c]129
[cfd35db]130}
[1e37ade]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.