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 "fmat.h"
25 #include "cvec.h"
26 #include "vecutils.h"
27 #include "spectral/filterbank.h"
28 #include "mathutils.h"
29
30 /** \brief A structure to store a set of n_filters filters of lenghts win_s */
31 struct _aubio_filterbank_t
32 {
33 uint_t win_s;
34 uint_t n_filters;
35 fmat_t *filters;
36 smpl_t norm;
37 smpl_t power;
38 };
39
40 aubio_filterbank_t *
new_aubio_filterbank(uint_t n_filters,uint_t win_s)41 new_aubio_filterbank (uint_t n_filters, uint_t win_s)
42 {
43 /* allocate space for filterbank object */
44 aubio_filterbank_t *fb = AUBIO_NEW (aubio_filterbank_t);
45
46 if ((sint_t)n_filters <= 0) {
47 AUBIO_ERR("filterbank: n_filters should be > 0, got %d\n", n_filters);
48 goto fail;
49 }
50 if ((sint_t)win_s <= 0) {
51 AUBIO_ERR("filterbank: win_s should be > 0, got %d\n", win_s);
52 goto fail;
53 }
54 fb->win_s = win_s;
55 fb->n_filters = n_filters;
56
57 /* allocate filter tables, a matrix of length win_s and of height n_filters */
58 fb->filters = new_fmat (n_filters, win_s / 2 + 1);
59
60 fb->norm = 1;
61
62 fb->power = 1;
63
64 return fb;
65 fail:
66 AUBIO_FREE (fb);
67 return NULL;
68 }
69
70 void
del_aubio_filterbank(aubio_filterbank_t * fb)71 del_aubio_filterbank (aubio_filterbank_t * fb)
72 {
73 del_fmat (fb->filters);
74 AUBIO_FREE (fb);
75 }
76
77 void
aubio_filterbank_do(aubio_filterbank_t * f,const cvec_t * in,fvec_t * out)78 aubio_filterbank_do (aubio_filterbank_t * f, const cvec_t * in, fvec_t * out)
79 {
80 /* apply filter to all input channel, provided out has enough channels */
81 //uint_t max_filters = MIN (f->n_filters, out->length);
82 //uint_t max_length = MIN (in->length, f->filters->length);
83
84 // view cvec->norm as fvec->data
85 fvec_t tmp;
86 tmp.length = in->length;
87 tmp.data = in->norm;
88
89 if (f->power != 1.) fvec_pow(&tmp, f->power);
90
91 fmat_vecmul(f->filters, &tmp, out);
92
93 return;
94 }
95
96 fmat_t *
aubio_filterbank_get_coeffs(const aubio_filterbank_t * f)97 aubio_filterbank_get_coeffs (const aubio_filterbank_t * f)
98 {
99 return f->filters;
100 }
101
102 uint_t
aubio_filterbank_set_coeffs(aubio_filterbank_t * f,const fmat_t * filter_coeffs)103 aubio_filterbank_set_coeffs (aubio_filterbank_t * f, const fmat_t * filter_coeffs)
104 {
105 fmat_copy(filter_coeffs, f->filters);
106 return 0;
107 }
108
109 uint_t
aubio_filterbank_set_norm(aubio_filterbank_t * f,smpl_t norm)110 aubio_filterbank_set_norm (aubio_filterbank_t *f, smpl_t norm)
111 {
112 if (norm != 0 && norm != 1) return AUBIO_FAIL;
113 f->norm = norm;
114 return AUBIO_OK;
115 }
116
117 smpl_t
aubio_filterbank_get_norm(aubio_filterbank_t * f)118 aubio_filterbank_get_norm (aubio_filterbank_t *f)
119 {
120 return f->norm;
121 }
122
123 uint_t
aubio_filterbank_set_power(aubio_filterbank_t * f,smpl_t power)124 aubio_filterbank_set_power (aubio_filterbank_t *f, smpl_t power)
125 {
126 f->power = power;
127 return AUBIO_OK;
128 }
129
130 smpl_t
aubio_filterbank_get_power(aubio_filterbank_t * f)131 aubio_filterbank_get_power (aubio_filterbank_t *f)
132 {
133 return f->power;
134 }
135