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