2 Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
4 This file is part of aubio.
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.
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.
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/>.
21 #include "aubio_priv.h"
23 #include "utils/scale.h"
24 #include "mathutils.h" //fvec_min fvec_max
25 #include "utils/hist.h"
31 struct _aubio_hist_t {
36 aubio_scale_t *scaler;
40 * Object creation/deletion calls
42 aubio_hist_t * new_aubio_hist (smpl_t ilow, smpl_t ihig, uint_t nelems, uint_t channels){
43 aubio_hist_t * s = AUBIO_NEW(aubio_hist_t);
44 smpl_t step = (ihig-ilow)/(smpl_t)(nelems);
47 s->channels = channels;
49 s->hist = new_fvec(nelems, channels);
50 s->cent = new_fvec(nelems, 1);
52 /* use scale to map ilow/ihig -> 0/nelems */
53 s->scaler = new_aubio_scale(ilow,ihig,0,nelems);
54 /* calculate centers now once */
55 s->cent->data[0][0] = ilow + 0.5 * step;
56 for (i=1; i < s->nelems; i++, accum+=step )
57 s->cent->data[0][i] = s->cent->data[0][0] + accum;
62 void del_aubio_hist(aubio_hist_t *s) {
65 del_aubio_scale(s->scaler);
72 void aubio_hist_do (aubio_hist_t *s, fvec_t *input) {
75 aubio_scale_do(s->scaler, input);
77 for (i=0; i < s->channels; i++)
78 for (j=0; j < s->nelems; j++)
79 s->hist->data[i][j] = 0;
81 for (i=0; i < input->channels; i++)
82 for (j=0; j < input->length; j++)
84 tmp = (sint_t)FLOOR(input->data[i][j]);
85 if ((tmp >= 0) && (tmp < (sint_t)s->nelems))
86 s->hist->data[i][tmp] += 1;
90 void aubio_hist_do_notnull (aubio_hist_t *s, fvec_t *input) {
93 aubio_scale_do(s->scaler, input);
95 for (i=0; i < s->channels; i++)
96 for (j=0; j < s->nelems; j++)
97 s->hist->data[i][j] = 0;
99 for (i=0; i < input->channels; i++)
100 for (j=0; j < input->length; j++) {
101 if (input->data[i][j] != 0) {
102 tmp = (sint_t)FLOOR(input->data[i][j]);
103 if ((tmp >= 0) && (tmp < (sint_t)s->nelems))
104 s->hist->data[i][tmp] += 1;
110 void aubio_hist_dyn_notnull (aubio_hist_t *s, fvec_t *input) {
113 smpl_t ilow = fvec_min(input);
114 smpl_t ihig = fvec_max(input);
115 smpl_t step = (ihig-ilow)/(smpl_t)(s->nelems);
118 aubio_scale_set_limits (s->scaler, ilow, ihig, 0, s->nelems);
120 /* recalculate centers */
121 s->cent->data[0][0] = ilow + 0.5f * step;
122 for (i=1; i < s->nelems; i++)
123 s->cent->data[0][i] = s->cent->data[0][0] + i * step;
126 aubio_scale_do(s->scaler, input);
129 for (i=0; i < s->channels; i++)
130 for (j=0; j < s->nelems; j++)
131 s->hist->data[i][j] = 0;
133 for (i=0; i < input->channels; i++)
134 for (j=0; j < input->length; j++) {
135 if (input->data[i][j] != 0) {
136 tmp = (sint_t)FLOOR(input->data[i][j]);
137 if ((tmp >= 0) && (tmp < (sint_t)s->nelems))
138 s->hist->data[i][tmp] += 1;
143 void aubio_hist_weight (aubio_hist_t *s) {
145 for (i=0; i < s->channels; i++)
146 for (j=0; j < s->nelems; j++) {
147 s->hist->data[i][j] *= s->cent->data[0][j];
151 smpl_t aubio_hist_mean (aubio_hist_t *s) {
154 for (i=0; i < s->channels; i++)
155 for (j=0; j < s->nelems; j++)
156 tmp += s->hist->data[i][j];
157 return tmp/(smpl_t)(s->nelems);