[96fb8ad] | 1 | /* |
---|
| 2 | Copyright (C) 2003 Paul Brossier |
---|
| 3 | |
---|
| 4 | This program is free software; you can redistribute it and/or modify |
---|
| 5 | it under the terms of the GNU General Public License as published by |
---|
| 6 | the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | (at your option) any later version. |
---|
| 8 | |
---|
| 9 | This program is distributed in the hope that it will be useful, |
---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | GNU General Public License for more details. |
---|
| 13 | |
---|
| 14 | You should have received a copy of the GNU General Public License |
---|
| 15 | along with this program; if not, write to the Free Software |
---|
| 16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #include "aubio_priv.h" |
---|
| 20 | #include "sample.h" |
---|
| 21 | #include "scale.h" |
---|
| 22 | #include "mathutils.h" //vec_min vec_max |
---|
| 23 | #include "hist.h" |
---|
| 24 | |
---|
| 25 | /******** |
---|
| 26 | * Object Structure |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | struct _aubio_hist_t { |
---|
| 30 | /*bug: move to a fvec */ |
---|
| 31 | smpl_t ** hist; |
---|
| 32 | uint_t nelems; |
---|
| 33 | uint_t channels; |
---|
| 34 | smpl_t * cent; |
---|
| 35 | aubio_scale_t *scaler; |
---|
| 36 | }; |
---|
| 37 | |
---|
[dde06ad] | 38 | /** |
---|
| 39 | * Object creation/deletion calls |
---|
[96fb8ad] | 40 | */ |
---|
[dde06ad] | 41 | aubio_hist_t * new_aubio_hist (smpl_t ilow, smpl_t ihig, uint_t nelems, uint_t channels){ |
---|
[96fb8ad] | 42 | aubio_hist_t * s = AUBIO_NEW(aubio_hist_t); |
---|
[dde06ad] | 43 | smpl_t step = (ihig-ilow)/(smpl_t)(nelems); |
---|
| 44 | smpl_t accum = step; |
---|
| 45 | uint_t i; |
---|
[96fb8ad] | 46 | s->channels = channels; |
---|
| 47 | s->nelems = nelems; |
---|
| 48 | s->hist = AUBIO_ARRAY(smpl_t*, channels); |
---|
| 49 | for (i=0; i< s->channels; i++) { |
---|
| 50 | s->hist[i] = AUBIO_ARRAY(smpl_t, nelems); |
---|
| 51 | } |
---|
| 52 | s->cent = AUBIO_ARRAY(smpl_t, nelems); |
---|
| 53 | |
---|
| 54 | /* use scale to map ilow/ihig -> 0/nelems */ |
---|
| 55 | s->scaler = new_aubio_scale(ilow,ihig,0,nelems); |
---|
| 56 | /* calculate centers now once */ |
---|
| 57 | s->cent[0] = ilow + 0.5 * step; |
---|
| 58 | for (i=1; i < s->nelems; i++, accum+=step ) |
---|
| 59 | s->cent[i] = s->cent[0] + accum; |
---|
| 60 | |
---|
| 61 | return s; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | void del_aubio_hist(aubio_hist_t *s) { |
---|
[dde06ad] | 65 | uint_t i; |
---|
| 66 | for (i=0; i< s->channels; i++) { |
---|
| 67 | AUBIO_FREE(s->hist[i]); |
---|
| 68 | } |
---|
| 69 | AUBIO_FREE(s->hist); |
---|
| 70 | AUBIO_FREE(s->cent); |
---|
| 71 | del_aubio_scale(s->scaler); |
---|
| 72 | AUBIO_FREE(s); |
---|
[96fb8ad] | 73 | } |
---|
| 74 | |
---|
| 75 | /*** |
---|
| 76 | * do it |
---|
| 77 | */ |
---|
| 78 | void aubio_hist_do (aubio_hist_t *s, fvec_t *input) |
---|
| 79 | { |
---|
| 80 | uint_t i,j; |
---|
[9c54265] | 81 | sint_t tmp = 0; |
---|
[96fb8ad] | 82 | aubio_scale_do(s->scaler, input); |
---|
| 83 | /* reset data */ |
---|
| 84 | for (i=0; i < s->channels; i++) |
---|
| 85 | for (j=0; j < s->nelems; j++) |
---|
| 86 | s->hist[i][j] = 0; |
---|
| 87 | /* run accum */ |
---|
| 88 | for (i=0; i < input->channels; i++) |
---|
[9c54265] | 89 | for (j=0; j < input->length; j++) |
---|
| 90 | { |
---|
| 91 | tmp = (sint_t)FLOOR(input->data[i][j]); |
---|
| 92 | if ((tmp >= 0) && (tmp < (sint_t)s->nelems)) |
---|
| 93 | s->hist[i][tmp] += 1; |
---|
| 94 | } |
---|
[96fb8ad] | 95 | } |
---|
| 96 | |
---|
| 97 | void aubio_hist_do_notnull (aubio_hist_t *s, fvec_t *input) |
---|
| 98 | { |
---|
| 99 | uint_t i,j; |
---|
[9c54265] | 100 | sint_t tmp = 0; |
---|
[96fb8ad] | 101 | aubio_scale_do(s->scaler, input); |
---|
| 102 | /* reset data */ |
---|
| 103 | for (i=0; i < s->channels; i++) |
---|
| 104 | for (j=0; j < s->nelems; j++) |
---|
| 105 | s->hist[i][j] = 0; |
---|
| 106 | /* run accum */ |
---|
| 107 | for (i=0; i < input->channels; i++) |
---|
| 108 | for (j=0; j < input->length; j++) |
---|
[9c54265] | 109 | { |
---|
| 110 | if (input->data[i][j] != 0) { |
---|
| 111 | tmp = (sint_t)FLOOR(input->data[i][j]); |
---|
| 112 | if ((tmp >= 0) && (tmp < (sint_t)s->nelems)) |
---|
| 113 | s->hist[i][tmp] += 1; |
---|
| 114 | } |
---|
| 115 | } |
---|
[96fb8ad] | 116 | } |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | void aubio_hist_dyn_notnull (aubio_hist_t *s, fvec_t *input) |
---|
| 120 | { |
---|
| 121 | uint_t i,j; |
---|
[9c54265] | 122 | sint_t tmp = 0; |
---|
[96fb8ad] | 123 | smpl_t ilow = vec_min(input); |
---|
| 124 | smpl_t ihig = vec_max(input); |
---|
| 125 | smpl_t step = (ihig-ilow)/(smpl_t)(s->nelems); |
---|
| 126 | |
---|
| 127 | /* readapt */ |
---|
| 128 | aubio_scale_set(s->scaler, ilow, ihig, 0, s->nelems); |
---|
| 129 | |
---|
| 130 | /* recalculate centers */ |
---|
| 131 | s->cent[0] = ilow + 0.5f * step; |
---|
| 132 | for (i=1; i < s->nelems; i++) |
---|
| 133 | s->cent[i] = s->cent[0] + i * step; |
---|
| 134 | |
---|
| 135 | /* scale */ |
---|
| 136 | aubio_scale_do(s->scaler, input); |
---|
| 137 | |
---|
| 138 | /* reset data */ |
---|
| 139 | for (i=0; i < s->channels; i++) |
---|
| 140 | for (j=0; j < s->nelems; j++) |
---|
| 141 | s->hist[i][j] = 0; |
---|
| 142 | /* run accum */ |
---|
| 143 | for (i=0; i < input->channels; i++) |
---|
| 144 | for (j=0; j < input->length; j++) |
---|
[9c54265] | 145 | { |
---|
| 146 | if (input->data[i][j] != 0) { |
---|
| 147 | tmp = (sint_t)FLOOR(input->data[i][j]); |
---|
| 148 | if ((tmp >= 0) && (tmp < (sint_t)s->nelems)) |
---|
| 149 | s->hist[i][tmp] += 1; |
---|
| 150 | } |
---|
| 151 | } |
---|
[96fb8ad] | 152 | } |
---|
| 153 | |
---|
| 154 | void aubio_hist_weigth (aubio_hist_t *s) |
---|
| 155 | { |
---|
| 156 | uint_t i,j; |
---|
| 157 | for (i=0; i < s->channels; i++) |
---|
| 158 | for (j=0; j < s->nelems; j++) { |
---|
| 159 | s->hist[i][j] *= s->cent[j]; |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | smpl_t aubio_hist_mean (aubio_hist_t *s) |
---|
| 164 | { |
---|
| 165 | uint_t i,j; |
---|
| 166 | smpl_t tmp = 0.0f; |
---|
| 167 | for (i=0; i < s->channels; i++) |
---|
| 168 | for (j=0; j < s->nelems; j++) |
---|
| 169 | tmp += s->hist[i][j]; |
---|
| 170 | return tmp/(smpl_t)(s->nelems); |
---|
| 171 | } |
---|
| 172 | |
---|