[96fb8ad] | 1 | /* |
---|
[7380327] | 2 | Copyright (C) 2003-2014 Paul Brossier <piem@aubio.org> |
---|
[96fb8ad] | 3 | |
---|
[a6db140] | 4 | This file is part of aubio. |
---|
[96fb8ad] | 5 | |
---|
[a6db140] | 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. |
---|
[96fb8ad] | 10 | |
---|
[a6db140] | 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 | |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | /* see in mathutils.h for doc */ |
---|
| 22 | |
---|
| 23 | #include "aubio_priv.h" |
---|
[6c7d49b] | 24 | #include "fvec.h" |
---|
[96fb8ad] | 25 | #include "mathutils.h" |
---|
[83963b3] | 26 | #include "musicutils.h" |
---|
[96fb8ad] | 27 | |
---|
[407bba9] | 28 | /** Window types */ |
---|
| 29 | typedef enum |
---|
| 30 | { |
---|
[e1d9cc3] | 31 | aubio_win_ones, |
---|
[407bba9] | 32 | aubio_win_rectangle, |
---|
| 33 | aubio_win_hamming, |
---|
| 34 | aubio_win_hanning, |
---|
| 35 | aubio_win_hanningz, |
---|
| 36 | aubio_win_blackman, |
---|
| 37 | aubio_win_blackman_harris, |
---|
| 38 | aubio_win_gaussian, |
---|
| 39 | aubio_win_welch, |
---|
| 40 | aubio_win_parzen, |
---|
| 41 | aubio_win_default = aubio_win_hanningz, |
---|
| 42 | } aubio_window_type; |
---|
| 43 | |
---|
[eb7f743] | 44 | fvec_t * |
---|
[33cd81f] | 45 | new_aubio_window (char_t * window_type, uint_t length) |
---|
[eb7f743] | 46 | { |
---|
[33cd81f] | 47 | fvec_t * win = new_fvec (length); |
---|
[c21acb9] | 48 | uint_t err; |
---|
[ca45e58] | 49 | if (win == NULL) { |
---|
| 50 | return NULL; |
---|
| 51 | } |
---|
[c21acb9] | 52 | err = fvec_set_window (win, window_type); |
---|
[ca45e58] | 53 | if (err != 0) { |
---|
| 54 | del_fvec(win); |
---|
| 55 | return NULL; |
---|
| 56 | } |
---|
[33cd81f] | 57 | return win; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | uint_t fvec_set_window (fvec_t *win, char_t *window_type) { |
---|
[8e5c051] | 61 | smpl_t * w = win->data; |
---|
[33cd81f] | 62 | uint_t i, size = win->length; |
---|
[407bba9] | 63 | aubio_window_type wintype; |
---|
[33cd81f] | 64 | if (window_type == NULL) { |
---|
| 65 | AUBIO_ERR ("window type can not be null.\n"); |
---|
| 66 | return 1; |
---|
[e1d9cc3] | 67 | } else if (strcmp (window_type, "ones") == 0) |
---|
| 68 | wintype = aubio_win_ones; |
---|
| 69 | else if (strcmp (window_type, "rectangle") == 0) |
---|
[407bba9] | 70 | wintype = aubio_win_rectangle; |
---|
| 71 | else if (strcmp (window_type, "hamming") == 0) |
---|
| 72 | wintype = aubio_win_hamming; |
---|
| 73 | else if (strcmp (window_type, "hanning") == 0) |
---|
| 74 | wintype = aubio_win_hanning; |
---|
| 75 | else if (strcmp (window_type, "hanningz") == 0) |
---|
| 76 | wintype = aubio_win_hanningz; |
---|
| 77 | else if (strcmp (window_type, "blackman") == 0) |
---|
| 78 | wintype = aubio_win_blackman; |
---|
| 79 | else if (strcmp (window_type, "blackman_harris") == 0) |
---|
| 80 | wintype = aubio_win_blackman_harris; |
---|
| 81 | else if (strcmp (window_type, "gaussian") == 0) |
---|
| 82 | wintype = aubio_win_gaussian; |
---|
| 83 | else if (strcmp (window_type, "welch") == 0) |
---|
| 84 | wintype = aubio_win_welch; |
---|
| 85 | else if (strcmp (window_type, "parzen") == 0) |
---|
| 86 | wintype = aubio_win_parzen; |
---|
| 87 | else if (strcmp (window_type, "default") == 0) |
---|
| 88 | wintype = aubio_win_default; |
---|
| 89 | else { |
---|
[33cd81f] | 90 | AUBIO_ERR ("unknown window type `%s`.\n", window_type); |
---|
| 91 | return 1; |
---|
[407bba9] | 92 | } |
---|
[96fb8ad] | 93 | switch(wintype) { |
---|
[e1d9cc3] | 94 | case aubio_win_ones: |
---|
| 95 | fvec_ones(win); |
---|
| 96 | break; |
---|
[b4b0324] | 97 | case aubio_win_rectangle: |
---|
[e1d9cc3] | 98 | fvec_set_all(win, .5); |
---|
[96fb8ad] | 99 | break; |
---|
[b4b0324] | 100 | case aubio_win_hamming: |
---|
[96fb8ad] | 101 | for (i=0;i<size;i++) |
---|
| 102 | w[i] = 0.54 - 0.46 * COS(TWO_PI * i / (size)); |
---|
| 103 | break; |
---|
[b4b0324] | 104 | case aubio_win_hanning: |
---|
[96fb8ad] | 105 | for (i=0;i<size;i++) |
---|
| 106 | w[i] = 0.5 - (0.5 * COS(TWO_PI * i / (size))); |
---|
| 107 | break; |
---|
[b4b0324] | 108 | case aubio_win_hanningz: |
---|
[96fb8ad] | 109 | for (i=0;i<size;i++) |
---|
| 110 | w[i] = 0.5 * (1.0 - COS(TWO_PI * i / (size))); |
---|
| 111 | break; |
---|
[b4b0324] | 112 | case aubio_win_blackman: |
---|
[96fb8ad] | 113 | for (i=0;i<size;i++) |
---|
| 114 | w[i] = 0.42 |
---|
| 115 | - 0.50 * COS( TWO_PI*i/(size-1.0)) |
---|
[ade9afe] | 116 | + 0.08 * COS(2.0*TWO_PI*i/(size-1.0)); |
---|
[96fb8ad] | 117 | break; |
---|
[b4b0324] | 118 | case aubio_win_blackman_harris: |
---|
[96fb8ad] | 119 | for (i=0;i<size;i++) |
---|
[ade9afe] | 120 | w[i] = 0.35875 |
---|
[96fb8ad] | 121 | - 0.48829 * COS( TWO_PI*i/(size-1.0)) |
---|
| 122 | + 0.14128 * COS(2.0*TWO_PI*i/(size-1.0)) |
---|
| 123 | - 0.01168 * COS(3.0*TWO_PI*i/(size-1.0)); |
---|
| 124 | break; |
---|
[b4b0324] | 125 | case aubio_win_gaussian: |
---|
[9e56228] | 126 | { |
---|
| 127 | lsmp_t a, b, c = 0.5; |
---|
| 128 | uint_t n; |
---|
| 129 | for (n = 0; n < size; n++) |
---|
| 130 | { |
---|
| 131 | a = (n-c*(size-1))/(SQR(c)*(size-1)); |
---|
| 132 | b = -c*SQR(a); |
---|
| 133 | w[n] = EXP(b); |
---|
| 134 | } |
---|
| 135 | } |
---|
[96fb8ad] | 136 | break; |
---|
[b4b0324] | 137 | case aubio_win_welch: |
---|
[96fb8ad] | 138 | for (i=0;i<size;i++) |
---|
[f9d5346] | 139 | w[i] = 1.0 - SQR((2.*i-size)/(size+1.0)); |
---|
[96fb8ad] | 140 | break; |
---|
[b4b0324] | 141 | case aubio_win_parzen: |
---|
[96fb8ad] | 142 | for (i=0;i<size;i++) |
---|
[053495b] | 143 | w[i] = 1.0 - ABS((2.f*i-size)/(size+1.0f)); |
---|
[96fb8ad] | 144 | break; |
---|
| 145 | default: |
---|
| 146 | break; |
---|
| 147 | } |
---|
[33cd81f] | 148 | return 0; |
---|
[96fb8ad] | 149 | } |
---|
| 150 | |
---|
[eb7f743] | 151 | smpl_t |
---|
| 152 | aubio_unwrap2pi (smpl_t phase) |
---|
| 153 | { |
---|
[96fb8ad] | 154 | /* mod(phase+pi,-2pi)+pi */ |
---|
[eb7f743] | 155 | return phase + TWO_PI * (1. + FLOOR (-(phase + PI) / TWO_PI)); |
---|
[96fb8ad] | 156 | } |
---|
| 157 | |
---|
[eb7f743] | 158 | smpl_t |
---|
| 159 | fvec_mean (fvec_t * s) |
---|
| 160 | { |
---|
[56ef7e1] | 161 | smpl_t tmp = 0.0; |
---|
[986131d] | 162 | #if defined(HAVE_INTEL_IPP) |
---|
[b65230a] | 163 | aubio_ippsMean(s->data, (int)s->length, &tmp); |
---|
[4b943729] | 164 | return tmp; |
---|
[986131d] | 165 | #elif defined(HAVE_ACCELERATE) |
---|
| 166 | aubio_vDSP_meanv(s->data, 1, &tmp, s->length); |
---|
| 167 | return tmp; |
---|
| 168 | #else |
---|
[98d8d2b] | 169 | uint_t j; |
---|
[8e5c051] | 170 | for (j = 0; j < s->length; j++) { |
---|
| 171 | tmp += s->data[j]; |
---|
| 172 | } |
---|
[986131d] | 173 | return tmp / (smpl_t)(s->length); |
---|
| 174 | #endif |
---|
[56ef7e1] | 175 | } |
---|
| 176 | |
---|
| 177 | smpl_t |
---|
[eb7f743] | 178 | fvec_sum (fvec_t * s) |
---|
| 179 | { |
---|
[acf7d30] | 180 | smpl_t tmp = 0.0; |
---|
[986131d] | 181 | #if defined(HAVE_INTEL_IPP) |
---|
[b65230a] | 182 | aubio_ippsSum(s->data, (int)s->length, &tmp); |
---|
[986131d] | 183 | #elif defined(HAVE_ACCELERATE) |
---|
| 184 | aubio_vDSP_sve(s->data, 1, &tmp, s->length); |
---|
| 185 | #else |
---|
[98d8d2b] | 186 | uint_t j; |
---|
[8e5c051] | 187 | for (j = 0; j < s->length; j++) { |
---|
| 188 | tmp += s->data[j]; |
---|
[eb7f743] | 189 | } |
---|
[986131d] | 190 | #endif |
---|
[96fb8ad] | 191 | return tmp; |
---|
| 192 | } |
---|
| 193 | |
---|
[eb7f743] | 194 | smpl_t |
---|
| 195 | fvec_max (fvec_t * s) |
---|
| 196 | { |
---|
[986131d] | 197 | #if defined(HAVE_INTEL_IPP) |
---|
| 198 | smpl_t tmp = 0.; |
---|
[4b943729] | 199 | aubio_ippsMax( s->data, (int)s->length, &tmp); |
---|
[986131d] | 200 | #elif defined(HAVE_ACCELERATE) |
---|
| 201 | smpl_t tmp = 0.; |
---|
| 202 | aubio_vDSP_maxv( s->data, 1, &tmp, s->length ); |
---|
| 203 | #else |
---|
[8e5c051] | 204 | uint_t j; |
---|
[986131d] | 205 | smpl_t tmp = s->data[0]; |
---|
| 206 | for (j = 1; j < s->length; j++) { |
---|
[8e5c051] | 207 | tmp = (tmp > s->data[j]) ? tmp : s->data[j]; |
---|
[eb7f743] | 208 | } |
---|
[7c07af2] | 209 | #endif |
---|
[96fb8ad] | 210 | return tmp; |
---|
| 211 | } |
---|
| 212 | |
---|
[eb7f743] | 213 | smpl_t |
---|
| 214 | fvec_min (fvec_t * s) |
---|
| 215 | { |
---|
[986131d] | 216 | #if defined(HAVE_INTEL_IPP) |
---|
| 217 | smpl_t tmp = 0.; |
---|
[4b943729] | 218 | aubio_ippsMin(s->data, (int)s->length, &tmp); |
---|
[986131d] | 219 | #elif defined(HAVE_ACCELERATE) |
---|
| 220 | smpl_t tmp = 0.; |
---|
| 221 | aubio_vDSP_minv(s->data, 1, &tmp, s->length); |
---|
| 222 | #else |
---|
[8e5c051] | 223 | uint_t j; |
---|
| 224 | smpl_t tmp = s->data[0]; |
---|
[986131d] | 225 | for (j = 1; j < s->length; j++) { |
---|
[8e5c051] | 226 | tmp = (tmp < s->data[j]) ? tmp : s->data[j]; |
---|
[eb7f743] | 227 | } |
---|
[7c07af2] | 228 | #endif |
---|
[96fb8ad] | 229 | return tmp; |
---|
| 230 | } |
---|
| 231 | |
---|
[eb7f743] | 232 | uint_t |
---|
| 233 | fvec_min_elem (fvec_t * s) |
---|
| 234 | { |
---|
[7c07af2] | 235 | #ifndef HAVE_ACCELERATE |
---|
[8e5c051] | 236 | uint_t j, pos = 0.; |
---|
| 237 | smpl_t tmp = s->data[0]; |
---|
| 238 | for (j = 0; j < s->length; j++) { |
---|
| 239 | pos = (tmp < s->data[j]) ? pos : j; |
---|
| 240 | tmp = (tmp < s->data[j]) ? tmp : s->data[j]; |
---|
[eb7f743] | 241 | } |
---|
[7c07af2] | 242 | #else |
---|
| 243 | smpl_t tmp = 0.; |
---|
[d6befbe] | 244 | vDSP_Length pos = 0; |
---|
| 245 | aubio_vDSP_minvi(s->data, 1, &tmp, &pos, s->length); |
---|
[7c07af2] | 246 | #endif |
---|
[d6befbe] | 247 | return (uint_t)pos; |
---|
[96fb8ad] | 248 | } |
---|
| 249 | |
---|
[eb7f743] | 250 | uint_t |
---|
| 251 | fvec_max_elem (fvec_t * s) |
---|
| 252 | { |
---|
[7c07af2] | 253 | #ifndef HAVE_ACCELERATE |
---|
[8e5c051] | 254 | uint_t j, pos = 0; |
---|
[acf7d30] | 255 | smpl_t tmp = 0.0; |
---|
[8e5c051] | 256 | for (j = 0; j < s->length; j++) { |
---|
| 257 | pos = (tmp > s->data[j]) ? pos : j; |
---|
| 258 | tmp = (tmp > s->data[j]) ? tmp : s->data[j]; |
---|
[eb7f743] | 259 | } |
---|
[7c07af2] | 260 | #else |
---|
| 261 | smpl_t tmp = 0.; |
---|
[d6befbe] | 262 | vDSP_Length pos = 0; |
---|
| 263 | aubio_vDSP_maxvi(s->data, 1, &tmp, &pos, s->length); |
---|
[7c07af2] | 264 | #endif |
---|
[d6befbe] | 265 | return (uint_t)pos; |
---|
[96fb8ad] | 266 | } |
---|
| 267 | |
---|
[eb7f743] | 268 | void |
---|
| 269 | fvec_shift (fvec_t * s) |
---|
| 270 | { |
---|
[4bf3731] | 271 | uint_t half = s->length / 2, start = half, j; |
---|
[116bd1b] | 272 | // if length is odd, middle element is moved to the end |
---|
| 273 | if (2 * half < s->length) start ++; |
---|
[630191c] | 274 | #ifndef HAVE_BLAS |
---|
[4bf3731] | 275 | for (j = 0; j < half; j++) { |
---|
[116bd1b] | 276 | ELEM_SWAP (s->data[j], s->data[j + start]); |
---|
[eb7f743] | 277 | } |
---|
[8982328] | 278 | #else |
---|
[116bd1b] | 279 | aubio_cblas_swap(half, s->data, 1, s->data + start, 1); |
---|
[8982328] | 280 | #endif |
---|
[116bd1b] | 281 | if (start != half) { |
---|
[4bf3731] | 282 | for (j = 0; j < half; j++) { |
---|
[116bd1b] | 283 | ELEM_SWAP (s->data[j + start - 1], s->data[j + start]); |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | void |
---|
| 289 | fvec_ishift (fvec_t * s) |
---|
| 290 | { |
---|
[4bf3731] | 291 | uint_t half = s->length / 2, start = half, j; |
---|
[116bd1b] | 292 | // if length is odd, middle element is moved to the beginning |
---|
| 293 | if (2 * half < s->length) start ++; |
---|
[630191c] | 294 | #ifndef HAVE_BLAS |
---|
[4bf3731] | 295 | for (j = 0; j < half; j++) { |
---|
[116bd1b] | 296 | ELEM_SWAP (s->data[j], s->data[j + start]); |
---|
| 297 | } |
---|
| 298 | #else |
---|
| 299 | aubio_cblas_swap(half, s->data, 1, s->data + start, 1); |
---|
| 300 | #endif |
---|
| 301 | if (start != half) { |
---|
[4bf3731] | 302 | for (j = 0; j < half; j++) { |
---|
[116bd1b] | 303 | ELEM_SWAP (s->data[half], s->data[j]); |
---|
| 304 | } |
---|
| 305 | } |
---|
[96fb8ad] | 306 | } |
---|
| 307 | |
---|
[ee8a57c] | 308 | void fvec_push(fvec_t *in, smpl_t new_elem) { |
---|
| 309 | uint_t i; |
---|
| 310 | for (i = 0; i < in->length - 1; i++) { |
---|
| 311 | in->data[i] = in->data[i + 1]; |
---|
| 312 | } |
---|
| 313 | in->data[in->length - 1] = new_elem; |
---|
| 314 | } |
---|
| 315 | |
---|
[b799241] | 316 | void fvec_clamp(fvec_t *in, smpl_t absmax) { |
---|
| 317 | uint_t i; |
---|
| 318 | for (i = 0; i < in->length; i++) { |
---|
| 319 | if (in->data[i] > 0 && in->data[i] > ABS(absmax)) { |
---|
| 320 | in->data[i] = absmax; |
---|
| 321 | } else if (in->data[i] < 0 && in->data[i] < -ABS(absmax)) { |
---|
| 322 | in->data[i] = -absmax; |
---|
| 323 | } |
---|
| 324 | } |
---|
| 325 | } |
---|
| 326 | |
---|
[eb7f743] | 327 | smpl_t |
---|
[ad1df9b] | 328 | aubio_level_lin (const fvec_t * f) |
---|
[eb7f743] | 329 | { |
---|
| 330 | smpl_t energy = 0.; |
---|
[630191c] | 331 | #ifndef HAVE_BLAS |
---|
[8e5c051] | 332 | uint_t j; |
---|
| 333 | for (j = 0; j < f->length; j++) { |
---|
| 334 | energy += SQR (f->data[j]); |
---|
[eb7f743] | 335 | } |
---|
[8982328] | 336 | #else |
---|
| 337 | energy = aubio_cblas_dot(f->length, f->data, 1, f->data, 1); |
---|
| 338 | #endif |
---|
[1a74ac3] | 339 | return energy / f->length; |
---|
[96fb8ad] | 340 | } |
---|
| 341 | |
---|
[eb7f743] | 342 | smpl_t |
---|
| 343 | fvec_local_hfc (fvec_t * v) |
---|
| 344 | { |
---|
| 345 | smpl_t hfc = 0.; |
---|
[8e5c051] | 346 | uint_t j; |
---|
| 347 | for (j = 0; j < v->length; j++) { |
---|
| 348 | hfc += (j + 1) * v->data[j]; |
---|
[eb7f743] | 349 | } |
---|
| 350 | return hfc; |
---|
[96fb8ad] | 351 | } |
---|
| 352 | |
---|
[eb7f743] | 353 | void |
---|
| 354 | fvec_min_removal (fvec_t * v) |
---|
| 355 | { |
---|
| 356 | smpl_t v_min = fvec_min (v); |
---|
| 357 | fvec_add (v, - v_min ); |
---|
[96fb8ad] | 358 | } |
---|
| 359 | |
---|
[eb7f743] | 360 | smpl_t |
---|
| 361 | fvec_alpha_norm (fvec_t * o, smpl_t alpha) |
---|
[c0b295c] | 362 | { |
---|
[8e5c051] | 363 | uint_t j; |
---|
[eb7f743] | 364 | smpl_t tmp = 0.; |
---|
[8e5c051] | 365 | for (j = 0; j < o->length; j++) { |
---|
| 366 | tmp += POW (ABS (o->data[j]), alpha); |
---|
[c0b295c] | 367 | } |
---|
[eb7f743] | 368 | return POW (tmp / o->length, 1. / alpha); |
---|
[96fb8ad] | 369 | } |
---|
| 370 | |
---|
[eb7f743] | 371 | void |
---|
| 372 | fvec_alpha_normalise (fvec_t * o, smpl_t alpha) |
---|
| 373 | { |
---|
[8e5c051] | 374 | uint_t j; |
---|
[eb7f743] | 375 | smpl_t norm = fvec_alpha_norm (o, alpha); |
---|
[8e5c051] | 376 | for (j = 0; j < o->length; j++) { |
---|
| 377 | o->data[j] /= norm; |
---|
[96fb8ad] | 378 | } |
---|
| 379 | } |
---|
| 380 | |
---|
[eb7f743] | 381 | void |
---|
| 382 | fvec_add (fvec_t * o, smpl_t val) |
---|
| 383 | { |
---|
[8e5c051] | 384 | uint_t j; |
---|
| 385 | for (j = 0; j < o->length; j++) { |
---|
| 386 | o->data[j] += val; |
---|
[96fb8ad] | 387 | } |
---|
| 388 | } |
---|
| 389 | |
---|
[5c4ec3c] | 390 | void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp, |
---|
[8e5c051] | 391 | uint_t post, uint_t pre) { |
---|
| 392 | uint_t length = vec->length, j; |
---|
[96fb8ad] | 393 | for (j=0;j<length;j++) { |
---|
[8e5c051] | 394 | vec->data[j] -= fvec_moving_thres(vec, tmp, post, pre, j); |
---|
[96fb8ad] | 395 | } |
---|
| 396 | } |
---|
| 397 | |
---|
[eb7f743] | 398 | smpl_t |
---|
| 399 | fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec, |
---|
[8e5c051] | 400 | uint_t post, uint_t pre, uint_t pos) |
---|
[eb7f743] | 401 | { |
---|
[8e5c051] | 402 | uint_t k; |
---|
| 403 | smpl_t *medar = (smpl_t *) tmpvec->data; |
---|
[eb7f743] | 404 | uint_t win_length = post + pre + 1; |
---|
| 405 | uint_t length = vec->length; |
---|
[96fb8ad] | 406 | /* post part of the buffer does not exist */ |
---|
[eb7f743] | 407 | if (pos < post + 1) { |
---|
| 408 | for (k = 0; k < post + 1 - pos; k++) |
---|
| 409 | medar[k] = 0.; /* 0-padding at the beginning */ |
---|
| 410 | for (k = post + 1 - pos; k < win_length; k++) |
---|
[8e5c051] | 411 | medar[k] = vec->data[k + pos - post]; |
---|
[eb7f743] | 412 | /* the buffer is fully defined */ |
---|
| 413 | } else if (pos + pre < length) { |
---|
| 414 | for (k = 0; k < win_length; k++) |
---|
[8e5c051] | 415 | medar[k] = vec->data[k + pos - post]; |
---|
[eb7f743] | 416 | /* pre part of the buffer does not exist */ |
---|
[96fb8ad] | 417 | } else { |
---|
[eb7f743] | 418 | for (k = 0; k < length - pos + post; k++) |
---|
[8e5c051] | 419 | medar[k] = vec->data[k + pos - post]; |
---|
[eb7f743] | 420 | for (k = length - pos + post; k < win_length; k++) |
---|
| 421 | medar[k] = 0.; /* 0-padding at the end */ |
---|
[ade9afe] | 422 | } |
---|
[8e5c051] | 423 | return fvec_median (tmpvec); |
---|
[96fb8ad] | 424 | } |
---|
| 425 | |
---|
[8e5c051] | 426 | smpl_t fvec_median (fvec_t * input) { |
---|
[96fb8ad] | 427 | uint_t n = input->length; |
---|
[8e5c051] | 428 | smpl_t * arr = (smpl_t *) input->data; |
---|
[96fb8ad] | 429 | uint_t low, high ; |
---|
| 430 | uint_t median; |
---|
| 431 | uint_t middle, ll, hh; |
---|
| 432 | |
---|
| 433 | low = 0 ; high = n-1 ; median = (low + high) / 2; |
---|
| 434 | for (;;) { |
---|
| 435 | if (high <= low) /* One element only */ |
---|
| 436 | return arr[median] ; |
---|
| 437 | |
---|
| 438 | if (high == low + 1) { /* Two elements only */ |
---|
| 439 | if (arr[low] > arr[high]) |
---|
| 440 | ELEM_SWAP(arr[low], arr[high]) ; |
---|
| 441 | return arr[median] ; |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | /* Find median of low, middle and high items; swap into position low */ |
---|
| 445 | middle = (low + high) / 2; |
---|
| 446 | if (arr[middle] > arr[high]) ELEM_SWAP(arr[middle], arr[high]); |
---|
| 447 | if (arr[low] > arr[high]) ELEM_SWAP(arr[low], arr[high]); |
---|
| 448 | if (arr[middle] > arr[low]) ELEM_SWAP(arr[middle], arr[low]) ; |
---|
| 449 | |
---|
| 450 | /* Swap low item (now in position middle) into position (low+1) */ |
---|
| 451 | ELEM_SWAP(arr[middle], arr[low+1]) ; |
---|
| 452 | |
---|
| 453 | /* Nibble from each end towards middle, swapping items when stuck */ |
---|
| 454 | ll = low + 1; |
---|
| 455 | hh = high; |
---|
| 456 | for (;;) { |
---|
| 457 | do ll++; while (arr[low] > arr[ll]) ; |
---|
| 458 | do hh--; while (arr[hh] > arr[low]) ; |
---|
| 459 | |
---|
| 460 | if (hh < ll) |
---|
| 461 | break; |
---|
| 462 | |
---|
| 463 | ELEM_SWAP(arr[ll], arr[hh]) ; |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | /* Swap middle item (in position low) back into correct position */ |
---|
| 467 | ELEM_SWAP(arr[low], arr[hh]) ; |
---|
| 468 | |
---|
| 469 | /* Re-set active partition */ |
---|
| 470 | if (hh <= median) |
---|
| 471 | low = ll; |
---|
| 472 | if (hh >= median) |
---|
| 473 | high = hh - 1; |
---|
| 474 | } |
---|
| 475 | } |
---|
| 476 | |
---|
[ad1df9b] | 477 | smpl_t fvec_quadratic_peak_pos (const fvec_t * x, uint_t pos) { |
---|
[5644069] | 478 | smpl_t s0, s1, s2; uint_t x0, x2; |
---|
[6ab6611] | 479 | smpl_t half = .5, two = 2.; |
---|
[acd97d1] | 480 | if (pos == 0 || pos == x->length - 1) return pos; |
---|
[5644069] | 481 | x0 = (pos < 1) ? pos : pos - 1; |
---|
| 482 | x2 = (pos + 1 < x->length) ? pos + 1 : pos; |
---|
[9499eefb] | 483 | if (x0 == pos) return (x->data[pos] <= x->data[x2]) ? pos : x2; |
---|
| 484 | if (x2 == pos) return (x->data[pos] <= x->data[x0]) ? pos : x0; |
---|
| 485 | s0 = x->data[x0]; |
---|
| 486 | s1 = x->data[pos]; |
---|
| 487 | s2 = x->data[x2]; |
---|
[6ab6611] | 488 | return pos + half * (s0 - s2 ) / (s0 - two * s1 + s2); |
---|
[9499eefb] | 489 | } |
---|
| 490 | |
---|
[7380327] | 491 | smpl_t fvec_quadratic_peak_mag (fvec_t *x, smpl_t pos) { |
---|
| 492 | smpl_t x0, x1, x2; |
---|
| 493 | uint_t index = (uint_t)(pos - .5) + 1; |
---|
| 494 | if (pos >= x->length || pos < 0.) return 0.; |
---|
| 495 | if ((smpl_t)index == pos) return x->data[index]; |
---|
| 496 | x0 = x->data[index - 1]; |
---|
| 497 | x1 = x->data[index]; |
---|
| 498 | x2 = x->data[index + 1]; |
---|
| 499 | return x1 - .25 * (x0 - x2) * (pos - index); |
---|
| 500 | } |
---|
| 501 | |
---|
[ad1df9b] | 502 | uint_t fvec_peakpick(const fvec_t * onset, uint_t pos) { |
---|
[8e5c051] | 503 | uint_t tmp=0; |
---|
| 504 | tmp = (onset->data[pos] > onset->data[pos-1] |
---|
| 505 | && onset->data[pos] > onset->data[pos+1] |
---|
| 506 | && onset->data[pos] > 0.); |
---|
[ade9afe] | 507 | return tmp; |
---|
[96fb8ad] | 508 | } |
---|
| 509 | |
---|
[eb7f743] | 510 | smpl_t |
---|
| 511 | aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf) |
---|
| 512 | { |
---|
| 513 | smpl_t tmp = |
---|
| 514 | s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2); |
---|
| 515 | return tmp; |
---|
| 516 | } |
---|
| 517 | |
---|
| 518 | smpl_t |
---|
| 519 | aubio_freqtomidi (smpl_t freq) |
---|
| 520 | { |
---|
[5644069] | 521 | smpl_t midi; |
---|
[037319a] | 522 | if (freq < 2. || freq > 100000.) return 0.; // avoid nans and infs |
---|
[ade9afe] | 523 | /* log(freq/A-2)/log(2) */ |
---|
[5644069] | 524 | midi = freq / 6.875; |
---|
[5ce504d] | 525 | midi = LOG (midi) / 0.6931471805599453; |
---|
[ade9afe] | 526 | midi *= 12; |
---|
| 527 | midi -= 3; |
---|
| 528 | return midi; |
---|
[79c2e52] | 529 | } |
---|
| 530 | |
---|
[eb7f743] | 531 | smpl_t |
---|
| 532 | aubio_miditofreq (smpl_t midi) |
---|
| 533 | { |
---|
[5644069] | 534 | smpl_t freq; |
---|
[037319a] | 535 | if (midi > 140.) return 0.; // avoid infs |
---|
[5644069] | 536 | freq = (midi + 3.) / 12.; |
---|
[5ce504d] | 537 | freq = EXP (freq * 0.6931471805599453); |
---|
[ade9afe] | 538 | freq *= 6.875; |
---|
| 539 | return freq; |
---|
[96fb8ad] | 540 | } |
---|
| 541 | |
---|
[eb7f743] | 542 | smpl_t |
---|
| 543 | aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize) |
---|
| 544 | { |
---|
| 545 | smpl_t freq = samplerate / fftsize; |
---|
[c965b33] | 546 | return freq * MAX(bin, 0); |
---|
[96fb8ad] | 547 | } |
---|
| 548 | |
---|
[eb7f743] | 549 | smpl_t |
---|
| 550 | aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize) |
---|
| 551 | { |
---|
| 552 | smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize); |
---|
| 553 | return aubio_freqtomidi (midi); |
---|
[96fb8ad] | 554 | } |
---|
| 555 | |
---|
[eb7f743] | 556 | smpl_t |
---|
| 557 | aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize) |
---|
| 558 | { |
---|
| 559 | smpl_t bin = fftsize / samplerate; |
---|
[c965b33] | 560 | return MAX(freq, 0) * bin; |
---|
[79c2e52] | 561 | } |
---|
| 562 | |
---|
[eb7f743] | 563 | smpl_t |
---|
| 564 | aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize) |
---|
| 565 | { |
---|
| 566 | smpl_t freq = aubio_miditofreq (midi); |
---|
| 567 | return aubio_freqtobin (freq, samplerate, fftsize); |
---|
[79c2e52] | 568 | } |
---|
| 569 | |
---|
[10a5413] | 570 | uint_t |
---|
[41f4c5b] | 571 | aubio_is_power_of_two (uint_t a) |
---|
| 572 | { |
---|
| 573 | if ((a & (a - 1)) == 0) { |
---|
[10a5413] | 574 | return 1; |
---|
| 575 | } else { |
---|
[41f4c5b] | 576 | return 0; |
---|
[10a5413] | 577 | } |
---|
| 578 | } |
---|
| 579 | |
---|
| 580 | uint_t |
---|
[41f4c5b] | 581 | aubio_next_power_of_two (uint_t a) |
---|
| 582 | { |
---|
[7581185] | 583 | uint_t i = 1; |
---|
| 584 | while (i < a) i <<= 1; |
---|
| 585 | return i; |
---|
[10a5413] | 586 | } |
---|
| 587 | |
---|
[986131d] | 588 | uint_t |
---|
| 589 | aubio_power_of_two_order (uint_t a) |
---|
| 590 | { |
---|
[b701179] | 591 | int order = 0; |
---|
[986131d] | 592 | int temp = aubio_next_power_of_two(a); |
---|
| 593 | while (temp >>= 1) { |
---|
| 594 | ++order; |
---|
| 595 | } |
---|
| 596 | return order; |
---|
| 597 | } |
---|
| 598 | |
---|
[eb7f743] | 599 | smpl_t |
---|
[ad1df9b] | 600 | aubio_db_spl (const fvec_t * o) |
---|
[eb7f743] | 601 | { |
---|
[5b41ef9] | 602 | return 10. * LOG10 (aubio_level_lin (o)); |
---|
[96fb8ad] | 603 | } |
---|
| 604 | |
---|
[eb7f743] | 605 | uint_t |
---|
[ad1df9b] | 606 | aubio_silence_detection (const fvec_t * o, smpl_t threshold) |
---|
[eb7f743] | 607 | { |
---|
| 608 | return (aubio_db_spl (o) < threshold); |
---|
| 609 | } |
---|
[96fb8ad] | 610 | |
---|
[eb7f743] | 611 | smpl_t |
---|
[24e9b0a] | 612 | aubio_level_detection (const fvec_t * o, smpl_t threshold) |
---|
[eb7f743] | 613 | { |
---|
| 614 | smpl_t db_spl = aubio_db_spl (o); |
---|
| 615 | if (db_spl < threshold) { |
---|
[ade9afe] | 616 | return 1.; |
---|
[eb7f743] | 617 | } else { |
---|
| 618 | return db_spl; |
---|
| 619 | } |
---|
[96fb8ad] | 620 | } |
---|
[a0fd4e4] | 621 | |
---|
[eb7f743] | 622 | smpl_t |
---|
| 623 | aubio_zero_crossing_rate (fvec_t * input) |
---|
| 624 | { |
---|
[8e5c051] | 625 | uint_t j; |
---|
[fff2bee] | 626 | uint_t zcr = 0; |
---|
[eb7f743] | 627 | for (j = 1; j < input->length; j++) { |
---|
[7e204d01] | 628 | // previous was strictly negative |
---|
[8e5c051] | 629 | if (input->data[j - 1] < 0.) { |
---|
[7e204d01] | 630 | // current is positive or null |
---|
[8e5c051] | 631 | if (input->data[j] >= 0.) { |
---|
[7e204d01] | 632 | zcr += 1; |
---|
| 633 | } |
---|
[eb7f743] | 634 | // previous was positive or null |
---|
[7e204d01] | 635 | } else { |
---|
| 636 | // current is strictly negative |
---|
[8e5c051] | 637 | if (input->data[j] < 0.) { |
---|
[fff2bee] | 638 | zcr += 1; |
---|
| 639 | } |
---|
| 640 | } |
---|
| 641 | } |
---|
[eb7f743] | 642 | return zcr / (smpl_t) input->length; |
---|
[fff2bee] | 643 | } |
---|
| 644 | |
---|
[eb7f743] | 645 | void |
---|
[ad1df9b] | 646 | aubio_autocorr (const fvec_t * input, fvec_t * output) |
---|
[eb7f743] | 647 | { |
---|
[8e5c051] | 648 | uint_t i, j, length = input->length; |
---|
[eb7f743] | 649 | smpl_t *data, *acf; |
---|
| 650 | smpl_t tmp = 0; |
---|
[8e5c051] | 651 | data = input->data; |
---|
| 652 | acf = output->data; |
---|
| 653 | for (i = 0; i < length; i++) { |
---|
| 654 | tmp = 0.; |
---|
| 655 | for (j = i; j < length; j++) { |
---|
| 656 | tmp += data[j - i] * data[j]; |
---|
[ade9afe] | 657 | } |
---|
[8e5c051] | 658 | acf[i] = tmp / (smpl_t) (length - i); |
---|
[ade9afe] | 659 | } |
---|
[a0fd4e4] | 660 | } |
---|
| 661 | |
---|
[eb7f743] | 662 | void |
---|
| 663 | aubio_cleanup (void) |
---|
| 664 | { |
---|
[729a3c0] | 665 | #ifdef HAVE_FFTW3F |
---|
[eb7f743] | 666 | fftwf_cleanup (); |
---|
[729a3c0] | 667 | #else |
---|
| 668 | #ifdef HAVE_FFTW3 |
---|
| 669 | fftw_cleanup (); |
---|
[714380d] | 670 | #endif |
---|
| 671 | #endif |
---|
| 672 | } |
---|