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 | uint_t channels; |
---|
35 | fvec_t * cent; |
---|
36 | aubio_scale_t *scaler; |
---|
37 | }; |
---|
38 | |
---|
39 | /** |
---|
40 | * Object creation/deletion calls |
---|
41 | */ |
---|
42 | aubio_hist_t * new_aubio_hist (smpl_t ilow, smpl_t ihig, uint_t nelems, uint_t channels){ |
---|
43 | aubio_hist_t * s = AUBIO_NEW(aubio_hist_t); |
---|
44 | smpl_t step = (ihig-ilow)/(smpl_t)(nelems); |
---|
45 | smpl_t accum = step; |
---|
46 | uint_t i; |
---|
47 | s->channels = channels; |
---|
48 | s->nelems = nelems; |
---|
49 | s->hist = new_fvec(nelems, channels); |
---|
50 | s->cent = new_fvec(nelems, 1); |
---|
51 | |
---|
52 | /* use scale to map ilow/ihig -> 0/nelems */ |
---|
53 | s->scaler = new_aubio_scale(ilow,ihig,0,nelems); |
---|
54 | /* calculate centers now once */ |
---|
55 | s->cent->data[0][0] = ilow + 0.5 * step; |
---|
56 | for (i=1; i < s->nelems; i++, accum+=step ) |
---|
57 | s->cent->data[0][i] = s->cent->data[0][0] + accum; |
---|
58 | |
---|
59 | return s; |
---|
60 | } |
---|
61 | |
---|
62 | void del_aubio_hist(aubio_hist_t *s) { |
---|
63 | del_fvec(s->hist); |
---|
64 | del_fvec(s->cent); |
---|
65 | del_aubio_scale(s->scaler); |
---|
66 | AUBIO_FREE(s); |
---|
67 | } |
---|
68 | |
---|
69 | /*** |
---|
70 | * do it |
---|
71 | */ |
---|
72 | void aubio_hist_do (aubio_hist_t *s, fvec_t *input) { |
---|
73 | uint_t i,j; |
---|
74 | sint_t tmp = 0; |
---|
75 | aubio_scale_do(s->scaler, input); |
---|
76 | /* reset data */ |
---|
77 | for (i=0; i < s->channels; i++) |
---|
78 | for (j=0; j < s->nelems; j++) |
---|
79 | s->hist->data[i][j] = 0; |
---|
80 | /* run accum */ |
---|
81 | for (i=0; i < input->channels; i++) |
---|
82 | for (j=0; j < input->length; j++) |
---|
83 | { |
---|
84 | tmp = (sint_t)FLOOR(input->data[i][j]); |
---|
85 | if ((tmp >= 0) && (tmp < (sint_t)s->nelems)) |
---|
86 | s->hist->data[i][tmp] += 1; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | void aubio_hist_do_notnull (aubio_hist_t *s, fvec_t *input) { |
---|
91 | uint_t i,j; |
---|
92 | sint_t tmp = 0; |
---|
93 | aubio_scale_do(s->scaler, input); |
---|
94 | /* reset data */ |
---|
95 | for (i=0; i < s->channels; i++) |
---|
96 | for (j=0; j < s->nelems; j++) |
---|
97 | s->hist->data[i][j] = 0; |
---|
98 | /* run accum */ |
---|
99 | for (i=0; i < input->channels; i++) |
---|
100 | for (j=0; j < input->length; j++) { |
---|
101 | if (input->data[i][j] != 0) { |
---|
102 | tmp = (sint_t)FLOOR(input->data[i][j]); |
---|
103 | if ((tmp >= 0) && (tmp < (sint_t)s->nelems)) |
---|
104 | s->hist->data[i][tmp] += 1; |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | void aubio_hist_dyn_notnull (aubio_hist_t *s, fvec_t *input) { |
---|
111 | uint_t i,j; |
---|
112 | sint_t tmp = 0; |
---|
113 | smpl_t ilow = fvec_min(input); |
---|
114 | smpl_t ihig = fvec_max(input); |
---|
115 | smpl_t step = (ihig-ilow)/(smpl_t)(s->nelems); |
---|
116 | |
---|
117 | /* readapt */ |
---|
118 | aubio_scale_set_limits (s->scaler, ilow, ihig, 0, s->nelems); |
---|
119 | |
---|
120 | /* recalculate centers */ |
---|
121 | s->cent->data[0][0] = ilow + 0.5f * step; |
---|
122 | for (i=1; i < s->nelems; i++) |
---|
123 | s->cent->data[0][i] = s->cent->data[0][0] + i * step; |
---|
124 | |
---|
125 | /* scale */ |
---|
126 | aubio_scale_do(s->scaler, input); |
---|
127 | |
---|
128 | /* reset data */ |
---|
129 | for (i=0; i < s->channels; i++) |
---|
130 | for (j=0; j < s->nelems; j++) |
---|
131 | s->hist->data[i][j] = 0; |
---|
132 | /* run accum */ |
---|
133 | for (i=0; i < input->channels; i++) |
---|
134 | for (j=0; j < input->length; j++) { |
---|
135 | if (input->data[i][j] != 0) { |
---|
136 | tmp = (sint_t)FLOOR(input->data[i][j]); |
---|
137 | if ((tmp >= 0) && (tmp < (sint_t)s->nelems)) |
---|
138 | s->hist->data[i][tmp] += 1; |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | void aubio_hist_weight (aubio_hist_t *s) { |
---|
144 | uint_t i,j; |
---|
145 | for (i=0; i < s->channels; i++) |
---|
146 | for (j=0; j < s->nelems; j++) { |
---|
147 | s->hist->data[i][j] *= s->cent->data[0][j]; |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | smpl_t aubio_hist_mean (aubio_hist_t *s) { |
---|
152 | uint_t i,j; |
---|
153 | smpl_t tmp = 0.0; |
---|
154 | for (i=0; i < s->channels; i++) |
---|
155 | for (j=0; j < s->nelems; j++) |
---|
156 | tmp += s->hist->data[i][j]; |
---|
157 | return tmp/(smpl_t)(s->nelems); |
---|
158 | } |
---|
159 | |
---|