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"
36 aubio_win_blackman_harris,
40 aubio_win_default = aubio_win_hanningz,
44 new_aubio_window (char_t * window_type, uint_t length)
46 fvec_t * win = new_fvec (length);
51 err = fvec_set_window (win, window_type);
59 uint_t fvec_set_window (fvec_t *win, char_t *window_type) {
60 smpl_t * w = win->data;
61 uint_t i, size = win->length;
62 aubio_window_type wintype;
63 if (window_type == NULL) {
64 AUBIO_ERR ("window type can not be null.\n");
66 } else if (strcmp (window_type, "rectangle") == 0)
67 wintype = aubio_win_rectangle;
68 else if (strcmp (window_type, "hamming") == 0)
69 wintype = aubio_win_hamming;
70 else if (strcmp (window_type, "hanning") == 0)
71 wintype = aubio_win_hanning;
72 else if (strcmp (window_type, "hanningz") == 0)
73 wintype = aubio_win_hanningz;
74 else if (strcmp (window_type, "blackman") == 0)
75 wintype = aubio_win_blackman;
76 else if (strcmp (window_type, "blackman_harris") == 0)
77 wintype = aubio_win_blackman_harris;
78 else if (strcmp (window_type, "gaussian") == 0)
79 wintype = aubio_win_gaussian;
80 else if (strcmp (window_type, "welch") == 0)
81 wintype = aubio_win_welch;
82 else if (strcmp (window_type, "parzen") == 0)
83 wintype = aubio_win_parzen;
84 else if (strcmp (window_type, "default") == 0)
85 wintype = aubio_win_default;
87 AUBIO_ERR ("unknown window type `%s`.\n", window_type);
91 case aubio_win_rectangle:
95 case aubio_win_hamming:
97 w[i] = 0.54 - 0.46 * COS(TWO_PI * i / (size));
99 case aubio_win_hanning:
101 w[i] = 0.5 - (0.5 * COS(TWO_PI * i / (size)));
103 case aubio_win_hanningz:
105 w[i] = 0.5 * (1.0 - COS(TWO_PI * i / (size)));
107 case aubio_win_blackman:
110 - 0.50 * COS( TWO_PI*i/(size-1.0))
111 + 0.08 * COS(2.0*TWO_PI*i/(size-1.0));
113 case aubio_win_blackman_harris:
116 - 0.48829 * COS( TWO_PI*i/(size-1.0))
117 + 0.14128 * COS(2.0*TWO_PI*i/(size-1.0))
118 - 0.01168 * COS(3.0*TWO_PI*i/(size-1.0));
120 case aubio_win_gaussian:
122 lsmp_t a, b, c = 0.5;
124 for (n = 0; n < size; n++)
126 a = (n-c*(size-1))/(SQR(c)*(size-1));
132 case aubio_win_welch:
134 w[i] = 1.0 - SQR((2.*i-size)/(size+1.0));
136 case aubio_win_parzen:
138 w[i] = 1.0 - ABS((2.f*i-size)/(size+1.0f));
147 aubio_unwrap2pi (smpl_t phase)
149 /* mod(phase+pi,-2pi)+pi */
150 return phase + TWO_PI * (1. + FLOOR (-(phase + PI) / TWO_PI));
154 fvec_mean (fvec_t * s)
157 #ifndef HAVE_ACCELERATE
159 for (j = 0; j < s->length; j++) {
162 return tmp / (smpl_t) (s->length);
164 aubio_vDSP_meanv(s->data, 1, &tmp, s->length);
166 #endif /* HAVE_ACCELERATE */
170 fvec_sum (fvec_t * s)
173 #ifndef HAVE_ACCELERATE
175 for (j = 0; j < s->length; j++) {
179 aubio_vDSP_sve(s->data, 1, &tmp, s->length);
180 #endif /* HAVE_ACCELERATE */
185 fvec_max (fvec_t * s)
187 #ifndef HAVE_ACCELERATE
190 for (j = 0; j < s->length; j++) {
191 tmp = (tmp > s->data[j]) ? tmp : s->data[j];
195 aubio_vDSP_maxv(s->data, 1, &tmp, s->length);
201 fvec_min (fvec_t * s)
203 #ifndef HAVE_ACCELERATE
205 smpl_t tmp = s->data[0];
206 for (j = 0; j < s->length; j++) {
207 tmp = (tmp < s->data[j]) ? tmp : s->data[j];
211 aubio_vDSP_minv(s->data, 1, &tmp, s->length);
217 fvec_min_elem (fvec_t * s)
219 #ifndef HAVE_ACCELERATE
221 smpl_t tmp = s->data[0];
222 for (j = 0; j < s->length; j++) {
223 pos = (tmp < s->data[j]) ? pos : j;
224 tmp = (tmp < s->data[j]) ? tmp : s->data[j];
229 aubio_vDSP_minvi(s->data, 1, &tmp, (vDSP_Length *)&pos, s->length);
235 fvec_max_elem (fvec_t * s)
237 #ifndef HAVE_ACCELERATE
240 for (j = 0; j < s->length; j++) {
241 pos = (tmp > s->data[j]) ? pos : j;
242 tmp = (tmp > s->data[j]) ? tmp : s->data[j];
247 aubio_vDSP_maxvi(s->data, 1, &tmp, (vDSP_Length *)&pos, s->length);
253 fvec_shift (fvec_t * s)
255 uint_t half = s->length / 2, start = half, j;
256 // if length is odd, middle element is moved to the end
257 if (2 * half < s->length) start ++;
259 for (j = 0; j < half; j++) {
260 ELEM_SWAP (s->data[j], s->data[j + start]);
263 aubio_cblas_swap(half, s->data, 1, s->data + start, 1);
266 for (j = 0; j < half; j++) {
267 ELEM_SWAP (s->data[j + start - 1], s->data[j + start]);
273 fvec_ishift (fvec_t * s)
275 uint_t half = s->length / 2, start = half, j;
276 // if length is odd, middle element is moved to the beginning
277 if (2 * half < s->length) start ++;
279 for (j = 0; j < half; j++) {
280 ELEM_SWAP (s->data[j], s->data[j + start]);
283 aubio_cblas_swap(half, s->data, 1, s->data + start, 1);
286 for (j = 0; j < half; j++) {
287 ELEM_SWAP (s->data[half], s->data[j]);
292 void fvec_push(fvec_t *in, smpl_t new_elem) {
294 for (i = 0; i < in->length - 1; i++) {
295 in->data[i] = in->data[i + 1];
297 in->data[in->length - 1] = new_elem;
300 void fvec_clamp(fvec_t *in, smpl_t absmax) {
302 for (i = 0; i < in->length; i++) {
303 if (in->data[i] > 0 && in->data[i] > ABS(absmax)) {
304 in->data[i] = absmax;
305 } else if (in->data[i] < 0 && in->data[i] < -ABS(absmax)) {
306 in->data[i] = -absmax;
312 aubio_level_lin (const fvec_t * f)
317 for (j = 0; j < f->length; j++) {
318 energy += SQR (f->data[j]);
321 energy = aubio_cblas_dot(f->length, f->data, 1, f->data, 1);
323 return energy / f->length;
327 fvec_local_hfc (fvec_t * v)
331 for (j = 0; j < v->length; j++) {
332 hfc += (j + 1) * v->data[j];
338 fvec_min_removal (fvec_t * v)
340 smpl_t v_min = fvec_min (v);
341 fvec_add (v, - v_min );
345 fvec_alpha_norm (fvec_t * o, smpl_t alpha)
349 for (j = 0; j < o->length; j++) {
350 tmp += POW (ABS (o->data[j]), alpha);
352 return POW (tmp / o->length, 1. / alpha);
356 fvec_alpha_normalise (fvec_t * o, smpl_t alpha)
359 smpl_t norm = fvec_alpha_norm (o, alpha);
360 for (j = 0; j < o->length; j++) {
366 fvec_add (fvec_t * o, smpl_t val)
369 for (j = 0; j < o->length; j++) {
374 void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
375 uint_t post, uint_t pre) {
376 uint_t length = vec->length, j;
377 for (j=0;j<length;j++) {
378 vec->data[j] -= fvec_moving_thres(vec, tmp, post, pre, j);
383 fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec,
384 uint_t post, uint_t pre, uint_t pos)
387 smpl_t *medar = (smpl_t *) tmpvec->data;
388 uint_t win_length = post + pre + 1;
389 uint_t length = vec->length;
390 /* post part of the buffer does not exist */
391 if (pos < post + 1) {
392 for (k = 0; k < post + 1 - pos; k++)
393 medar[k] = 0.; /* 0-padding at the beginning */
394 for (k = post + 1 - pos; k < win_length; k++)
395 medar[k] = vec->data[k + pos - post];
396 /* the buffer is fully defined */
397 } else if (pos + pre < length) {
398 for (k = 0; k < win_length; k++)
399 medar[k] = vec->data[k + pos - post];
400 /* pre part of the buffer does not exist */
402 for (k = 0; k < length - pos + post; k++)
403 medar[k] = vec->data[k + pos - post];
404 for (k = length - pos + post; k < win_length; k++)
405 medar[k] = 0.; /* 0-padding at the end */
407 return fvec_median (tmpvec);
410 smpl_t fvec_median (fvec_t * input) {
411 uint_t n = input->length;
412 smpl_t * arr = (smpl_t *) input->data;
415 uint_t middle, ll, hh;
417 low = 0 ; high = n-1 ; median = (low + high) / 2;
419 if (high <= low) /* One element only */
422 if (high == low + 1) { /* Two elements only */
423 if (arr[low] > arr[high])
424 ELEM_SWAP(arr[low], arr[high]) ;
428 /* Find median of low, middle and high items; swap into position low */
429 middle = (low + high) / 2;
430 if (arr[middle] > arr[high]) ELEM_SWAP(arr[middle], arr[high]);
431 if (arr[low] > arr[high]) ELEM_SWAP(arr[low], arr[high]);
432 if (arr[middle] > arr[low]) ELEM_SWAP(arr[middle], arr[low]) ;
434 /* Swap low item (now in position middle) into position (low+1) */
435 ELEM_SWAP(arr[middle], arr[low+1]) ;
437 /* Nibble from each end towards middle, swapping items when stuck */
441 do ll++; while (arr[low] > arr[ll]) ;
442 do hh--; while (arr[hh] > arr[low]) ;
447 ELEM_SWAP(arr[ll], arr[hh]) ;
450 /* Swap middle item (in position low) back into correct position */
451 ELEM_SWAP(arr[low], arr[hh]) ;
453 /* Re-set active partition */
461 smpl_t fvec_quadratic_peak_pos (const fvec_t * x, uint_t pos) {
462 smpl_t s0, s1, s2; uint_t x0, x2;
463 smpl_t half = .5, two = 2.;
464 if (pos == 0 || pos == x->length - 1) return pos;
465 x0 = (pos < 1) ? pos : pos - 1;
466 x2 = (pos + 1 < x->length) ? pos + 1 : pos;
467 if (x0 == pos) return (x->data[pos] <= x->data[x2]) ? pos : x2;
468 if (x2 == pos) return (x->data[pos] <= x->data[x0]) ? pos : x0;
472 return pos + half * (s0 - s2 ) / (s0 - two * s1 + s2);
475 smpl_t fvec_quadratic_peak_mag (fvec_t *x, smpl_t pos) {
477 uint_t index = (uint_t)(pos - .5) + 1;
478 if (pos >= x->length || pos < 0.) return 0.;
479 if ((smpl_t)index == pos) return x->data[index];
480 x0 = x->data[index - 1];
482 x2 = x->data[index + 1];
483 return x1 - .25 * (x0 - x2) * (pos - index);
486 uint_t fvec_peakpick(const fvec_t * onset, uint_t pos) {
488 tmp = (onset->data[pos] > onset->data[pos-1]
489 && onset->data[pos] > onset->data[pos+1]
490 && onset->data[pos] > 0.);
495 aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf)
498 s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2);
503 aubio_freqtomidi (smpl_t freq)
506 if (freq < 2. || freq > 100000.) return 0.; // avoid nans and infs
507 /* log(freq/A-2)/log(2) */
509 midi = LOG (midi) / 0.69314718055995;
516 aubio_miditofreq (smpl_t midi)
519 if (midi > 140.) return 0.; // avoid infs
520 freq = (midi + 3.) / 12.;
521 freq = EXP (freq * 0.69314718055995);
527 aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
529 smpl_t freq = samplerate / fftsize;
530 return freq * MAX(bin, 0);
534 aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
536 smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize);
537 return aubio_freqtomidi (midi);
541 aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize)
543 smpl_t bin = fftsize / samplerate;
544 return MAX(freq, 0) * bin;
548 aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize)
550 smpl_t freq = aubio_miditofreq (midi);
551 return aubio_freqtobin (freq, samplerate, fftsize);
555 aubio_is_power_of_two (uint_t a)
557 if ((a & (a - 1)) == 0) {
565 aubio_next_power_of_two (uint_t a)
568 while (i < a) i <<= 1;
573 aubio_db_spl (const fvec_t * o)
575 return 10. * LOG10 (aubio_level_lin (o));
579 aubio_silence_detection (const fvec_t * o, smpl_t threshold)
581 return (aubio_db_spl (o) < threshold);
585 aubio_level_detection (const fvec_t * o, smpl_t threshold)
587 smpl_t db_spl = aubio_db_spl (o);
588 if (db_spl < threshold) {
596 aubio_zero_crossing_rate (fvec_t * input)
600 for (j = 1; j < input->length; j++) {
601 // previous was strictly negative
602 if (input->data[j - 1] < 0.) {
603 // current is positive or null
604 if (input->data[j] >= 0.) {
607 // previous was positive or null
609 // current is strictly negative
610 if (input->data[j] < 0.) {
615 return zcr / (smpl_t) input->length;
619 aubio_autocorr (const fvec_t * input, fvec_t * output)
621 uint_t i, j, length = input->length;
626 for (i = 0; i < length; i++) {
628 for (j = i; j < length; j++) {
629 tmp += data[j - i] * data[j];
631 acf[i] = tmp / (smpl_t) (length - i);