2 Copyright (C) 2003 Paul Brossier
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include "aubio_priv.h"
21 #include "utils/scale.h"
22 #include "mathutils.h" //vec_min vec_max
23 #include "utils/hist.h"
29 struct _aubio_hist_t {
34 aubio_scale_t *scaler;
38 * Object creation/deletion calls
40 aubio_hist_t * new_aubio_hist (smpl_t ilow, smpl_t ihig, uint_t nelems, uint_t channels){
41 aubio_hist_t * s = AUBIO_NEW(aubio_hist_t);
42 smpl_t step = (ihig-ilow)/(smpl_t)(nelems);
45 s->channels = channels;
47 s->hist = new_fvec(nelems, channels);
48 s->cent = new_fvec(nelems, 1);
50 /* use scale to map ilow/ihig -> 0/nelems */
51 s->scaler = new_aubio_scale(ilow,ihig,0,nelems);
52 /* calculate centers now once */
53 s->cent->data[0][0] = ilow + 0.5 * step;
54 for (i=1; i < s->nelems; i++, accum+=step )
55 s->cent->data[0][i] = s->cent->data[0][0] + accum;
60 void del_aubio_hist(aubio_hist_t *s) {
63 del_aubio_scale(s->scaler);
70 void aubio_hist_do (aubio_hist_t *s, fvec_t *input) {
73 aubio_scale_do(s->scaler, input);
75 for (i=0; i < s->channels; i++)
76 for (j=0; j < s->nelems; j++)
77 s->hist->data[i][j] = 0;
79 for (i=0; i < input->channels; i++)
80 for (j=0; j < input->length; j++)
82 tmp = (sint_t)FLOOR(input->data[i][j]);
83 if ((tmp >= 0) && (tmp < (sint_t)s->nelems))
84 s->hist->data[i][tmp] += 1;
88 void aubio_hist_do_notnull (aubio_hist_t *s, fvec_t *input) {
91 aubio_scale_do(s->scaler, input);
93 for (i=0; i < s->channels; i++)
94 for (j=0; j < s->nelems; j++)
95 s->hist->data[i][j] = 0;
97 for (i=0; i < input->channels; i++)
98 for (j=0; j < input->length; j++) {
99 if (input->data[i][j] != 0) {
100 tmp = (sint_t)FLOOR(input->data[i][j]);
101 if ((tmp >= 0) && (tmp < (sint_t)s->nelems))
102 s->hist->data[i][tmp] += 1;
108 void aubio_hist_dyn_notnull (aubio_hist_t *s, fvec_t *input) {
111 smpl_t ilow = vec_min(input);
112 smpl_t ihig = vec_max(input);
113 smpl_t step = (ihig-ilow)/(smpl_t)(s->nelems);
116 aubio_scale_set(s->scaler, ilow, ihig, 0, s->nelems);
118 /* recalculate centers */
119 s->cent->data[0][0] = ilow + 0.5f * step;
120 for (i=1; i < s->nelems; i++)
121 s->cent->data[0][i] = s->cent->data[0][0] + i * step;
124 aubio_scale_do(s->scaler, input);
127 for (i=0; i < s->channels; i++)
128 for (j=0; j < s->nelems; j++)
129 s->hist->data[i][j] = 0;
131 for (i=0; i < input->channels; i++)
132 for (j=0; j < input->length; j++) {
133 if (input->data[i][j] != 0) {
134 tmp = (sint_t)FLOOR(input->data[i][j]);
135 if ((tmp >= 0) && (tmp < (sint_t)s->nelems))
136 s->hist->data[i][tmp] += 1;
141 void aubio_hist_weight (aubio_hist_t *s) {
143 for (i=0; i < s->channels; i++)
144 for (j=0; j < s->nelems; j++) {
145 s->hist->data[i][j] *= s->cent->data[0][j];
149 smpl_t aubio_hist_mean (aubio_hist_t *s) {
152 for (i=0; i < s->channels; i++)
153 for (j=0; j < s->nelems; j++)
154 tmp += s->hist->data[i][j];
155 return tmp/(smpl_t)(s->nelems);