2 Copyright (C) 2003-2014 Paul Brossier <piem@aubio.org>
4 This file is part of aubio.
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.
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.
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/>.
21 /* see in mathutils.h for doc */
23 #include "aubio_priv.h"
25 #include "mathutils.h"
26 #include "musicutils.h"
37 aubio_win_blackman_harris,
41 aubio_win_default = aubio_win_hanningz,
45 new_aubio_window (char_t * window_type, uint_t length)
47 fvec_t * win = new_fvec (length);
52 err = fvec_set_window (win, window_type);
60 uint_t fvec_set_window (fvec_t *win, char_t *window_type) {
61 smpl_t * w = win->data;
62 uint_t i, size = win->length;
63 aubio_window_type wintype;
64 if (window_type == NULL) {
65 AUBIO_ERR ("window type can not be null.\n");
67 } else if (strcmp (window_type, "rectangle") == 0)
68 wintype = aubio_win_rectangle;
69 else if (strcmp (window_type, "hamming") == 0)
70 wintype = aubio_win_hamming;
71 else if (strcmp (window_type, "hanning") == 0)
72 wintype = aubio_win_hanning;
73 else if (strcmp (window_type, "hanningz") == 0)
74 wintype = aubio_win_hanningz;
75 else if (strcmp (window_type, "blackman") == 0)
76 wintype = aubio_win_blackman;
77 else if (strcmp (window_type, "blackman_harris") == 0)
78 wintype = aubio_win_blackman_harris;
79 else if (strcmp (window_type, "gaussian") == 0)
80 wintype = aubio_win_gaussian;
81 else if (strcmp (window_type, "welch") == 0)
82 wintype = aubio_win_welch;
83 else if (strcmp (window_type, "parzen") == 0)
84 wintype = aubio_win_parzen;
85 else if (strcmp (window_type, "default") == 0)
86 wintype = aubio_win_default;
88 AUBIO_ERR ("unknown window type `%s`.\n", window_type);
92 case aubio_win_rectangle:
96 case aubio_win_hamming:
98 w[i] = 0.54 - 0.46 * COS(TWO_PI * i / (size));
100 case aubio_win_hanning:
102 w[i] = 0.5 - (0.5 * COS(TWO_PI * i / (size)));
104 case aubio_win_hanningz:
106 w[i] = 0.5 * (1.0 - COS(TWO_PI * i / (size)));
108 case aubio_win_blackman:
111 - 0.50 * COS( TWO_PI*i/(size-1.0))
112 + 0.08 * COS(2.0*TWO_PI*i/(size-1.0));
114 case aubio_win_blackman_harris:
117 - 0.48829 * COS( TWO_PI*i/(size-1.0))
118 + 0.14128 * COS(2.0*TWO_PI*i/(size-1.0))
119 - 0.01168 * COS(3.0*TWO_PI*i/(size-1.0));
121 case aubio_win_gaussian:
123 lsmp_t a, b, c = 0.5;
125 for (n = 0; n < size; n++)
127 a = (n-c*(size-1))/(SQR(c)*(size-1));
133 case aubio_win_welch:
135 w[i] = 1.0 - SQR((2.*i-size)/(size+1.0));
137 case aubio_win_parzen:
139 w[i] = 1.0 - ABS((2.f*i-size)/(size+1.0f));
148 aubio_unwrap2pi (smpl_t phase)
150 /* mod(phase+pi,-2pi)+pi */
151 return phase + TWO_PI * (1. + FLOOR (-(phase + PI) / TWO_PI));
155 fvec_mean (fvec_t * s)
158 #ifndef HAVE_ACCELERATE
160 for (j = 0; j < s->length; j++) {
163 return tmp / (smpl_t) (s->length);
165 aubio_vDSP_meanv(s->data, 1, &tmp, s->length);
167 #endif /* HAVE_ACCELERATE */
171 fvec_sum (fvec_t * s)
174 #ifndef HAVE_ACCELERATE
176 for (j = 0; j < s->length; j++) {
180 aubio_vDSP_sve(s->data, 1, &tmp, s->length);
181 #endif /* HAVE_ACCELERATE */
186 fvec_max (fvec_t * s)
188 #ifndef HAVE_ACCELERATE
191 for (j = 0; j < s->length; j++) {
192 tmp = (tmp > s->data[j]) ? tmp : s->data[j];
196 aubio_vDSP_maxv(s->data, 1, &tmp, s->length);
202 fvec_min (fvec_t * s)
204 #ifndef HAVE_ACCELERATE
206 smpl_t tmp = s->data[0];
207 for (j = 0; j < s->length; j++) {
208 tmp = (tmp < s->data[j]) ? tmp : s->data[j];
212 aubio_vDSP_minv(s->data, 1, &tmp, s->length);
218 fvec_min_elem (fvec_t * s)
220 #ifndef HAVE_ACCELERATE
222 smpl_t tmp = s->data[0];
223 for (j = 0; j < s->length; j++) {
224 pos = (tmp < s->data[j]) ? pos : j;
225 tmp = (tmp < s->data[j]) ? tmp : s->data[j];
230 aubio_vDSP_minvi(s->data, 1, &tmp, (vDSP_Length *)&pos, s->length);
236 fvec_max_elem (fvec_t * s)
238 #ifndef HAVE_ACCELERATE
241 for (j = 0; j < s->length; j++) {
242 pos = (tmp > s->data[j]) ? pos : j;
243 tmp = (tmp > s->data[j]) ? tmp : s->data[j];
248 aubio_vDSP_maxvi(s->data, 1, &tmp, (vDSP_Length *)&pos, s->length);
254 fvec_shift (fvec_t * s)
256 uint_t half = s->length / 2, start = half, j;
257 // if length is odd, middle element is moved to the end
258 if (2 * half < s->length) start ++;
260 for (j = 0; j < half; j++) {
261 ELEM_SWAP (s->data[j], s->data[j + start]);
264 aubio_cblas_swap(half, s->data, 1, s->data + start, 1);
267 for (j = 0; j < half; j++) {
268 ELEM_SWAP (s->data[j + start - 1], s->data[j + start]);
274 fvec_ishift (fvec_t * s)
276 uint_t half = s->length / 2, start = half, j;
277 // if length is odd, middle element is moved to the beginning
278 if (2 * half < s->length) start ++;
280 for (j = 0; j < half; j++) {
281 ELEM_SWAP (s->data[j], s->data[j + start]);
284 aubio_cblas_swap(half, s->data, 1, s->data + start, 1);
287 for (j = 0; j < half; j++) {
288 ELEM_SWAP (s->data[half], s->data[j]);
294 aubio_level_lin (const fvec_t * f)
299 for (j = 0; j < f->length; j++) {
300 energy += SQR (f->data[j]);
303 energy = aubio_cblas_dot(f->length, f->data, 1, f->data, 1);
305 return energy / f->length;
309 fvec_local_hfc (fvec_t * v)
313 for (j = 0; j < v->length; j++) {
314 hfc += (j + 1) * v->data[j];
320 fvec_min_removal (fvec_t * v)
322 smpl_t v_min = fvec_min (v);
323 fvec_add (v, - v_min );
327 fvec_alpha_norm (fvec_t * o, smpl_t alpha)
331 for (j = 0; j < o->length; j++) {
332 tmp += POW (ABS (o->data[j]), alpha);
334 return POW (tmp / o->length, 1. / alpha);
338 fvec_alpha_normalise (fvec_t * o, smpl_t alpha)
341 smpl_t norm = fvec_alpha_norm (o, alpha);
342 for (j = 0; j < o->length; j++) {
348 fvec_add (fvec_t * o, smpl_t val)
351 for (j = 0; j < o->length; j++) {
356 void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
357 uint_t post, uint_t pre) {
358 uint_t length = vec->length, j;
359 for (j=0;j<length;j++) {
360 vec->data[j] -= fvec_moving_thres(vec, tmp, post, pre, j);
365 fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec,
366 uint_t post, uint_t pre, uint_t pos)
369 smpl_t *medar = (smpl_t *) tmpvec->data;
370 uint_t win_length = post + pre + 1;
371 uint_t length = vec->length;
372 /* post part of the buffer does not exist */
373 if (pos < post + 1) {
374 for (k = 0; k < post + 1 - pos; k++)
375 medar[k] = 0.; /* 0-padding at the beginning */
376 for (k = post + 1 - pos; k < win_length; k++)
377 medar[k] = vec->data[k + pos - post];
378 /* the buffer is fully defined */
379 } else if (pos + pre < length) {
380 for (k = 0; k < win_length; k++)
381 medar[k] = vec->data[k + pos - post];
382 /* pre part of the buffer does not exist */
384 for (k = 0; k < length - pos + post; k++)
385 medar[k] = vec->data[k + pos - post];
386 for (k = length - pos + post; k < win_length; k++)
387 medar[k] = 0.; /* 0-padding at the end */
389 return fvec_median (tmpvec);
392 smpl_t fvec_median (fvec_t * input) {
393 uint_t n = input->length;
394 smpl_t * arr = (smpl_t *) input->data;
397 uint_t middle, ll, hh;
399 low = 0 ; high = n-1 ; median = (low + high) / 2;
401 if (high <= low) /* One element only */
404 if (high == low + 1) { /* Two elements only */
405 if (arr[low] > arr[high])
406 ELEM_SWAP(arr[low], arr[high]) ;
410 /* Find median of low, middle and high items; swap into position low */
411 middle = (low + high) / 2;
412 if (arr[middle] > arr[high]) ELEM_SWAP(arr[middle], arr[high]);
413 if (arr[low] > arr[high]) ELEM_SWAP(arr[low], arr[high]);
414 if (arr[middle] > arr[low]) ELEM_SWAP(arr[middle], arr[low]) ;
416 /* Swap low item (now in position middle) into position (low+1) */
417 ELEM_SWAP(arr[middle], arr[low+1]) ;
419 /* Nibble from each end towards middle, swapping items when stuck */
423 do ll++; while (arr[low] > arr[ll]) ;
424 do hh--; while (arr[hh] > arr[low]) ;
429 ELEM_SWAP(arr[ll], arr[hh]) ;
432 /* Swap middle item (in position low) back into correct position */
433 ELEM_SWAP(arr[low], arr[hh]) ;
435 /* Re-set active partition */
443 smpl_t fvec_quadratic_peak_pos (const fvec_t * x, uint_t pos) {
444 smpl_t s0, s1, s2; uint_t x0, x2;
445 smpl_t half = .5, two = 2.;
446 if (pos == 0 || pos == x->length - 1) return pos;
447 x0 = (pos < 1) ? pos : pos - 1;
448 x2 = (pos + 1 < x->length) ? pos + 1 : pos;
449 if (x0 == pos) return (x->data[pos] <= x->data[x2]) ? pos : x2;
450 if (x2 == pos) return (x->data[pos] <= x->data[x0]) ? pos : x0;
454 return pos + half * (s0 - s2 ) / (s0 - two * s1 + s2);
457 smpl_t fvec_quadratic_peak_mag (fvec_t *x, smpl_t pos) {
459 uint_t index = (uint_t)(pos - .5) + 1;
460 if (pos >= x->length || pos < 0.) return 0.;
461 if ((smpl_t)index == pos) return x->data[index];
462 x0 = x->data[index - 1];
464 x2 = x->data[index + 1];
465 return x1 - .25 * (x0 - x2) * (pos - index);
468 uint_t fvec_peakpick(const fvec_t * onset, uint_t pos) {
470 tmp = (onset->data[pos] > onset->data[pos-1]
471 && onset->data[pos] > onset->data[pos+1]
472 && onset->data[pos] > 0.);
477 aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf)
480 s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2);
485 aubio_freqtomidi (smpl_t freq)
488 if (freq < 2. || freq > 100000.) return 0.; // avoid nans and infs
489 /* log(freq/A-2)/log(2) */
491 midi = LOG (midi) / 0.69314718055995;
498 aubio_miditofreq (smpl_t midi)
501 if (midi > 140.) return 0.; // avoid infs
502 freq = (midi + 3.) / 12.;
503 freq = EXP (freq * 0.69314718055995);
509 aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
511 smpl_t freq = samplerate / fftsize;
512 return freq * MAX(bin, 0);
516 aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
518 smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize);
519 return aubio_freqtomidi (midi);
523 aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize)
525 smpl_t bin = fftsize / samplerate;
526 return MAX(freq, 0) * bin;
530 aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize)
532 smpl_t freq = aubio_miditofreq (midi);
533 return aubio_freqtobin (freq, samplerate, fftsize);
537 aubio_is_power_of_two (uint_t a)
539 if ((a & (a - 1)) == 0) {
547 aubio_next_power_of_two (uint_t a)
550 while (i < a) i <<= 1;
555 aubio_db_spl (const fvec_t * o)
557 return 10. * LOG10 (aubio_level_lin (o));
561 aubio_silence_detection (const fvec_t * o, smpl_t threshold)
563 return (aubio_db_spl (o) < threshold);
567 aubio_level_detection (const fvec_t * o, smpl_t threshold)
569 smpl_t db_spl = aubio_db_spl (o);
570 if (db_spl < threshold) {
578 aubio_zero_crossing_rate (fvec_t * input)
582 for (j = 1; j < input->length; j++) {
583 // previous was strictly negative
584 if (input->data[j - 1] < 0.) {
585 // current is positive or null
586 if (input->data[j] >= 0.) {
589 // previous was positive or null
591 // current is strictly negative
592 if (input->data[j] < 0.) {
597 return zcr / (smpl_t) input->length;
601 aubio_autocorr (const fvec_t * input, fvec_t * output)
603 uint_t i, j, length = input->length;
608 for (i = 0; i < length; i++) {
610 for (j = i; j < length; j++) {
611 tmp += data[j - i] * data[j];
613 acf[i] = tmp / (smpl_t) (length - i);