source: src/utils/hist.c @ 64959c4

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

[hist] fix potential memory leak

  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[96fb8ad]1/*
[e6a78ea]2  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
[96fb8ad]3
[e6a78ea]4  This file is part of aubio.
[96fb8ad]5
[e6a78ea]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/>.
[96fb8ad]18
[3c707f7]19*/
[96fb8ad]20
21#include "aubio_priv.h"
[6c7d49b]22#include "fvec.h"
[32d6958]23#include "utils/scale.h"
[2f64b0e]24#include "mathutils.h" //fvec_min fvec_max
[32d6958]25#include "utils/hist.h"
[96fb8ad]26
27/********
28 * Object Structure
29 */
30
31struct _aubio_hist_t {
[3c707f7]32  fvec_t * hist;
33  uint_t nelems;
34  fvec_t * cent;
35  aubio_scale_t *scaler;
[96fb8ad]36};
37
[dde06ad]38/**
39 * Object creation/deletion calls
[96fb8ad]40 */
[32082e5]41aubio_hist_t * new_aubio_hist (smpl_t flow, smpl_t fhig, uint_t nelems){
[3c707f7]42  aubio_hist_t * s = AUBIO_NEW(aubio_hist_t);
[32082e5]43  smpl_t step = (fhig-flow)/(smpl_t)(nelems);
[3c707f7]44  smpl_t accum = step;
45  uint_t i;
[042d77d]46  if ((sint_t)nelems <= 0) {
[fb0afbb]47    AUBIO_FREE(s);
[042d77d]48    return NULL;
49  }
[3c707f7]50  s->nelems = nelems;
[fc61225]51  s->hist = new_fvec(nelems);
52  s->cent = new_fvec(nelems);
[3c707f7]53
[32082e5]54  /* use scale to map flow/fhig -> 0/nelems */
55  s->scaler = new_aubio_scale(flow,fhig,0,nelems);
[3c707f7]56  /* calculate centers now once */
[32082e5]57  s->cent->data[0] = flow + 0.5 * step;
[3c707f7]58  for (i=1; i < s->nelems; i++, accum+=step )
[fc61225]59    s->cent->data[i] = s->cent->data[0] + accum;
[3c707f7]60
61  return s;
[96fb8ad]62}
63
64void del_aubio_hist(aubio_hist_t *s) {
[274839f]65  del_fvec(s->hist);
[3c707f7]66  del_fvec(s->cent);
67  del_aubio_scale(s->scaler);
68  AUBIO_FREE(s);
[96fb8ad]69}
70
71/***
72 * do it
73 */
[3c707f7]74void aubio_hist_do (aubio_hist_t *s, fvec_t *input) {
[fc61225]75  uint_t j;
[3c707f7]76  sint_t tmp = 0;
77  aubio_scale_do(s->scaler, input);
78  /* reset data */
[fc61225]79  fvec_zeros(s->hist);
[3c707f7]80  /* run accum */
[fc61225]81  for (j=0;  j < input->length; j++)
82  {
83    tmp = (sint_t)FLOOR(input->data[j]);
84    if ((tmp >= 0) && (tmp < (sint_t)s->nelems)) {
85      s->hist->data[tmp] += 1;
[3c707f7]86    }
[fc61225]87  }
[96fb8ad]88}
89
[3c707f7]90void aubio_hist_do_notnull (aubio_hist_t *s, fvec_t *input) {
[fc61225]91  uint_t j;
[3c707f7]92  sint_t tmp = 0;
93  aubio_scale_do(s->scaler, input);
94  /* reset data */
[fc61225]95  fvec_zeros(s->hist);
[3c707f7]96  /* run accum */
[fc61225]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;
[3c707f7]102    }
[fc61225]103  }
[96fb8ad]104}
105
106
[3c707f7]107void aubio_hist_dyn_notnull (aubio_hist_t *s, fvec_t *input) {
[fc61225]108  uint_t i;
[3c707f7]109  sint_t tmp = 0;
[2f64b0e]110  smpl_t ilow = fvec_min(input);
[1e2c82f]111  smpl_t ihig = fvec_max(input);
[3c707f7]112  smpl_t step = (ihig-ilow)/(smpl_t)(s->nelems);
113
114  /* readapt */
[0ce97483]115  aubio_scale_set_limits (s->scaler, ilow, ihig, 0, s->nelems);
[3c707f7]116
117  /* recalculate centers */
[fc61225]118  s->cent->data[0] = ilow + 0.5f * step;
[3c707f7]119  for (i=1; i < s->nelems; i++)
[fc61225]120    s->cent->data[i] = s->cent->data[0] + i * step;
[3c707f7]121
122  /* scale */
123  aubio_scale_do(s->scaler, input);
124
125  /* reset data */
[fc61225]126  fvec_zeros(s->hist);
[3c707f7]127  /* run accum */
[fc61225]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;
[3c707f7]133    }
[fc61225]134  }
[96fb8ad]135}
136
[3c707f7]137void aubio_hist_weight (aubio_hist_t *s) {
[fc61225]138  uint_t j;
139  for (j=0; j < s->nelems; j++) {
140    s->hist->data[j] *= s->cent->data[j];
141  }
[96fb8ad]142}
143
[55d1fa4]144smpl_t aubio_hist_mean (const aubio_hist_t *s) {
[fc61225]145  uint_t j;
[acf7d30]146  smpl_t tmp = 0.0;
[fc61225]147  for (j=0; j < s->nelems; j++)
148    tmp += s->hist->data[j];
[3c707f7]149  return tmp/(smpl_t)(s->nelems);
[96fb8ad]150}
151
Note: See TracBrowser for help on using the repository browser.