summaryrefslogtreecommitdiff
path: root/src/utils/hist.c
blob: 9b5ab10a2988df48512073eb6969d9ac83877a55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>

  This file is part of aubio.

  aubio is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  aubio is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with aubio.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "aubio_priv.h"
#include "fvec.h"
#include "utils/scale.h"
#include "mathutils.h" //fvec_min fvec_max
#include "utils/hist.h"

/********
 * Object Structure
 */

struct _aubio_hist_t {
  fvec_t * hist;
  uint_t nelems;
  fvec_t * cent;
  aubio_scale_t *scaler;
};

/**
 * Object creation/deletion calls
 */
aubio_hist_t * new_aubio_hist (smpl_t flow, smpl_t fhig, uint_t nelems){
  aubio_hist_t * s = AUBIO_NEW(aubio_hist_t);
  smpl_t step = (fhig-flow)/(smpl_t)(nelems);
  smpl_t accum = step;
  uint_t i;
  s->nelems = nelems;
  s->hist = new_fvec(nelems);
  s->cent = new_fvec(nelems);

  /* use scale to map flow/fhig -> 0/nelems */
  s->scaler = new_aubio_scale(flow,fhig,0,nelems);
  /* calculate centers now once */
  s->cent->data[0] = flow + 0.5 * step;
  for (i=1; i < s->nelems; i++, accum+=step )
    s->cent->data[i] = s->cent->data[0] + accum;

  return s;
}

void del_aubio_hist(aubio_hist_t *s) {
  del_fvec(s->hist);
  del_fvec(s->cent);
  del_aubio_scale(s->scaler);
  AUBIO_FREE(s);
}

/***
 * do it
 */
void aubio_hist_do (aubio_hist_t *s, fvec_t *input) {
  uint_t j;
  sint_t tmp = 0;
  aubio_scale_do(s->scaler, input);
  /* reset data */
  fvec_zeros(s->hist);
  /* run accum */
  for (j=0;  j < input->length; j++)
  {
    tmp = (sint_t)FLOOR(input->data[j]);
    if ((tmp >= 0) && (tmp < (sint_t)s->nelems)) {
      s->hist->data[tmp] += 1;
    }
  }
}

void aubio_hist_do_notnull (aubio_hist_t *s, fvec_t *input) {
  uint_t j;
  sint_t tmp = 0;
  aubio_scale_do(s->scaler, input);
  /* reset data */
  fvec_zeros(s->hist);
  /* run accum */
  for (j=0;  j < input->length; j++) {
    if (input->data[j] != 0) {
      tmp = (sint_t)FLOOR(input->data[j]);
      if ((tmp >= 0) && (tmp < (sint_t)s->nelems))
        s->hist->data[tmp] += 1;
    }
  }
}


void aubio_hist_dyn_notnull (aubio_hist_t *s, fvec_t *input) {
  uint_t i;
  sint_t tmp = 0;
  smpl_t ilow = fvec_min(input);
  smpl_t ihig = fvec_max(input);
  smpl_t step = (ihig-ilow)/(smpl_t)(s->nelems);

  /* readapt */
  aubio_scale_set_limits (s->scaler, ilow, ihig, 0, s->nelems);

  /* recalculate centers */
  s->cent->data[0] = ilow + 0.5f * step;
  for (i=1; i < s->nelems; i++)
    s->cent->data[i] = s->cent->data[0] + i * step;

  /* scale */
  aubio_scale_do(s->scaler, input);

  /* reset data */
  fvec_zeros(s->hist);
  /* run accum */
  for (i=0;  i < input->length; i++) {
    if (input->data[i] != 0) {
      tmp = (sint_t)FLOOR(input->data[i]);
      if ((tmp >= 0) && (tmp < (sint_t)s->nelems))
        s->hist->data[tmp] += 1;
    }
  }
}

void aubio_hist_weight (aubio_hist_t *s) {
  uint_t j;
  for (j=0; j < s->nelems; j++) {
    s->hist->data[j] *= s->cent->data[j];
  }
}

smpl_t aubio_hist_mean (const aubio_hist_t *s) {
  uint_t j;
  smpl_t tmp = 0.0;
  for (j=0; j < s->nelems; j++)
    tmp += s->hist->data[j];
  return tmp/(smpl_t)(s->nelems);
}