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 {
35 aubio_scale_t *scaler;
39 * Object creation/deletion calls
41 aubio_hist_t * new_aubio_hist (smpl_t flow, smpl_t fhig, uint_t nelems){
42 aubio_hist_t * s = AUBIO_NEW(aubio_hist_t);
43 smpl_t step = (fhig-flow)/(smpl_t)(nelems);
46 if ((sint_t)nelems <= 0) {
51 s->hist = new_fvec(nelems);
52 s->cent = new_fvec(nelems);
54 /* use scale to map flow/fhig -> 0/nelems */
55 s->scaler = new_aubio_scale(flow,fhig,0,nelems);
56 /* calculate centers now once */
57 s->cent->data[0] = flow + 0.5 * step;
58 for (i=1; i < s->nelems; i++, accum+=step )
59 s->cent->data[i] = s->cent->data[0] + accum;
64 void del_aubio_hist(aubio_hist_t *s) {
67 del_aubio_scale(s->scaler);
74 void aubio_hist_do (aubio_hist_t *s, fvec_t *input) {
77 aubio_scale_do(s->scaler, input);
81 for (j=0; j < input->length; j++)
83 tmp = (sint_t)FLOOR(input->data[j]);
84 if ((tmp >= 0) && (tmp < (sint_t)s->nelems)) {
85 s->hist->data[tmp] += 1;
90 void aubio_hist_do_notnull (aubio_hist_t *s, fvec_t *input) {
93 aubio_scale_do(s->scaler, input);
97 for (j=0; j < input->length; j++) {
98 if (input->data[j] != 0) {
99 tmp = (sint_t)FLOOR(input->data[j]);
100 if ((tmp >= 0) && (tmp < (sint_t)s->nelems))
101 s->hist->data[tmp] += 1;
107 void aubio_hist_dyn_notnull (aubio_hist_t *s, fvec_t *input) {
110 smpl_t ilow = fvec_min(input);
111 smpl_t ihig = fvec_max(input);
112 smpl_t step = (ihig-ilow)/(smpl_t)(s->nelems);
115 aubio_scale_set_limits (s->scaler, ilow, ihig, 0, s->nelems);
117 /* recalculate centers */
118 s->cent->data[0] = ilow + 0.5f * step;
119 for (i=1; i < s->nelems; i++)
120 s->cent->data[i] = s->cent->data[0] + i * step;
123 aubio_scale_do(s->scaler, input);
128 for (i=0; i < input->length; i++) {
129 if (input->data[i] != 0) {
130 tmp = (sint_t)FLOOR(input->data[i]);
131 if ((tmp >= 0) && (tmp < (sint_t)s->nelems))
132 s->hist->data[tmp] += 1;
137 void aubio_hist_weight (aubio_hist_t *s) {
139 for (j=0; j < s->nelems; j++) {
140 s->hist->data[j] *= s->cent->data[j];
144 smpl_t aubio_hist_mean (const aubio_hist_t *s) {
147 for (j=0; j < s->nelems; j++)
148 tmp += s->hist->data[j];
149 return tmp/(smpl_t)(s->nelems);