812bb2070ce0d2490665800b89e548812521a0f7
[aubio.git] / src / spectral / awhitening.c
1 /*
2  * Copyright (C) 2003-2015 Paul Brossier <piem@aubio.org>
3  *
4  * This file is part of aubio.
5  *
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
9  * version.
10  *
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
14  * details.
15  *
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/>.
18  *
19  */
20
21 #include "aubio_priv.h"
22 #include "fvec.h"
23 #include "cvec.h"
24 #include "mathutils.h"
25 #include "spectral/awhitening.h"
26
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
30
31 /** structure to store object state */
32 struct _aubio_spectral_whitening_t {
33   uint_t buf_size;
34   uint_t hop_size;
35   uint_t samplerate;
36   smpl_t relax_time;
37   smpl_t r_decay;
38   smpl_t floor;
39   fvec_t *peak_values;
40 };
41
42 void
43 aubio_spectral_whitening_do (aubio_spectral_whitening_t * o, cvec_t * fftgrain)
44 {
45   uint_t i = 0;
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];
50   }
51 }
52
53 aubio_spectral_whitening_t *
54 new_aubio_spectral_whitening (uint_t buf_size, uint_t hop_size, uint_t samplerate)
55 {
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);
59     goto beach;
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);
62     goto beach;
63   } else if ((sint_t)samplerate < 1) {
64     AUBIO_ERR("spectral_whitening: got samplerate %d, but can not be < 1\n", samplerate);
65     goto beach;
66   }
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);
74   return o;
75
76 beach:
77   AUBIO_FREE(o);
78   return NULL;
79 }
80
81 uint_t
82 aubio_spectral_whitening_set_relax_time (aubio_spectral_whitening_t * o, smpl_t relax_time)
83 {
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);
87   return AUBIO_OK;
88 }
89
90 smpl_t
91 aubio_spectral_whitening_get_relax_time (aubio_spectral_whitening_t * o)
92 {
93   return o->relax_time;
94 }
95
96 uint_t
97 aubio_spectral_whitening_set_floor (aubio_spectral_whitening_t *o, smpl_t floor)
98 {
99   o->floor = floor;
100   return AUBIO_OK;
101 }
102
103 smpl_t aubio_spectral_whitening_get_floor (aubio_spectral_whitening_t *o)
104 {
105   return o->floor;
106 }
107
108 void
109 aubio_spectral_whitening_reset (aubio_spectral_whitening_t * o)
110 {
111   /* cover the case n == 0. */
112   fvec_set_all (o->peak_values, o->floor);
113 }
114
115 void
116 del_aubio_spectral_whitening (aubio_spectral_whitening_t * o)
117 {
118   del_fvec (o->peak_values);
119   AUBIO_FREE (o);
120 }