[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 | |
---|
| 38 | |
---|
| 39 | /******************************************************** |
---|
| 40 | * Object Memory Allocation |
---|
| 41 | */ |
---|
| 42 | |
---|
| 43 | static aubio_hist_t * aubio_hist_malloc(uint_t channels, uint_t nelems); |
---|
| 44 | static void aubio_hist_free(aubio_hist_t * s); |
---|
| 45 | |
---|
| 46 | aubio_hist_t * aubio_hist_malloc(uint_t channels, uint_t nelems) { |
---|
| 47 | uint_t i; |
---|
| 48 | aubio_hist_t * s = AUBIO_NEW(aubio_hist_t); |
---|
| 49 | s->channels = channels; |
---|
| 50 | s->nelems = nelems; |
---|
| 51 | |
---|
| 52 | s->hist = AUBIO_ARRAY(smpl_t*, channels); |
---|
| 53 | for (i=0; i< s->channels; i++) { |
---|
| 54 | s->hist[i] = AUBIO_ARRAY(smpl_t, nelems); |
---|
| 55 | } |
---|
| 56 | s->cent = AUBIO_ARRAY(smpl_t, nelems); |
---|
| 57 | return s; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | void aubio_hist_free(aubio_hist_t * s) { |
---|
| 61 | AUBIO_FREE(s); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | /*** |
---|
| 65 | * Object creation/deletion calls |
---|
| 66 | */ |
---|
| 67 | aubio_hist_t * new_aubio_hist (smpl_t ilow, smpl_t ihig, uint_t nelems, uint_t channels){ |
---|
| 68 | smpl_t step = (ihig-ilow)/(smpl_t)(nelems); |
---|
| 69 | smpl_t accum = step; |
---|
| 70 | uint_t i; |
---|
| 71 | aubio_hist_t * s = aubio_hist_malloc(channels, nelems); |
---|
| 72 | |
---|
| 73 | /* use scale to map ilow/ihig -> 0/nelems */ |
---|
| 74 | s->scaler = new_aubio_scale(ilow,ihig,0,nelems); |
---|
| 75 | /* calculate centers now once */ |
---|
| 76 | s->cent[0] = ilow + 0.5 * step; |
---|
| 77 | for (i=1; i < s->nelems; i++, accum+=step ) |
---|
| 78 | s->cent[i] = s->cent[0] + accum; |
---|
| 79 | |
---|
| 80 | return s; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | void del_aubio_hist(aubio_hist_t *s) { |
---|
| 84 | aubio_hist_free(s); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /*** |
---|
| 88 | * do it |
---|
| 89 | */ |
---|
| 90 | void aubio_hist_do (aubio_hist_t *s, fvec_t *input) |
---|
| 91 | { |
---|
| 92 | uint_t i,j; |
---|
| 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[i][j] = 0; |
---|
| 98 | /* run accum */ |
---|
| 99 | for (i=0; i < input->channels; i++) |
---|
| 100 | for (j=0; j < input->length; j++) |
---|
| 101 | s->hist[i][(uint_t)floor(input->data[i][j])] += 1; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | void aubio_hist_do_notnull (aubio_hist_t *s, fvec_t *input) |
---|
| 105 | { |
---|
| 106 | uint_t i,j; |
---|
| 107 | aubio_scale_do(s->scaler, input); |
---|
| 108 | /* reset data */ |
---|
| 109 | for (i=0; i < s->channels; i++) |
---|
| 110 | for (j=0; j < s->nelems; j++) |
---|
| 111 | s->hist[i][j] = 0; |
---|
| 112 | /* run accum */ |
---|
| 113 | for (i=0; i < input->channels; i++) |
---|
| 114 | for (j=0; j < input->length; j++) |
---|
| 115 | if (input->data[i][j]!=0.0f) //input is not 0 |
---|
| 116 | s->hist[i][(uint_t)floor(input->data[i][j])] += 1; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | void aubio_hist_dyn_notnull (aubio_hist_t *s, fvec_t *input) |
---|
| 121 | { |
---|
| 122 | uint_t i,j; |
---|
| 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++) |
---|
| 145 | if (input->data[i][j]!=0.) //input was not 0 |
---|
| 146 | s->hist[i][(uint_t)floorf(input->data[i][j])] += 1; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | void aubio_hist_weigth (aubio_hist_t *s) |
---|
| 150 | { |
---|
| 151 | uint_t i,j; |
---|
| 152 | for (i=0; i < s->channels; i++) |
---|
| 153 | for (j=0; j < s->nelems; j++) { |
---|
| 154 | s->hist[i][j] *= s->cent[j]; |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | smpl_t aubio_hist_mean (aubio_hist_t *s) |
---|
| 159 | { |
---|
| 160 | uint_t i,j; |
---|
| 161 | smpl_t tmp = 0.0f; |
---|
| 162 | for (i=0; i < s->channels; i++) |
---|
| 163 | for (j=0; j < s->nelems; j++) |
---|
| 164 | tmp += s->hist[i][j]; |
---|
| 165 | return tmp/(smpl_t)(s->nelems); |
---|
| 166 | } |
---|
| 167 | |
---|