[96fb8ad] | 1 | /* |
---|
[e6a78ea] | 2 | Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org> |
---|
[96fb8ad] | 3 | |
---|
[e6a78ea] | 4 | This file is part of aubio. |
---|
[96fb8ad] | 5 | |
---|
[e6a78ea] | 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/>. |
---|
[96fb8ad] | 18 | |
---|
[3c707f7] | 19 | */ |
---|
[96fb8ad] | 20 | |
---|
| 21 | #include "aubio_priv.h" |
---|
[6c7d49b] | 22 | #include "fvec.h" |
---|
[32d6958] | 23 | #include "utils/scale.h" |
---|
[2f64b0e] | 24 | #include "mathutils.h" //fvec_min fvec_max |
---|
[32d6958] | 25 | #include "utils/hist.h" |
---|
[96fb8ad] | 26 | |
---|
| 27 | /******** |
---|
| 28 | * Object Structure |
---|
| 29 | */ |
---|
| 30 | |
---|
| 31 | struct _aubio_hist_t { |
---|
[3c707f7] | 32 | fvec_t * hist; |
---|
| 33 | uint_t nelems; |
---|
| 34 | uint_t channels; |
---|
| 35 | fvec_t * cent; |
---|
| 36 | aubio_scale_t *scaler; |
---|
[96fb8ad] | 37 | }; |
---|
| 38 | |
---|
[dde06ad] | 39 | /** |
---|
| 40 | * Object creation/deletion calls |
---|
[96fb8ad] | 41 | */ |
---|
[dde06ad] | 42 | aubio_hist_t * new_aubio_hist (smpl_t ilow, smpl_t ihig, uint_t nelems, uint_t channels){ |
---|
[3c707f7] | 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; |
---|
[274839f] | 49 | s->hist = new_fvec(nelems, channels); |
---|
[3c707f7] | 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; |
---|
[96fb8ad] | 60 | } |
---|
| 61 | |
---|
| 62 | void del_aubio_hist(aubio_hist_t *s) { |
---|
[274839f] | 63 | del_fvec(s->hist); |
---|
[3c707f7] | 64 | del_fvec(s->cent); |
---|
| 65 | del_aubio_scale(s->scaler); |
---|
| 66 | AUBIO_FREE(s); |
---|
[96fb8ad] | 67 | } |
---|
| 68 | |
---|
| 69 | /*** |
---|
| 70 | * do it |
---|
| 71 | */ |
---|
[3c707f7] | 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 | } |
---|
[96fb8ad] | 88 | } |
---|
| 89 | |
---|
[3c707f7] | 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 | } |
---|
[96fb8ad] | 107 | } |
---|
| 108 | |
---|
| 109 | |
---|
[3c707f7] | 110 | void aubio_hist_dyn_notnull (aubio_hist_t *s, fvec_t *input) { |
---|
| 111 | uint_t i,j; |
---|
| 112 | sint_t tmp = 0; |
---|
[2f64b0e] | 113 | smpl_t ilow = fvec_min(input); |
---|
[1e2c82f] | 114 | smpl_t ihig = fvec_max(input); |
---|
[3c707f7] | 115 | smpl_t step = (ihig-ilow)/(smpl_t)(s->nelems); |
---|
| 116 | |
---|
| 117 | /* readapt */ |
---|
[0ce97483] | 118 | aubio_scale_set_limits (s->scaler, ilow, ihig, 0, s->nelems); |
---|
[3c707f7] | 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 | } |
---|
[96fb8ad] | 141 | } |
---|
| 142 | |
---|
[3c707f7] | 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 | } |
---|
[96fb8ad] | 149 | } |
---|
| 150 | |
---|
[3c707f7] | 151 | smpl_t aubio_hist_mean (aubio_hist_t *s) { |
---|
| 152 | uint_t i,j; |
---|
[acf7d30] | 153 | smpl_t tmp = 0.0; |
---|
[3c707f7] | 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); |
---|
[96fb8ad] | 158 | } |
---|
| 159 | |
---|