source: src/spectral/filterbank_mel.c @ 779966b

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

src/spectral: switch to mono

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