source: src/spectral/awhitening.c @ 21eb4c2

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since 21eb4c2 was fa0ef3b, checked in by Paul Brossier <piem@piem.org>, 8 years ago

src/spectral/awhitening.h: add _set_floor/_get_floor, improve documentation

  • Property mode set to 100644
File size: 3.4 KB
RevLine 
[9f060d1]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
[04ebe31]27#define aubio_spectral_whitening_default_relax_time   250   // in seconds, between 22 and 446
[9f060d1]28#define aubio_spectral_whitening_default_decay        0.001 // -60dB attenuation
[04ebe31]29#define aubio_spectral_whitening_default_floor        1.e-4 // from 1.e-6 to .2
[9f060d1]30
31/** structure to store object state */
32struct _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
42void
43aubio_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++) {
[04ebe31]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);
[9f060d1]49    fftgrain->norm[i] /= o->peak_values->data[i];
50  }
51}
52
53aubio_spectral_whitening_t *
54new_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;
[04ebe31]71  o->floor = aubio_spectral_whitening_default_floor;
[9f060d1]72  aubio_spectral_whitening_set_relax_time (o, aubio_spectral_whitening_default_relax_time);
73  aubio_spectral_whitening_reset (o);
74  return o;
75
76beach:
77  AUBIO_FREE(o);
78  return NULL;
79}
80
[192fa6a]81uint_t
[9f060d1]82aubio_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);
[192fa6a]87  return AUBIO_OK;
[9f060d1]88}
89
90smpl_t
91aubio_spectral_whitening_get_relax_time (aubio_spectral_whitening_t * o)
92{
93  return o->relax_time;
94}
95
[fa0ef3b]96uint_t
97aubio_spectral_whitening_set_floor (aubio_spectral_whitening_t *o, smpl_t floor)
98{
99  o->floor = floor;
100  return AUBIO_OK;
101}
102
103smpl_t aubio_spectral_whitening_get_floor (aubio_spectral_whitening_t *o)
104{
105  return o->floor;
106}
107
[9f060d1]108void
109aubio_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
115void
116del_aubio_spectral_whitening (aubio_spectral_whitening_t * o)
117{
118  del_fvec (o->peak_values);
119  AUBIO_FREE (o);
120}
Note: See TracBrowser for help on using the repository browser.