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, "ones") == 0)
68 wintype = aubio_win_ones;
69 else if (strcmp (window_type, "rectangle") == 0)
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;
90 AUBIO_ERR ("unknown window type `%s`.\n", window_type);
97 case aubio_win_rectangle:
98 fvec_set_all(win, .5);
100 case aubio_win_hamming:
102 w[i] = 0.54 - 0.46 * COS(TWO_PI * i / (size));
104 case aubio_win_hanning:
106 w[i] = 0.5 - (0.5 * COS(TWO_PI * i / (size)));
108 case aubio_win_hanningz:
110 w[i] = 0.5 * (1.0 - COS(TWO_PI * i / (size)));
112 case aubio_win_blackman:
115 - 0.50 * COS( TWO_PI*i/(size-1.0))
116 + 0.08 * COS(2.0*TWO_PI*i/(size-1.0));
118 case aubio_win_blackman_harris:
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));
125 case aubio_win_gaussian:
127 lsmp_t a, b, c = 0.5;
129 for (n = 0; n < size; n++)
131 a = (n-c*(size-1))/(SQR(c)*(size-1));
137 case aubio_win_welch:
139 w[i] = 1.0 - SQR((2.*i-size)/(size+1.0));
141 case aubio_win_parzen:
143 w[i] = 1.0 - ABS((2.f*i-size)/(size+1.0f));
152 aubio_unwrap2pi (smpl_t phase)
154 /* mod(phase+pi,-2pi)+pi */
155 return phase + TWO_PI * (1. + FLOOR (-(phase + PI) / TWO_PI));
159 fvec_mean (fvec_t * s)
162 #if defined(HAVE_INTEL_IPP)
163 aubio_ippsMean(s->data, (int)s->length, &tmp);
165 #elif defined(HAVE_ACCELERATE)
166 aubio_vDSP_meanv(s->data, 1, &tmp, s->length);
170 for (j = 0; j < s->length; j++) {
173 return tmp / (smpl_t)(s->length);
178 fvec_sum (fvec_t * s)
181 #if defined(HAVE_INTEL_IPP)
182 aubio_ippsSum(s->data, (int)s->length, &tmp);
183 #elif defined(HAVE_ACCELERATE)
184 aubio_vDSP_sve(s->data, 1, &tmp, s->length);
187 for (j = 0; j < s->length; j++) {
195 fvec_max (fvec_t * s)
197 #if defined(HAVE_INTEL_IPP)
199 aubio_ippsMax( s->data, (int)s->length, &tmp);
200 #elif defined(HAVE_ACCELERATE)
202 aubio_vDSP_maxv( s->data, 1, &tmp, s->length );
205 smpl_t tmp = s->data[0];
206 for (j = 1; j < s->length; j++) {
207 tmp = (tmp > s->data[j]) ? tmp : s->data[j];
214 fvec_min (fvec_t * s)
216 #if defined(HAVE_INTEL_IPP)
218 aubio_ippsMin(s->data, (int)s->length, &tmp);
219 #elif defined(HAVE_ACCELERATE)
221 aubio_vDSP_minv(s->data, 1, &tmp, s->length);
224 smpl_t tmp = s->data[0];
225 for (j = 1; j < s->length; j++) {
226 tmp = (tmp < s->data[j]) ? tmp : s->data[j];
233 fvec_min_elem (fvec_t * s)
235 #ifndef HAVE_ACCELERATE
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];
245 aubio_vDSP_minvi(s->data, 1, &tmp, &pos, s->length);
251 fvec_max_elem (fvec_t * s)
253 #ifndef HAVE_ACCELERATE
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];
263 aubio_vDSP_maxvi(s->data, 1, &tmp, &pos, s->length);
269 fvec_shift (fvec_t * s)
271 uint_t half = s->length / 2, start = half, j;
272 // if length is odd, middle element is moved to the end
273 if (2 * half < s->length) start ++;
275 for (j = 0; j < half; j++) {
276 ELEM_SWAP (s->data[j], s->data[j + start]);
279 aubio_cblas_swap(half, s->data, 1, s->data + start, 1);
282 for (j = 0; j < half; j++) {
283 ELEM_SWAP (s->data[j + start - 1], s->data[j + start]);
289 fvec_ishift (fvec_t * s)
291 uint_t half = s->length / 2, start = half, j;
292 // if length is odd, middle element is moved to the beginning
293 if (2 * half < s->length) start ++;
295 for (j = 0; j < half; j++) {
296 ELEM_SWAP (s->data[j], s->data[j + start]);
299 aubio_cblas_swap(half, s->data, 1, s->data + start, 1);
302 for (j = 0; j < half; j++) {
303 ELEM_SWAP (s->data[half], s->data[j]);
308 void fvec_push(fvec_t *in, smpl_t new_elem) {
310 for (i = 0; i < in->length - 1; i++) {
311 in->data[i] = in->data[i + 1];
313 in->data[in->length - 1] = new_elem;
316 void fvec_clamp(fvec_t *in, smpl_t absmax) {
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;
328 aubio_level_lin (const fvec_t * f)
333 for (j = 0; j < f->length; j++) {
334 energy += SQR (f->data[j]);
337 energy = aubio_cblas_dot(f->length, f->data, 1, f->data, 1);
339 return energy / f->length;
343 fvec_local_hfc (fvec_t * v)
347 for (j = 0; j < v->length; j++) {
348 hfc += (j + 1) * v->data[j];
354 fvec_min_removal (fvec_t * v)
356 smpl_t v_min = fvec_min (v);
357 fvec_add (v, - v_min );
361 fvec_alpha_norm (fvec_t * o, smpl_t alpha)
365 for (j = 0; j < o->length; j++) {
366 tmp += POW (ABS (o->data[j]), alpha);
368 return POW (tmp / o->length, 1. / alpha);
372 fvec_alpha_normalise (fvec_t * o, smpl_t alpha)
375 smpl_t norm = fvec_alpha_norm (o, alpha);
376 for (j = 0; j < o->length; j++) {
382 fvec_add (fvec_t * o, smpl_t val)
385 for (j = 0; j < o->length; j++) {
390 void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
391 uint_t post, uint_t pre) {
392 uint_t length = vec->length, j;
393 for (j=0;j<length;j++) {
394 vec->data[j] -= fvec_moving_thres(vec, tmp, post, pre, j);
399 fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec,
400 uint_t post, uint_t pre, uint_t pos)
403 smpl_t *medar = (smpl_t *) tmpvec->data;
404 uint_t win_length = post + pre + 1;
405 uint_t length = vec->length;
406 /* post part of the buffer does not exist */
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++)
411 medar[k] = vec->data[k + pos - post];
412 /* the buffer is fully defined */
413 } else if (pos + pre < length) {
414 for (k = 0; k < win_length; k++)
415 medar[k] = vec->data[k + pos - post];
416 /* pre part of the buffer does not exist */
418 for (k = 0; k < length - pos + post; k++)
419 medar[k] = vec->data[k + pos - post];
420 for (k = length - pos + post; k < win_length; k++)
421 medar[k] = 0.; /* 0-padding at the end */
423 return fvec_median (tmpvec);
426 smpl_t fvec_median (fvec_t * input) {
427 uint_t n = input->length;
428 smpl_t * arr = (smpl_t *) input->data;
431 uint_t middle, ll, hh;
433 low = 0 ; high = n-1 ; median = (low + high) / 2;
435 if (high <= low) /* One element only */
438 if (high == low + 1) { /* Two elements only */
439 if (arr[low] > arr[high])
440 ELEM_SWAP(arr[low], arr[high]) ;
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]) ;
450 /* Swap low item (now in position middle) into position (low+1) */
451 ELEM_SWAP(arr[middle], arr[low+1]) ;
453 /* Nibble from each end towards middle, swapping items when stuck */
457 do ll++; while (arr[low] > arr[ll]) ;
458 do hh--; while (arr[hh] > arr[low]) ;
463 ELEM_SWAP(arr[ll], arr[hh]) ;
466 /* Swap middle item (in position low) back into correct position */
467 ELEM_SWAP(arr[low], arr[hh]) ;
469 /* Re-set active partition */
477 smpl_t fvec_quadratic_peak_pos (const fvec_t * x, uint_t pos) {
478 smpl_t s0, s1, s2; uint_t x0, x2;
479 smpl_t half = .5, two = 2.;
480 if (pos == 0 || pos == x->length - 1) return pos;
481 x0 = (pos < 1) ? pos : pos - 1;
482 x2 = (pos + 1 < x->length) ? pos + 1 : pos;
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;
488 return pos + half * (s0 - s2 ) / (s0 - two * s1 + s2);
491 smpl_t fvec_quadratic_peak_mag (fvec_t *x, smpl_t pos) {
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];
498 x2 = x->data[index + 1];
499 return x1 - .25 * (x0 - x2) * (pos - index);
502 uint_t fvec_peakpick(const fvec_t * onset, uint_t pos) {
504 tmp = (onset->data[pos] > onset->data[pos-1]
505 && onset->data[pos] > onset->data[pos+1]
506 && onset->data[pos] > 0.);
511 aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf)
514 s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2);
519 aubio_freqtomidi (smpl_t freq)
522 if (freq < 2. || freq > 100000.) return 0.; // avoid nans and infs
523 /* log(freq/A-2)/log(2) */
525 midi = LOG (midi) / 0.69314718055995;
532 aubio_miditofreq (smpl_t midi)
535 if (midi > 140.) return 0.; // avoid infs
536 freq = (midi + 3.) / 12.;
537 freq = EXP (freq * 0.69314718055995);
543 aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
545 smpl_t freq = samplerate / fftsize;
546 return freq * MAX(bin, 0);
550 aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
552 smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize);
553 return aubio_freqtomidi (midi);
557 aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize)
559 smpl_t bin = fftsize / samplerate;
560 return MAX(freq, 0) * bin;
564 aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize)
566 smpl_t freq = aubio_miditofreq (midi);
567 return aubio_freqtobin (freq, samplerate, fftsize);
571 aubio_is_power_of_two (uint_t a)
573 if ((a & (a - 1)) == 0) {
581 aubio_next_power_of_two (uint_t a)
584 while (i < a) i <<= 1;
589 aubio_power_of_two_order (uint_t a)
592 int temp = aubio_next_power_of_two(a);
600 aubio_db_spl (const fvec_t * o)
602 return 10. * LOG10 (aubio_level_lin (o));
606 aubio_silence_detection (const fvec_t * o, smpl_t threshold)
608 return (aubio_db_spl (o) < threshold);
612 aubio_level_detection (const fvec_t * o, smpl_t threshold)
614 smpl_t db_spl = aubio_db_spl (o);
615 if (db_spl < threshold) {
623 aubio_zero_crossing_rate (fvec_t * input)
627 for (j = 1; j < input->length; j++) {
628 // previous was strictly negative
629 if (input->data[j - 1] < 0.) {
630 // current is positive or null
631 if (input->data[j] >= 0.) {
634 // previous was positive or null
636 // current is strictly negative
637 if (input->data[j] < 0.) {
642 return zcr / (smpl_t) input->length;
646 aubio_autocorr (const fvec_t * input, fvec_t * output)
648 uint_t i, j, length = input->length;
653 for (i = 0; i < length; i++) {
655 for (j = i; j < length; j++) {
656 tmp += data[j - i] * data[j];
658 acf[i] = tmp / (smpl_t) (length - i);