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