2 * Copyright (C) 2003-2015 Paul Brossier <piem@aubio.org>
4 * This file is part of aubio.
6 * aubio is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, either version 3 of the License, or (at your option) any later
11 * aubio is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License along with
17 * aubio. If not, see <http://www.gnu.org/licenses/>.
21 #include "aubio_priv.h"
24 #include "mathutils.h"
25 #include "spectral/awhitening.h"
27 #define aubio_spectral_whitening_default_relax_time 250 // in seconds, between 22 and 446
28 #define aubio_spectral_whitening_default_decay 0.001 // -60dB attenuation
29 #define aubio_spectral_whitening_default_floor 1.e-4 // from 1.e-6 to .2
31 /** structure to store object state */
32 struct _aubio_spectral_whitening_t {
43 aubio_spectral_whitening_do (aubio_spectral_whitening_t * o, cvec_t * fftgrain)
46 for (i = 0; i < o->peak_values->length; i++) {
47 smpl_t tmp = MAX(o->r_decay * o->peak_values->data[i], o->floor);
48 o->peak_values->data[i] = MAX(fftgrain->norm[i], tmp);
49 fftgrain->norm[i] /= o->peak_values->data[i];
53 aubio_spectral_whitening_t *
54 new_aubio_spectral_whitening (uint_t buf_size, uint_t hop_size, uint_t samplerate)
56 aubio_spectral_whitening_t *o = AUBIO_NEW (aubio_spectral_whitening_t);
57 if ((sint_t)buf_size < 1) {
58 AUBIO_ERR("spectral_whitening: got buffer_size %d, but can not be < 1\n", buf_size);
60 } else if ((sint_t)hop_size < 1) {
61 AUBIO_ERR("spectral_whitening: got hop_size %d, but can not be < 1\n", hop_size);
63 } else if ((sint_t)samplerate < 1) {
64 AUBIO_ERR("spectral_whitening: got samplerate %d, but can not be < 1\n", samplerate);
67 o->peak_values = new_fvec (buf_size / 2 + 1);
68 o->buf_size = buf_size;
69 o->hop_size = hop_size;
70 o->samplerate = samplerate;
71 o->floor = aubio_spectral_whitening_default_floor;
72 aubio_spectral_whitening_set_relax_time (o, aubio_spectral_whitening_default_relax_time);
73 aubio_spectral_whitening_reset (o);
82 aubio_spectral_whitening_set_relax_time (aubio_spectral_whitening_t * o, smpl_t relax_time)
84 o->relax_time = relax_time;
85 o->r_decay = POW (aubio_spectral_whitening_default_decay,
86 (o->hop_size / (float) o->samplerate) / o->relax_time);
91 aubio_spectral_whitening_get_relax_time (aubio_spectral_whitening_t * o)
97 aubio_spectral_whitening_reset (aubio_spectral_whitening_t * o)
99 /* cover the case n == 0. */
100 fvec_set_all (o->peak_values, o->floor);
104 del_aubio_spectral_whitening (aubio_spectral_whitening_t * o)
106 del_fvec (o->peak_values);