source: src/spectral/constantq.c @ b6d0dd0

feature/constantq
Last change on this file since b6d0dd0 was b6d0dd0, checked in by Paul Brossier <piem@piem.org>, 6 years ago

src/spectral/constantq.c: first constant-q draft, plain implementation

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*
2  Copyright (C) 2018 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
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10
11  aubio is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21/** \file
22
23  Constant-Q Transform
24
25  TODO add long description
26
27  \example spectral/test-constantq.c
28
29*/
30
31#include "aubio_priv.h"
32#include "fvec.h"
33#include "cvec.h"
34#include "fmat.h"
35#include "spectral/constantq.h"
36
37struct _aubio_constantq_t {
38  uint_t size;
39  uint_t samplerate;
40  uint_t bins_per_octave;
41  fmat_t *coeffs;
42  smpl_t high_freq;
43  smpl_t low_freq;
44};
45
46uint_t aubio_constantq_reset_filters (aubio_constantq_t *s);
47
48aubio_constantq_t * new_aubio_constantq(uint_t size, uint_t samplerate,
49    uint_t bins_per_octave) {
50  aubio_constantq_t * s = AUBIO_NEW (aubio_constantq_t);
51  if ((sint_t) size <= 0) {
52    AUBIO_ERROR("constantq: failed creating with size %d, should be > 0\n",
53        size);
54    goto beach;
55  }
56  if ((sint_t) bins_per_octave <= 0) {
57    AUBIO_ERROR("constantq: failed creating with bins_per_octave %d"
58        ", should be > 0\n", bins_per_octave);
59    goto beach;
60  }
61
62  s->size = size / 2 + 1;
63  s->samplerate = samplerate;
64  s->bins_per_octave = bins_per_octave;
65  s->low_freq = aubio_miditofreq(19.0);
66  s->high_freq = aubio_miditofreq(MIN(140.0, 139.0 + 12.0/s->bins_per_octave));
67  if (aubio_constantq_reset_filters (s) != AUBIO_OK) goto beach;
68  //AUBIO_WRN ("constantq: %d size, %d Hz, %d bins\n", s->size, s->samplerate,
69  //    s->bins_per_octave);
70  return s;
71beach:
72  AUBIO_FREE(s);
73  return NULL;
74}
75
76uint_t aubio_constantq_reset_filters (aubio_constantq_t *s)
77{
78  // compute transform coefficients
79  uint_t i, j;
80  smpl_t ratio = POW(2.0, 1.0 / s->bins_per_octave);
81  uint_t nbins = (uint_t) FLOOR (LOG (s->high_freq / s->low_freq) / LOG (ratio));
82  if (nbins <= 0) {
83    AUBIO_ERROR ("constantq: can not be created with 0 bins, low_freq %.2f, "
84        "high_freq %.2f, bins_per_octave %d\n", s->low_freq, s->high_freq,
85        s->bins_per_octave);
86    return AUBIO_FAIL;
87  }
88  if (s->coeffs) del_fmat(s->coeffs);
89  s->coeffs = new_fmat (nbins, s->size);
90
91  smpl_t freq_step = s->samplerate * 0.5 / (s->size - 1.);
92  smpl_t min_bw = (smpl_t) s->samplerate / (smpl_t) s->size;
93
94  for (i = 0; i < nbins; i++) {
95    smpl_t band_freq = s->low_freq * POW (ratio, i);
96    smpl_t band_bw = MAX (band_freq * (ratio - 1.), min_bw);
97    for (j = 0; j < s->size; j++) {
98      smpl_t fftfreq = j * freq_step;
99      smpl_t val = (band_freq - fftfreq) / band_bw;
100      s->coeffs->data[i][j] = EXP (- 0.5 * SQR(val));
101    }
102  }
103
104  return AUBIO_OK;
105}
106
107void del_aubio_constantq(aubio_constantq_t *s) {
108  del_fmat(s->coeffs);
109  AUBIO_FREE(s);
110}
111
112void aubio_constantq_do(aubio_constantq_t *s, const cvec_t *input,
113    fvec_t *cqt_output) {
114  fvec_t input_norm;
115  input_norm.length = input->length;
116  input_norm.data = input->norm;
117  fmat_vecmul (s->coeffs, &input_norm, cqt_output);
118}
119
120uint_t aubio_constantq_get_numbins (aubio_constantq_t *s) {
121  if (!s || !s->coeffs) return 0;
122  return s->coeffs->height;
123}
Note: See TracBrowser for help on using the repository browser.