source: src/spectral/filterbank_mel.c @ 4e0fbe6

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

src/spectral/filterbank_mel.c: shorten counter variable names, improve comments, run through indent

  • Property mode set to 100644
File size: 4.7 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, smpl_t samplerate,
30    smpl_t freq_min, smpl_t freq_max)
31{
32
33  fvec_t *filters = aubio_filterbank_get_coeffs (fb);
34  uint_t n_filters = filters->channels, win_s = filters->length;
35
36  /* Malcolm Slaney parameters */
37  smpl_t lowestFrequency = 133.3333;
38  smpl_t linearSpacing = 66.66666666;
39  smpl_t logSpacing = 1.0711703;
40
41  uint_t linearFilters = 13;
42  uint_t logFilters = 27;
43  uint_t allFilters = linearFilters + logFilters;
44
45  /* throw a warning if filterbank object fb is too short */
46  if (allFilters > n_filters) {
47    AUBIO_WRN ("not enough Mel filters, got %d but %d needed\n",
48        n_filters, allFilters);
49  }
50
51  /* buffers for computing filter frequencies */
52  fvec_t *freqs = new_fvec (allFilters + 2, 1);
53
54  /* convenience reference to lower/center/upper frequency for each triangle */
55  fvec_t *lower_freqs = new_fvec (allFilters, 1);
56  fvec_t *upper_freqs = new_fvec (allFilters, 1);
57  fvec_t *center_freqs = new_fvec (allFilters, 1);
58
59  /* height of each triangle */
60  fvec_t *triangle_heights = new_fvec (allFilters, 1);
61
62  /* lookup table of each bin frequency in hz */
63  fvec_t *fft_freqs = new_fvec (win_s, 1);
64
65  uint_t fn;                    /* filter counter */
66  uint_t bin;                   /* bin counter */
67
68  /* first step: filling all the linear filter frequencies */
69  for (fn = 0; fn < linearFilters; fn++) {
70    freqs->data[0][fn] = lowestFrequency + fn * linearSpacing;
71  }
72  smpl_t lastlinearCF = freqs->data[0][fn - 1];
73
74  /* second step: filling all the log filter frequencies */
75  for (fn = 0; fn < logFilters + 2; fn++) {
76    freqs->data[0][fn + linearFilters] =
77        lastlinearCF * (POW (logSpacing, fn + 1));
78  }
79
80  /* fill up the lower/center/upper */
81  for (fn = 0; fn < allFilters; fn++) {
82    lower_freqs->data[0][fn] = freqs->data[0][fn];
83    center_freqs->data[0][fn] = freqs->data[0][fn + 1];
84    upper_freqs->data[0][fn] = freqs->data[0][fn + 2];
85  }
86
87  /* compute triangle heights so that each triangle has unit area */
88  for (fn = 0; fn < allFilters; fn++) {
89    triangle_heights->data[0][fn] =
90        2. / (upper_freqs->data[0][fn] - lower_freqs->data[0][fn]);
91  }
92
93  /* fill fft_freqs lookup table, which assigns the frequency in hz to each bin */
94  for (bin = 0; bin < win_s; bin++) {
95    fft_freqs->data[0][bin] = aubio_bintofreq (bin, samplerate, win_s);
96  }
97
98  /* zeroing of all filters */
99  fvec_zeros (filters);
100
101  /* building each filter table */
102  for (fn = 0; fn < n_filters; fn++) {
103
104    /* skip first elements */
105    for (bin = 0; bin < win_s - 1; bin++) {
106      if (fft_freqs->data[0][bin] <= lower_freqs->data[0][fn] &&
107          fft_freqs->data[0][bin + 1] > lower_freqs->data[0][fn]) {
108        break;
109      }
110    }
111    bin++;
112
113    /* compute positive slope step size */
114    smpl_t riseInc =
115        triangle_heights->data[0][fn] /
116        (center_freqs->data[0][fn] - lower_freqs->data[0][fn]);
117
118    /* compute coefficients in positive slope */
119    for (; bin < win_s - 1; bin++) {
120      filters->data[fn][bin] =
121          (fft_freqs->data[0][bin] - lower_freqs->data[0][fn]) * riseInc;
122
123      if (fft_freqs->data[0][bin + 1] > center_freqs->data[0][fn])
124        break;
125    }
126    bin++;
127
128    /* compute negative slope step size */
129    smpl_t downInc =
130        triangle_heights->data[0][fn] /
131        (upper_freqs->data[0][fn] - center_freqs->data[0][fn]);
132
133    /* compute coefficents in negative slope */
134    for (; bin < win_s - 1; bin++) {
135      filters->data[fn][bin] +=
136          (upper_freqs->data[0][fn] - fft_freqs->data[0][bin]) * downInc;
137
138      if (fft_freqs->data[0][bin + 1] > upper_freqs->data[0][fn])
139        break;
140    }
141    /* nothing else to do */
142
143  }
144
145  /* destroy temporarly allocated vectors */
146  del_fvec (freqs);
147  del_fvec (lower_freqs);
148  del_fvec (upper_freqs);
149  del_fvec (center_freqs);
150
151  del_fvec (triangle_heights);
152  del_fvec (fft_freqs);
153
154}
Note: See TracBrowser for help on using the repository browser.