[96fb8ad] | 1 | /* |
---|
[a6db140] | 2 | Copyright (C) 2003-2009 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" |
---|
[714380d] | 26 | #include "config.h" |
---|
[96fb8ad] | 27 | |
---|
[eb7f743] | 28 | fvec_t * |
---|
| 29 | new_aubio_window (uint_t size, aubio_window_type wintype) |
---|
| 30 | { |
---|
[d84d19e] | 31 | // create fvec of size x 1 channel |
---|
| 32 | fvec_t * win = new_fvec( size, 1); |
---|
| 33 | smpl_t * w = win->data[0]; |
---|
[96fb8ad] | 34 | uint_t i; |
---|
| 35 | switch(wintype) { |
---|
[b4b0324] | 36 | case aubio_win_rectangle: |
---|
[96fb8ad] | 37 | for (i=0;i<size;i++) |
---|
[ade9afe] | 38 | w[i] = 0.5; |
---|
[96fb8ad] | 39 | break; |
---|
[b4b0324] | 40 | case aubio_win_hamming: |
---|
[96fb8ad] | 41 | for (i=0;i<size;i++) |
---|
| 42 | w[i] = 0.54 - 0.46 * COS(TWO_PI * i / (size)); |
---|
| 43 | break; |
---|
[b4b0324] | 44 | case aubio_win_hanning: |
---|
[96fb8ad] | 45 | for (i=0;i<size;i++) |
---|
| 46 | w[i] = 0.5 - (0.5 * COS(TWO_PI * i / (size))); |
---|
| 47 | break; |
---|
[b4b0324] | 48 | case aubio_win_hanningz: |
---|
[96fb8ad] | 49 | for (i=0;i<size;i++) |
---|
| 50 | w[i] = 0.5 * (1.0 - COS(TWO_PI * i / (size))); |
---|
| 51 | break; |
---|
[b4b0324] | 52 | case aubio_win_blackman: |
---|
[96fb8ad] | 53 | for (i=0;i<size;i++) |
---|
| 54 | w[i] = 0.42 |
---|
| 55 | - 0.50 * COS( TWO_PI*i/(size-1.0)) |
---|
[ade9afe] | 56 | + 0.08 * COS(2.0*TWO_PI*i/(size-1.0)); |
---|
[96fb8ad] | 57 | break; |
---|
[b4b0324] | 58 | case aubio_win_blackman_harris: |
---|
[96fb8ad] | 59 | for (i=0;i<size;i++) |
---|
[ade9afe] | 60 | w[i] = 0.35875 |
---|
[96fb8ad] | 61 | - 0.48829 * COS( TWO_PI*i/(size-1.0)) |
---|
| 62 | + 0.14128 * COS(2.0*TWO_PI*i/(size-1.0)) |
---|
| 63 | - 0.01168 * COS(3.0*TWO_PI*i/(size-1.0)); |
---|
| 64 | break; |
---|
[b4b0324] | 65 | case aubio_win_gaussian: |
---|
[96fb8ad] | 66 | for (i=0;i<size;i++) |
---|
| 67 | w[i] = EXP(- 1.0 / SQR(size) * SQR(2.0*i-size)); |
---|
| 68 | break; |
---|
[b4b0324] | 69 | case aubio_win_welch: |
---|
[96fb8ad] | 70 | for (i=0;i<size;i++) |
---|
| 71 | w[i] = 1.0 - SQR((2*i-size)/(size+1.0)); |
---|
| 72 | break; |
---|
[b4b0324] | 73 | case aubio_win_parzen: |
---|
[96fb8ad] | 74 | for (i=0;i<size;i++) |
---|
[8f70a18] | 75 | w[i] = 1.0 - ABS((2*i-size)/(size+1.0)); |
---|
[96fb8ad] | 76 | break; |
---|
| 77 | default: |
---|
| 78 | break; |
---|
| 79 | } |
---|
[d84d19e] | 80 | return win; |
---|
[96fb8ad] | 81 | } |
---|
| 82 | |
---|
[eb7f743] | 83 | smpl_t |
---|
| 84 | aubio_unwrap2pi (smpl_t phase) |
---|
| 85 | { |
---|
[96fb8ad] | 86 | /* mod(phase+pi,-2pi)+pi */ |
---|
[eb7f743] | 87 | return phase + TWO_PI * (1. + FLOOR (-(phase + PI) / TWO_PI)); |
---|
[96fb8ad] | 88 | } |
---|
| 89 | |
---|
[eb7f743] | 90 | smpl_t |
---|
| 91 | fvec_mean (fvec_t * s) |
---|
| 92 | { |
---|
| 93 | uint_t i, j; |
---|
[acf7d30] | 94 | smpl_t tmp = 0.0; |
---|
[eb7f743] | 95 | for (i = 0; i < s->channels; i++) |
---|
| 96 | for (j = 0; j < s->length; j++) |
---|
[96fb8ad] | 97 | tmp += s->data[i][j]; |
---|
[eb7f743] | 98 | return tmp / (smpl_t) (s->length); |
---|
[96fb8ad] | 99 | } |
---|
| 100 | |
---|
[eb7f743] | 101 | smpl_t |
---|
| 102 | fvec_sum (fvec_t * s) |
---|
| 103 | { |
---|
| 104 | uint_t i, j; |
---|
[acf7d30] | 105 | smpl_t tmp = 0.0; |
---|
[eb7f743] | 106 | for (i = 0; i < s->channels; i++) { |
---|
| 107 | for (j = 0; j < s->length; j++) { |
---|
[96fb8ad] | 108 | tmp += s->data[i][j]; |
---|
[eb7f743] | 109 | } |
---|
| 110 | } |
---|
[96fb8ad] | 111 | return tmp; |
---|
| 112 | } |
---|
| 113 | |
---|
[eb7f743] | 114 | smpl_t |
---|
| 115 | fvec_max (fvec_t * s) |
---|
| 116 | { |
---|
| 117 | uint_t i, j; |
---|
[acf7d30] | 118 | smpl_t tmp = 0.0; |
---|
[eb7f743] | 119 | for (i = 0; i < s->channels; i++) { |
---|
| 120 | for (j = 0; j < s->length; j++) { |
---|
| 121 | tmp = (tmp > s->data[i][j]) ? tmp : s->data[i][j]; |
---|
| 122 | } |
---|
| 123 | } |
---|
[96fb8ad] | 124 | return tmp; |
---|
| 125 | } |
---|
| 126 | |
---|
[eb7f743] | 127 | smpl_t |
---|
| 128 | fvec_min (fvec_t * s) |
---|
| 129 | { |
---|
| 130 | uint_t i, j; |
---|
[96fb8ad] | 131 | smpl_t tmp = s->data[0][0]; |
---|
[eb7f743] | 132 | for (i = 0; i < s->channels; i++) { |
---|
| 133 | for (j = 0; j < s->length; j++) { |
---|
| 134 | tmp = (tmp < s->data[i][j]) ? tmp : s->data[i][j]; |
---|
| 135 | } |
---|
| 136 | } |
---|
[96fb8ad] | 137 | return tmp; |
---|
| 138 | } |
---|
| 139 | |
---|
[eb7f743] | 140 | uint_t |
---|
| 141 | fvec_min_elem (fvec_t * s) |
---|
| 142 | { |
---|
[41f4c5b] | 143 | uint_t i, j, pos = 0.; |
---|
[96fb8ad] | 144 | smpl_t tmp = s->data[0][0]; |
---|
[eb7f743] | 145 | for (i = 0; i < s->channels; i++) { |
---|
| 146 | for (j = 0; j < s->length; j++) { |
---|
| 147 | pos = (tmp < s->data[i][j]) ? pos : j; |
---|
| 148 | tmp = (tmp < s->data[i][j]) ? tmp : s->data[i][j]; |
---|
[96fb8ad] | 149 | } |
---|
[eb7f743] | 150 | } |
---|
[96fb8ad] | 151 | return pos; |
---|
| 152 | } |
---|
| 153 | |
---|
[eb7f743] | 154 | uint_t |
---|
| 155 | fvec_max_elem (fvec_t * s) |
---|
| 156 | { |
---|
[41f4c5b] | 157 | uint_t i, j, pos = 0; |
---|
[acf7d30] | 158 | smpl_t tmp = 0.0; |
---|
[eb7f743] | 159 | for (i = 0; i < s->channels; i++) { |
---|
| 160 | for (j = 0; j < s->length; j++) { |
---|
| 161 | pos = (tmp > s->data[i][j]) ? pos : j; |
---|
| 162 | tmp = (tmp > s->data[i][j]) ? tmp : s->data[i][j]; |
---|
[96fb8ad] | 163 | } |
---|
[eb7f743] | 164 | } |
---|
[96fb8ad] | 165 | return pos; |
---|
| 166 | } |
---|
| 167 | |
---|
[eb7f743] | 168 | void |
---|
| 169 | fvec_shift (fvec_t * s) |
---|
| 170 | { |
---|
| 171 | uint_t i, j; |
---|
| 172 | for (i = 0; i < s->channels; i++) { |
---|
| 173 | for (j = 0; j < s->length / 2; j++) { |
---|
| 174 | ELEM_SWAP (s->data[i][j], s->data[i][j + s->length / 2]); |
---|
[96fb8ad] | 175 | } |
---|
[eb7f743] | 176 | } |
---|
[96fb8ad] | 177 | } |
---|
| 178 | |
---|
[eb7f743] | 179 | smpl_t |
---|
| 180 | fvec_local_energy (fvec_t * f) |
---|
| 181 | { |
---|
| 182 | smpl_t energy = 0.; |
---|
| 183 | uint_t i, j; |
---|
| 184 | for (i = 0; i < f->channels; i++) { |
---|
| 185 | for (j = 0; j < f->length; j++) { |
---|
| 186 | energy += SQR (f->data[i][j]); |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | return energy; |
---|
[96fb8ad] | 190 | } |
---|
| 191 | |
---|
[eb7f743] | 192 | smpl_t |
---|
| 193 | fvec_local_hfc (fvec_t * v) |
---|
| 194 | { |
---|
| 195 | smpl_t hfc = 0.; |
---|
| 196 | uint_t i, j; |
---|
| 197 | for (i = 0; i < v->channels; i++) { |
---|
| 198 | for (j = 0; j < v->length; j++) { |
---|
| 199 | hfc += (i + 1) * v->data[i][j]; |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | return hfc; |
---|
[96fb8ad] | 203 | } |
---|
| 204 | |
---|
[eb7f743] | 205 | void |
---|
| 206 | fvec_min_removal (fvec_t * v) |
---|
| 207 | { |
---|
| 208 | smpl_t v_min = fvec_min (v); |
---|
| 209 | fvec_add (v, - v_min ); |
---|
[96fb8ad] | 210 | } |
---|
| 211 | |
---|
[eb7f743] | 212 | smpl_t |
---|
| 213 | fvec_alpha_norm (fvec_t * o, smpl_t alpha) |
---|
[c0b295c] | 214 | { |
---|
| 215 | uint_t i, j; |
---|
[eb7f743] | 216 | smpl_t tmp = 0.; |
---|
[c0b295c] | 217 | for (i = 0; i < o->channels; i++) { |
---|
| 218 | for (j = 0; j < o->length; j++) { |
---|
[eb7f743] | 219 | tmp += POW (ABS (o->data[i][j]), alpha); |
---|
[96fb8ad] | 220 | } |
---|
[c0b295c] | 221 | } |
---|
[eb7f743] | 222 | return POW (tmp / o->length, 1. / alpha); |
---|
[96fb8ad] | 223 | } |
---|
| 224 | |
---|
[eb7f743] | 225 | void |
---|
| 226 | fvec_alpha_normalise (fvec_t * o, smpl_t alpha) |
---|
| 227 | { |
---|
| 228 | uint_t i, j; |
---|
| 229 | smpl_t norm = fvec_alpha_norm (o, alpha); |
---|
| 230 | for (i = 0; i < o->channels; i++) { |
---|
| 231 | for (j = 0; j < o->length; j++) { |
---|
| 232 | o->data[i][j] /= norm; |
---|
| 233 | } |
---|
[96fb8ad] | 234 | } |
---|
| 235 | } |
---|
| 236 | |
---|
[eb7f743] | 237 | void |
---|
| 238 | fvec_add (fvec_t * o, smpl_t val) |
---|
| 239 | { |
---|
| 240 | uint_t i, j; |
---|
| 241 | for (i = 0; i < o->channels; i++) { |
---|
| 242 | for (j = 0; j < o->length; j++) { |
---|
| 243 | o->data[i][j] += val; |
---|
| 244 | } |
---|
[96fb8ad] | 245 | } |
---|
| 246 | } |
---|
| 247 | |
---|
[5c4ec3c] | 248 | void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp, |
---|
[ade9afe] | 249 | uint_t post, uint_t pre) { |
---|
[96fb8ad] | 250 | uint_t length = vec->length, i=0, j; |
---|
| 251 | for (j=0;j<length;j++) { |
---|
[5c4ec3c] | 252 | vec->data[i][j] -= fvec_moving_thres(vec, tmp, post, pre, j); |
---|
[96fb8ad] | 253 | } |
---|
| 254 | } |
---|
| 255 | |
---|
[eb7f743] | 256 | smpl_t |
---|
| 257 | fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec, |
---|
| 258 | uint_t post, uint_t pre, uint_t pos) |
---|
| 259 | { |
---|
| 260 | smpl_t *medar = (smpl_t *) tmpvec->data[0]; |
---|
[96fb8ad] | 261 | uint_t k; |
---|
[eb7f743] | 262 | uint_t win_length = post + pre + 1; |
---|
| 263 | uint_t length = vec->length; |
---|
[96fb8ad] | 264 | /* post part of the buffer does not exist */ |
---|
[eb7f743] | 265 | if (pos < post + 1) { |
---|
| 266 | for (k = 0; k < post + 1 - pos; k++) |
---|
| 267 | medar[k] = 0.; /* 0-padding at the beginning */ |
---|
| 268 | for (k = post + 1 - pos; k < win_length; k++) |
---|
| 269 | medar[k] = vec->data[0][k + pos - post]; |
---|
| 270 | /* the buffer is fully defined */ |
---|
| 271 | } else if (pos + pre < length) { |
---|
| 272 | for (k = 0; k < win_length; k++) |
---|
| 273 | medar[k] = vec->data[0][k + pos - post]; |
---|
| 274 | /* pre part of the buffer does not exist */ |
---|
[96fb8ad] | 275 | } else { |
---|
[eb7f743] | 276 | for (k = 0; k < length - pos + post; k++) |
---|
| 277 | medar[k] = vec->data[0][k + pos - post]; |
---|
| 278 | for (k = length - pos + post; k < win_length; k++) |
---|
| 279 | medar[k] = 0.; /* 0-padding at the end */ |
---|
[ade9afe] | 280 | } |
---|
[eb7f743] | 281 | return fvec_median (tmpvec); |
---|
[96fb8ad] | 282 | } |
---|
| 283 | |
---|
[5c4ec3c] | 284 | smpl_t fvec_median(fvec_t * input) { |
---|
[96fb8ad] | 285 | uint_t n = input->length; |
---|
| 286 | smpl_t * arr = (smpl_t *) input->data[0]; |
---|
| 287 | uint_t low, high ; |
---|
| 288 | uint_t median; |
---|
| 289 | uint_t middle, ll, hh; |
---|
| 290 | |
---|
| 291 | low = 0 ; high = n-1 ; median = (low + high) / 2; |
---|
| 292 | for (;;) { |
---|
| 293 | if (high <= low) /* One element only */ |
---|
| 294 | return arr[median] ; |
---|
| 295 | |
---|
| 296 | if (high == low + 1) { /* Two elements only */ |
---|
| 297 | if (arr[low] > arr[high]) |
---|
| 298 | ELEM_SWAP(arr[low], arr[high]) ; |
---|
| 299 | return arr[median] ; |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | /* Find median of low, middle and high items; swap into position low */ |
---|
| 303 | middle = (low + high) / 2; |
---|
| 304 | if (arr[middle] > arr[high]) ELEM_SWAP(arr[middle], arr[high]); |
---|
| 305 | if (arr[low] > arr[high]) ELEM_SWAP(arr[low], arr[high]); |
---|
| 306 | if (arr[middle] > arr[low]) ELEM_SWAP(arr[middle], arr[low]) ; |
---|
| 307 | |
---|
| 308 | /* Swap low item (now in position middle) into position (low+1) */ |
---|
| 309 | ELEM_SWAP(arr[middle], arr[low+1]) ; |
---|
| 310 | |
---|
| 311 | /* Nibble from each end towards middle, swapping items when stuck */ |
---|
| 312 | ll = low + 1; |
---|
| 313 | hh = high; |
---|
| 314 | for (;;) { |
---|
| 315 | do ll++; while (arr[low] > arr[ll]) ; |
---|
| 316 | do hh--; while (arr[hh] > arr[low]) ; |
---|
| 317 | |
---|
| 318 | if (hh < ll) |
---|
| 319 | break; |
---|
| 320 | |
---|
| 321 | ELEM_SWAP(arr[ll], arr[hh]) ; |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | /* Swap middle item (in position low) back into correct position */ |
---|
| 325 | ELEM_SWAP(arr[low], arr[hh]) ; |
---|
| 326 | |
---|
| 327 | /* Re-set active partition */ |
---|
| 328 | if (hh <= median) |
---|
| 329 | low = ll; |
---|
| 330 | if (hh >= median) |
---|
| 331 | high = hh - 1; |
---|
| 332 | } |
---|
| 333 | } |
---|
| 334 | |
---|
[5c4ec3c] | 335 | smpl_t fvec_quadint(fvec_t * x,uint_t pos, uint_t span) { |
---|
[c5c0c98] | 336 | smpl_t s0, s1, s2; |
---|
| 337 | uint_t x0 = (pos < span) ? pos : pos - span; |
---|
| 338 | uint_t x2 = (pos + span < x->length) ? pos + span : pos; |
---|
| 339 | if (x0 == pos) return (x->data[0][pos] <= x->data[0][x2]) ? pos : x2; |
---|
| 340 | if (x2 == pos) return (x->data[0][pos] <= x->data[0][x0]) ? pos : x0; |
---|
| 341 | s0 = x->data[0][x0]; |
---|
[eb7f743] | 342 | s1 = x->data[0][pos]; |
---|
[c5c0c98] | 343 | s2 = x->data[0][x2]; |
---|
| 344 | return pos + 0.5 * (s2 - s0 ) / (s2 - 2.* s1 + s0); |
---|
[9771488] | 345 | } |
---|
| 346 | |
---|
[5c4ec3c] | 347 | uint_t fvec_peakpick(fvec_t * onset, uint_t pos) { |
---|
[ade9afe] | 348 | uint_t i=0, tmp=0; |
---|
| 349 | /*for (i=0;i<onset->channels;i++)*/ |
---|
| 350 | tmp = (onset->data[i][pos] > onset->data[i][pos-1] |
---|
| 351 | && onset->data[i][pos] > onset->data[i][pos+1] |
---|
| 352 | && onset->data[i][pos] > 0.); |
---|
| 353 | return tmp; |
---|
[96fb8ad] | 354 | } |
---|
| 355 | |
---|
[eb7f743] | 356 | smpl_t |
---|
| 357 | aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf) |
---|
| 358 | { |
---|
| 359 | smpl_t tmp = |
---|
| 360 | s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2); |
---|
| 361 | return tmp; |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | smpl_t |
---|
| 365 | aubio_freqtomidi (smpl_t freq) |
---|
| 366 | { |
---|
[ade9afe] | 367 | /* log(freq/A-2)/log(2) */ |
---|
[eb7f743] | 368 | smpl_t midi = freq / 6.875; |
---|
| 369 | midi = LOG (midi) / 0.69314718055995; |
---|
[ade9afe] | 370 | midi *= 12; |
---|
| 371 | midi -= 3; |
---|
| 372 | return midi; |
---|
[79c2e52] | 373 | } |
---|
| 374 | |
---|
[eb7f743] | 375 | smpl_t |
---|
| 376 | aubio_miditofreq (smpl_t midi) |
---|
| 377 | { |
---|
| 378 | smpl_t freq = (midi + 3.) / 12.; |
---|
| 379 | freq = EXP (freq * 0.69314718055995); |
---|
[ade9afe] | 380 | freq *= 6.875; |
---|
| 381 | return freq; |
---|
[96fb8ad] | 382 | } |
---|
| 383 | |
---|
[eb7f743] | 384 | smpl_t |
---|
| 385 | aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize) |
---|
| 386 | { |
---|
| 387 | smpl_t freq = samplerate / fftsize; |
---|
| 388 | return freq * bin; |
---|
[96fb8ad] | 389 | } |
---|
| 390 | |
---|
[eb7f743] | 391 | smpl_t |
---|
| 392 | aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize) |
---|
| 393 | { |
---|
| 394 | smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize); |
---|
| 395 | return aubio_freqtomidi (midi); |
---|
[96fb8ad] | 396 | } |
---|
| 397 | |
---|
[eb7f743] | 398 | smpl_t |
---|
| 399 | aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize) |
---|
| 400 | { |
---|
| 401 | smpl_t bin = fftsize / samplerate; |
---|
| 402 | return freq * bin; |
---|
[79c2e52] | 403 | } |
---|
| 404 | |
---|
[eb7f743] | 405 | smpl_t |
---|
| 406 | aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize) |
---|
| 407 | { |
---|
| 408 | smpl_t freq = aubio_miditofreq (midi); |
---|
| 409 | return aubio_freqtobin (freq, samplerate, fftsize); |
---|
[79c2e52] | 410 | } |
---|
| 411 | |
---|
[10a5413] | 412 | uint_t |
---|
[41f4c5b] | 413 | aubio_is_power_of_two (uint_t a) |
---|
| 414 | { |
---|
| 415 | if ((a & (a - 1)) == 0) { |
---|
[10a5413] | 416 | return 1; |
---|
| 417 | } else { |
---|
[41f4c5b] | 418 | return 0; |
---|
[10a5413] | 419 | } |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | uint_t |
---|
[41f4c5b] | 423 | aubio_next_power_of_two (uint_t a) |
---|
| 424 | { |
---|
[10a5413] | 425 | uint_t i; |
---|
| 426 | a--; |
---|
[41f4c5b] | 427 | for (i = 0; i < sizeof (uint_t) * CHAR_BIT; i++) { |
---|
[10a5413] | 428 | a = a | a >> 1; |
---|
| 429 | } |
---|
[41f4c5b] | 430 | return a + 1; |
---|
[10a5413] | 431 | } |
---|
| 432 | |
---|
[eb7f743] | 433 | smpl_t |
---|
| 434 | aubio_db_spl (fvec_t * o) |
---|
| 435 | { |
---|
| 436 | smpl_t val = SQRT (fvec_local_energy (o)); |
---|
| 437 | val /= (smpl_t) o->length; |
---|
| 438 | return LIN2DB (val); |
---|
[96fb8ad] | 439 | } |
---|
| 440 | |
---|
[eb7f743] | 441 | uint_t |
---|
| 442 | aubio_silence_detection (fvec_t * o, smpl_t threshold) |
---|
| 443 | { |
---|
| 444 | return (aubio_db_spl (o) < threshold); |
---|
| 445 | } |
---|
[96fb8ad] | 446 | |
---|
[eb7f743] | 447 | smpl_t |
---|
| 448 | aubio_level_detection (fvec_t * o, smpl_t threshold) |
---|
| 449 | { |
---|
| 450 | smpl_t db_spl = aubio_db_spl (o); |
---|
| 451 | if (db_spl < threshold) { |
---|
[ade9afe] | 452 | return 1.; |
---|
[eb7f743] | 453 | } else { |
---|
| 454 | return db_spl; |
---|
| 455 | } |
---|
[96fb8ad] | 456 | } |
---|
[a0fd4e4] | 457 | |
---|
[eb7f743] | 458 | smpl_t |
---|
| 459 | aubio_zero_crossing_rate (fvec_t * input) |
---|
| 460 | { |
---|
| 461 | uint_t i = 0, j; |
---|
[fff2bee] | 462 | uint_t zcr = 0; |
---|
[eb7f743] | 463 | for (j = 1; j < input->length; j++) { |
---|
[7e204d01] | 464 | // previous was strictly negative |
---|
[eb7f743] | 465 | if (input->data[i][j - 1] < 0.) { |
---|
[7e204d01] | 466 | // current is positive or null |
---|
[eb7f743] | 467 | if (input->data[i][j] >= 0.) { |
---|
[7e204d01] | 468 | zcr += 1; |
---|
| 469 | } |
---|
[eb7f743] | 470 | // previous was positive or null |
---|
[7e204d01] | 471 | } else { |
---|
| 472 | // current is strictly negative |
---|
[eb7f743] | 473 | if (input->data[i][j] < 0.) { |
---|
[fff2bee] | 474 | zcr += 1; |
---|
| 475 | } |
---|
| 476 | } |
---|
| 477 | } |
---|
[eb7f743] | 478 | return zcr / (smpl_t) input->length; |
---|
[fff2bee] | 479 | } |
---|
| 480 | |
---|
[eb7f743] | 481 | void |
---|
| 482 | aubio_autocorr (fvec_t * input, fvec_t * output) |
---|
| 483 | { |
---|
| 484 | uint_t i, j, k, length = input->length; |
---|
| 485 | smpl_t *data, *acf; |
---|
| 486 | smpl_t tmp = 0; |
---|
| 487 | for (k = 0; k < input->channels; k++) { |
---|
| 488 | data = input->data[k]; |
---|
| 489 | acf = output->data[k]; |
---|
| 490 | for (i = 0; i < length; i++) { |
---|
| 491 | tmp = 0.; |
---|
| 492 | for (j = i; j < length; j++) { |
---|
| 493 | tmp += data[j - i] * data[j]; |
---|
| 494 | } |
---|
| 495 | acf[i] = tmp / (smpl_t) (length - i); |
---|
[ade9afe] | 496 | } |
---|
| 497 | } |
---|
[a0fd4e4] | 498 | } |
---|
| 499 | |
---|
[eb7f743] | 500 | void |
---|
| 501 | aubio_cleanup (void) |
---|
| 502 | { |
---|
[b511fa9] | 503 | #if HAVE_FFTW3 |
---|
[eb7f743] | 504 | fftw_cleanup (); |
---|
[714380d] | 505 | #else |
---|
[b511fa9] | 506 | #if HAVE_FFTW3F |
---|
[eb7f743] | 507 | fftwf_cleanup (); |
---|
[714380d] | 508 | #endif |
---|
| 509 | #endif |
---|
| 510 | } |
---|