2 Copyright (C) 2003-2013 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"
38 aubio_win_blackman_harris,
42 aubio_win_default = aubio_win_hanningz,
46 new_aubio_window (char_t * window_type, uint_t length)
48 fvec_t * win = new_fvec (length);
49 fvec_set_window (win, window_type);
53 uint_t fvec_set_window (fvec_t *win, char_t *window_type) {
54 smpl_t * w = win->data;
55 uint_t i, size = win->length;
56 aubio_window_type wintype;
57 if (window_type == NULL) {
58 AUBIO_ERR ("window type can not be null.\n");
60 } else if (strcmp (window_type, "rectangle") == 0)
61 wintype = aubio_win_rectangle;
62 else if (strcmp (window_type, "hamming") == 0)
63 wintype = aubio_win_hamming;
64 else if (strcmp (window_type, "hanning") == 0)
65 wintype = aubio_win_hanning;
66 else if (strcmp (window_type, "hanningz") == 0)
67 wintype = aubio_win_hanningz;
68 else if (strcmp (window_type, "blackman") == 0)
69 wintype = aubio_win_blackman;
70 else if (strcmp (window_type, "blackman_harris") == 0)
71 wintype = aubio_win_blackman_harris;
72 else if (strcmp (window_type, "gaussian") == 0)
73 wintype = aubio_win_gaussian;
74 else if (strcmp (window_type, "welch") == 0)
75 wintype = aubio_win_welch;
76 else if (strcmp (window_type, "parzen") == 0)
77 wintype = aubio_win_parzen;
78 else if (strcmp (window_type, "default") == 0)
79 wintype = aubio_win_default;
81 AUBIO_ERR ("unknown window type `%s`.\n", window_type);
85 case aubio_win_rectangle:
89 case aubio_win_hamming:
91 w[i] = 0.54 - 0.46 * COS(TWO_PI * i / (size));
93 case aubio_win_hanning:
95 w[i] = 0.5 - (0.5 * COS(TWO_PI * i / (size)));
97 case aubio_win_hanningz:
99 w[i] = 0.5 * (1.0 - COS(TWO_PI * i / (size)));
101 case aubio_win_blackman:
104 - 0.50 * COS( TWO_PI*i/(size-1.0))
105 + 0.08 * COS(2.0*TWO_PI*i/(size-1.0));
107 case aubio_win_blackman_harris:
110 - 0.48829 * COS( TWO_PI*i/(size-1.0))
111 + 0.14128 * COS(2.0*TWO_PI*i/(size-1.0))
112 - 0.01168 * COS(3.0*TWO_PI*i/(size-1.0));
114 case aubio_win_gaussian:
116 lsmp_t a, b, c = 0.5;
118 for (n = 0; n < size; n++)
120 a = (n-c*(size-1))/(SQR(c)*(size-1));
126 case aubio_win_welch:
128 w[i] = 1.0 - SQR((2.*i-size)/(size+1.0));
130 case aubio_win_parzen:
132 w[i] = 1.0 - ABS((2.*i-size)/(size+1.0));
141 aubio_unwrap2pi (smpl_t phase)
143 /* mod(phase+pi,-2pi)+pi */
144 return phase + TWO_PI * (1. + FLOOR (-(phase + PI) / TWO_PI));
148 fvec_mean (fvec_t * s)
152 for (j = 0; j < s->length; j++) {
155 return tmp / (smpl_t) (s->length);
159 fvec_sum (fvec_t * s)
163 for (j = 0; j < s->length; j++) {
170 fvec_max (fvec_t * s)
174 for (j = 0; j < s->length; j++) {
175 tmp = (tmp > s->data[j]) ? tmp : s->data[j];
181 fvec_min (fvec_t * s)
184 smpl_t tmp = s->data[0];
185 for (j = 0; j < s->length; j++) {
186 tmp = (tmp < s->data[j]) ? tmp : s->data[j];
192 fvec_min_elem (fvec_t * s)
195 smpl_t tmp = s->data[0];
196 for (j = 0; j < s->length; j++) {
197 pos = (tmp < s->data[j]) ? pos : j;
198 tmp = (tmp < s->data[j]) ? tmp : s->data[j];
204 fvec_max_elem (fvec_t * s)
208 for (j = 0; j < s->length; j++) {
209 pos = (tmp > s->data[j]) ? pos : j;
210 tmp = (tmp > s->data[j]) ? tmp : s->data[j];
216 fvec_shift (fvec_t * s)
219 for (j = 0; j < s->length / 2; j++) {
220 ELEM_SWAP (s->data[j], s->data[j + s->length / 2]);
225 fvec_local_energy (fvec_t * f)
229 for (j = 0; j < f->length; j++) {
230 energy += SQR (f->data[j]);
232 return energy / f->length;
236 fvec_local_hfc (fvec_t * v)
240 for (j = 0; j < v->length; j++) {
241 hfc += (j + 1) * v->data[j];
247 fvec_min_removal (fvec_t * v)
249 smpl_t v_min = fvec_min (v);
250 fvec_add (v, - v_min );
254 fvec_alpha_norm (fvec_t * o, smpl_t alpha)
258 for (j = 0; j < o->length; j++) {
259 tmp += POW (ABS (o->data[j]), alpha);
261 return POW (tmp / o->length, 1. / alpha);
265 fvec_alpha_normalise (fvec_t * o, smpl_t alpha)
268 smpl_t norm = fvec_alpha_norm (o, alpha);
269 for (j = 0; j < o->length; j++) {
275 fvec_add (fvec_t * o, smpl_t val)
278 for (j = 0; j < o->length; j++) {
283 void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
284 uint_t post, uint_t pre) {
285 uint_t length = vec->length, j;
286 for (j=0;j<length;j++) {
287 vec->data[j] -= fvec_moving_thres(vec, tmp, post, pre, j);
292 fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec,
293 uint_t post, uint_t pre, uint_t pos)
296 smpl_t *medar = (smpl_t *) tmpvec->data;
297 uint_t win_length = post + pre + 1;
298 uint_t length = vec->length;
299 /* post part of the buffer does not exist */
300 if (pos < post + 1) {
301 for (k = 0; k < post + 1 - pos; k++)
302 medar[k] = 0.; /* 0-padding at the beginning */
303 for (k = post + 1 - pos; k < win_length; k++)
304 medar[k] = vec->data[k + pos - post];
305 /* the buffer is fully defined */
306 } else if (pos + pre < length) {
307 for (k = 0; k < win_length; k++)
308 medar[k] = vec->data[k + pos - post];
309 /* pre part of the buffer does not exist */
311 for (k = 0; k < length - pos + post; k++)
312 medar[k] = vec->data[k + pos - post];
313 for (k = length - pos + post; k < win_length; k++)
314 medar[k] = 0.; /* 0-padding at the end */
316 return fvec_median (tmpvec);
319 smpl_t fvec_median (fvec_t * input) {
320 uint_t n = input->length;
321 smpl_t * arr = (smpl_t *) input->data;
324 uint_t middle, ll, hh;
326 low = 0 ; high = n-1 ; median = (low + high) / 2;
328 if (high <= low) /* One element only */
331 if (high == low + 1) { /* Two elements only */
332 if (arr[low] > arr[high])
333 ELEM_SWAP(arr[low], arr[high]) ;
337 /* Find median of low, middle and high items; swap into position low */
338 middle = (low + high) / 2;
339 if (arr[middle] > arr[high]) ELEM_SWAP(arr[middle], arr[high]);
340 if (arr[low] > arr[high]) ELEM_SWAP(arr[low], arr[high]);
341 if (arr[middle] > arr[low]) ELEM_SWAP(arr[middle], arr[low]) ;
343 /* Swap low item (now in position middle) into position (low+1) */
344 ELEM_SWAP(arr[middle], arr[low+1]) ;
346 /* Nibble from each end towards middle, swapping items when stuck */
350 do ll++; while (arr[low] > arr[ll]) ;
351 do hh--; while (arr[hh] > arr[low]) ;
356 ELEM_SWAP(arr[ll], arr[hh]) ;
359 /* Swap middle item (in position low) back into correct position */
360 ELEM_SWAP(arr[low], arr[hh]) ;
362 /* Re-set active partition */
370 smpl_t fvec_quadint (fvec_t * x, uint_t pos) {
372 uint_t x0 = (pos < 1) ? pos : pos - 1;
373 uint_t x2 = (pos + 1 < x->length) ? pos + 1 : pos;
374 if (x0 == pos) return (x->data[pos] <= x->data[x2]) ? pos : x2;
375 if (x2 == pos) return (x->data[pos] <= x->data[x0]) ? pos : x0;
379 return pos + 0.5 * (s2 - s0 ) / (s2 - 2.* s1 + s0);
382 smpl_t fvec_quadratic_peak_pos (fvec_t * x, uint_t pos) {
384 uint_t x0 = (pos < 1) ? pos : pos - 1;
385 uint_t x2 = (pos + 1 < x->length) ? pos + 1 : pos;
386 if (x0 == pos) return (x->data[pos] <= x->data[x2]) ? pos : x2;
387 if (x2 == pos) return (x->data[pos] <= x->data[x0]) ? pos : x0;
391 return pos + 0.5 * (s0 - s2 ) / (s0 - 2.* s1 + s2);
394 uint_t fvec_peakpick(fvec_t * onset, uint_t pos) {
396 tmp = (onset->data[pos] > onset->data[pos-1]
397 && onset->data[pos] > onset->data[pos+1]
398 && onset->data[pos] > 0.);
403 aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf)
406 s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2);
411 aubio_freqtomidi (smpl_t freq)
413 if (freq < 2. || freq > 100000.) return 0.; // avoid nans and infs
414 /* log(freq/A-2)/log(2) */
415 smpl_t midi = freq / 6.875;
416 midi = LOG (midi) / 0.69314718055995;
423 aubio_miditofreq (smpl_t midi)
425 if (midi > 140.) return 0.; // avoid infs
426 smpl_t freq = (midi + 3.) / 12.;
427 freq = EXP (freq * 0.69314718055995);
433 aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
435 smpl_t freq = samplerate / fftsize;
436 return freq * MAX(bin, 0);
440 aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
442 smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize);
443 return aubio_freqtomidi (midi);
447 aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize)
449 smpl_t bin = fftsize / samplerate;
450 return MAX(freq, 0) * bin;
454 aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize)
456 smpl_t freq = aubio_miditofreq (midi);
457 return aubio_freqtobin (freq, samplerate, fftsize);
461 aubio_is_power_of_two (uint_t a)
463 if ((a & (a - 1)) == 0) {
471 aubio_next_power_of_two (uint_t a)
474 while (i < a) i <<= 1;
479 aubio_db_spl (fvec_t * o)
481 return 10. * LOG10 (fvec_local_energy (o));
485 aubio_silence_detection (fvec_t * o, smpl_t threshold)
487 return (aubio_db_spl (o) < threshold);
491 aubio_level_detection (fvec_t * o, smpl_t threshold)
493 smpl_t db_spl = aubio_db_spl (o);
494 if (db_spl < threshold) {
502 aubio_zero_crossing_rate (fvec_t * input)
506 for (j = 1; j < input->length; j++) {
507 // previous was strictly negative
508 if (input->data[j - 1] < 0.) {
509 // current is positive or null
510 if (input->data[j] >= 0.) {
513 // previous was positive or null
515 // current is strictly negative
516 if (input->data[j] < 0.) {
521 return zcr / (smpl_t) input->length;
525 aubio_autocorr (fvec_t * input, fvec_t * output)
527 uint_t i, j, length = input->length;
532 for (i = 0; i < length; i++) {
534 for (j = i; j < length; j++) {
535 tmp += data[j - i] * data[j];
537 acf[i] = tmp / (smpl_t) (length - i);