[1651b58] | 1 | /* |
---|
| 2 | Copyright (C) 2007-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 "cvec.h" |
---|
| 23 | #include "spectral/specdesc.h" |
---|
| 24 | |
---|
[029bf4e] | 25 | void aubio_specdesc_centroid (aubio_specdesc_t * o, cvec_t * spec, |
---|
| 26 | fvec_t * desc); |
---|
| 27 | void aubio_specdesc_spread (aubio_specdesc_t * o, cvec_t * spec, |
---|
| 28 | fvec_t * desc); |
---|
| 29 | void aubio_specdesc_skewness (aubio_specdesc_t * o, cvec_t * spec, |
---|
| 30 | fvec_t * desc); |
---|
| 31 | void aubio_specdesc_kurtosis (aubio_specdesc_t * o, cvec_t * spec, |
---|
| 32 | fvec_t * desc); |
---|
| 33 | void aubio_specdesc_slope (aubio_specdesc_t * o, cvec_t * spec, |
---|
| 34 | fvec_t * desc); |
---|
| 35 | void aubio_specdesc_decrease (aubio_specdesc_t * o, cvec_t * spec, |
---|
| 36 | fvec_t * desc); |
---|
| 37 | void aubio_specdesc_rolloff (aubio_specdesc_t * o, cvec_t * spec, |
---|
| 38 | fvec_t * desc); |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | smpl_t cvec_sum (cvec_t * s); |
---|
| 42 | smpl_t cvec_mean (cvec_t * s); |
---|
| 43 | smpl_t cvec_centroid (cvec_t * s); |
---|
| 44 | smpl_t cvec_moment (cvec_t * s, uint_t moment); |
---|
| 45 | |
---|
[1651b58] | 46 | smpl_t |
---|
[d95ff38] | 47 | cvec_sum (cvec_t * s) |
---|
[1651b58] | 48 | { |
---|
| 49 | uint_t j; |
---|
| 50 | smpl_t tmp = 0.0; |
---|
[d95ff38] | 51 | for (j = 0; j < s->length; j++) { |
---|
| 52 | tmp += s->norm[j]; |
---|
| 53 | } |
---|
[1651b58] | 54 | return tmp; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | smpl_t |
---|
[d95ff38] | 58 | cvec_mean (cvec_t * s) |
---|
[1651b58] | 59 | { |
---|
[d95ff38] | 60 | return cvec_sum (s) / (smpl_t) (s->length); |
---|
[1651b58] | 61 | } |
---|
| 62 | |
---|
| 63 | smpl_t |
---|
[d95ff38] | 64 | cvec_centroid (cvec_t * spec) |
---|
[1651b58] | 65 | { |
---|
| 66 | smpl_t sum = 0., sc = 0.; |
---|
| 67 | uint_t j; |
---|
[d95ff38] | 68 | sum = cvec_sum (spec); |
---|
[1651b58] | 69 | if (sum == 0.) { |
---|
| 70 | return 0.; |
---|
| 71 | } else { |
---|
| 72 | for (j = 0; j < spec->length; j++) { |
---|
[d95ff38] | 73 | sc += (smpl_t) j *spec->norm[j]; |
---|
[1651b58] | 74 | } |
---|
| 75 | return sc / sum; |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | smpl_t |
---|
[d95ff38] | 80 | cvec_moment (cvec_t * spec, uint_t order) |
---|
[1651b58] | 81 | { |
---|
| 82 | smpl_t sum = 0., centroid = 0., sc = 0.; |
---|
| 83 | uint_t j; |
---|
[d95ff38] | 84 | sum = cvec_sum (spec); |
---|
[1651b58] | 85 | if (sum == 0.) { |
---|
| 86 | return 0.; |
---|
| 87 | } else { |
---|
[d95ff38] | 88 | centroid = cvec_centroid (spec); |
---|
[1651b58] | 89 | for (j = 0; j < spec->length; j++) { |
---|
[d95ff38] | 90 | sc += (smpl_t) POW(j - centroid, order) * spec->norm[j]; |
---|
[1651b58] | 91 | } |
---|
| 92 | return sc / sum; |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | void |
---|
| 97 | aubio_specdesc_centroid (aubio_specdesc_t * o UNUSED, cvec_t * spec, |
---|
| 98 | fvec_t * desc) |
---|
| 99 | { |
---|
[d95ff38] | 100 | desc->data[0] = cvec_centroid (spec); |
---|
[1651b58] | 101 | } |
---|
| 102 | |
---|
| 103 | void |
---|
| 104 | aubio_specdesc_spread (aubio_specdesc_t * o UNUSED, cvec_t * spec, |
---|
| 105 | fvec_t * desc) |
---|
| 106 | { |
---|
[d95ff38] | 107 | desc->data[0] = cvec_moment (spec, 2); |
---|
[1651b58] | 108 | } |
---|
| 109 | |
---|
| 110 | void |
---|
| 111 | aubio_specdesc_skewness (aubio_specdesc_t * o UNUSED, cvec_t * spec, |
---|
| 112 | fvec_t * desc) |
---|
| 113 | { |
---|
[d95ff38] | 114 | smpl_t spread; |
---|
| 115 | spread = cvec_moment (spec, 2); |
---|
| 116 | if (spread == 0) { |
---|
| 117 | desc->data[0] = 0.; |
---|
| 118 | } else { |
---|
| 119 | desc->data[0] = cvec_moment (spec, 3); |
---|
| 120 | desc->data[0] /= POW ( SQRT (spread), 3); |
---|
[1651b58] | 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | void |
---|
| 125 | aubio_specdesc_kurtosis (aubio_specdesc_t * o UNUSED, cvec_t * spec, |
---|
| 126 | fvec_t * desc) |
---|
| 127 | { |
---|
[d95ff38] | 128 | smpl_t spread; |
---|
| 129 | spread = cvec_moment (spec, 2); |
---|
| 130 | if (spread == 0) { |
---|
| 131 | desc->data[0] = 0.; |
---|
| 132 | } else { |
---|
| 133 | desc->data[0] = cvec_moment (spec, 4); |
---|
| 134 | desc->data[0] /= SQR (spread); |
---|
[1651b58] | 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | void |
---|
| 139 | aubio_specdesc_slope (aubio_specdesc_t * o UNUSED, cvec_t * spec, |
---|
| 140 | fvec_t * desc) |
---|
| 141 | { |
---|
[d95ff38] | 142 | uint_t j; |
---|
[1651b58] | 143 | smpl_t norm = 0, sum = 0.; |
---|
| 144 | // compute N * sum(j**2) - sum(j)**2 |
---|
| 145 | for (j = 0; j < spec->length; j++) { |
---|
| 146 | norm += j*j; |
---|
| 147 | } |
---|
| 148 | norm *= spec->length; |
---|
| 149 | // sum_0^N(j) = length * (length + 1) / 2 |
---|
| 150 | norm -= SQR( (spec->length) * (spec->length - 1.) / 2. ); |
---|
[d95ff38] | 151 | sum = cvec_sum (spec); |
---|
| 152 | desc->data[0] = 0.; |
---|
| 153 | if (sum == 0.) { |
---|
| 154 | return; |
---|
| 155 | } else { |
---|
| 156 | for (j = 0; j < spec->length; j++) { |
---|
| 157 | desc->data[0] += j * spec->norm[j]; |
---|
[1651b58] | 158 | } |
---|
[d95ff38] | 159 | desc->data[0] *= spec->length; |
---|
| 160 | desc->data[0] -= sum * spec->length * (spec->length - 1) / 2.; |
---|
| 161 | desc->data[0] /= norm; |
---|
| 162 | desc->data[0] /= sum; |
---|
[1651b58] | 163 | } |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | void |
---|
| 167 | aubio_specdesc_decrease (aubio_specdesc_t *o UNUSED, cvec_t * spec, |
---|
| 168 | fvec_t * desc) |
---|
| 169 | { |
---|
[d95ff38] | 170 | uint_t j; smpl_t sum; |
---|
| 171 | sum = cvec_sum (spec); |
---|
| 172 | desc->data[0] = 0; |
---|
| 173 | if (sum == 0.) { |
---|
| 174 | return; |
---|
| 175 | } else { |
---|
| 176 | sum -= spec->norm[0]; |
---|
| 177 | for (j = 1; j < spec->length; j++) { |
---|
| 178 | desc->data[0] += (spec->norm[j] - spec->norm[0]) / j; |
---|
[1651b58] | 179 | } |
---|
[d95ff38] | 180 | desc->data[0] /= sum; |
---|
[1651b58] | 181 | } |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | void |
---|
| 185 | aubio_specdesc_rolloff (aubio_specdesc_t *o UNUSED, cvec_t * spec, |
---|
| 186 | fvec_t *desc) |
---|
| 187 | { |
---|
[d95ff38] | 188 | uint_t j; smpl_t cumsum, rollsum; |
---|
| 189 | cumsum = 0.; rollsum = 0.; |
---|
| 190 | for (j = 0; j < spec->length; j++) { |
---|
| 191 | cumsum += SQR (spec->norm[j]); |
---|
| 192 | } |
---|
| 193 | if (cumsum == 0) { |
---|
| 194 | desc->data[0] = 0.; |
---|
| 195 | } else { |
---|
| 196 | cumsum *= 0.95; |
---|
| 197 | j = 0; |
---|
| 198 | while (rollsum < cumsum) { |
---|
| 199 | rollsum += SQR (spec->norm[j]); |
---|
| 200 | j++; |
---|
[1651b58] | 201 | } |
---|
[d95ff38] | 202 | desc->data[0] = j; |
---|
[1651b58] | 203 | } |
---|
| 204 | } |
---|