Merge branch 'develop' into accelerate
[aubio.git] / src / mathutils.c
1 /*
2   Copyright (C) 2003-2014 Paul Brossier <piem@aubio.org>
3
4   This file is part of aubio.
5
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.
10
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/>.
18
19 */
20
21 /* see in mathutils.h for doc */
22
23 #include "aubio_priv.h"
24 #include "fvec.h"
25 #include "mathutils.h"
26 #include "musicutils.h"
27 #include "config.h"
28
29 #ifdef HAVE_ACCELERATE
30 #include <Accelerate/Accelerate.h>
31 #endif
32
33 #if !HAVE_AUBIO_DOUBLE
34 #define aubio_cblas_xswap cblas_sswap
35 #define aubio_cblas_dot   cblas_sdot
36 #else
37 #define aubio_cblas_xswap cblas_dswap
38 #define aubio_cblas_dot   cblas_ddot
39 #endif
40
41 /** Window types */
42 typedef enum
43 {
44   aubio_win_rectangle,
45   aubio_win_hamming,
46   aubio_win_hanning,
47   aubio_win_hanningz,
48   aubio_win_blackman,
49   aubio_win_blackman_harris,
50   aubio_win_gaussian,
51   aubio_win_welch,
52   aubio_win_parzen,
53   aubio_win_default = aubio_win_hanningz,
54 } aubio_window_type;
55
56 fvec_t *
57 new_aubio_window (char_t * window_type, uint_t length)
58 {
59   fvec_t * win = new_fvec (length);
60   uint_t err;
61   if (win == NULL) {
62     return NULL;
63   }
64   err = fvec_set_window (win, window_type);
65   if (err != 0) {
66     del_fvec(win);
67     return NULL;
68   }
69   return win;
70 }
71
72 uint_t fvec_set_window (fvec_t *win, char_t *window_type) {
73   smpl_t * w = win->data;
74   uint_t i, size = win->length;
75   aubio_window_type wintype;
76   if (window_type == NULL) {
77       AUBIO_ERR ("window type can not be null.\n");
78       return 1;
79   } else if (strcmp (window_type, "rectangle") == 0)
80       wintype = aubio_win_rectangle;
81   else if (strcmp (window_type, "hamming") == 0)
82       wintype = aubio_win_hamming;
83   else if (strcmp (window_type, "hanning") == 0)
84       wintype = aubio_win_hanning;
85   else if (strcmp (window_type, "hanningz") == 0)
86       wintype = aubio_win_hanningz;
87   else if (strcmp (window_type, "blackman") == 0)
88       wintype = aubio_win_blackman;
89   else if (strcmp (window_type, "blackman_harris") == 0)
90       wintype = aubio_win_blackman_harris;
91   else if (strcmp (window_type, "gaussian") == 0)
92       wintype = aubio_win_gaussian;
93   else if (strcmp (window_type, "welch") == 0)
94       wintype = aubio_win_welch;
95   else if (strcmp (window_type, "parzen") == 0)
96       wintype = aubio_win_parzen;
97   else if (strcmp (window_type, "default") == 0)
98       wintype = aubio_win_default;
99   else {
100       AUBIO_ERR ("unknown window type `%s`.\n", window_type);
101       return 1;
102   }
103   switch(wintype) {
104     case aubio_win_rectangle:
105       for (i=0;i<size;i++)
106         w[i] = 0.5;
107       break;
108     case aubio_win_hamming:
109       for (i=0;i<size;i++)
110         w[i] = 0.54 - 0.46 * COS(TWO_PI * i / (size));
111       break;
112     case aubio_win_hanning:
113       for (i=0;i<size;i++)
114         w[i] = 0.5 - (0.5 * COS(TWO_PI * i / (size)));
115       break;
116     case aubio_win_hanningz:
117       for (i=0;i<size;i++)
118         w[i] = 0.5 * (1.0 - COS(TWO_PI * i / (size)));
119       break;
120     case aubio_win_blackman:
121       for (i=0;i<size;i++)
122         w[i] = 0.42
123           - 0.50 * COS(    TWO_PI*i/(size-1.0))
124           + 0.08 * COS(2.0*TWO_PI*i/(size-1.0));
125       break;
126     case aubio_win_blackman_harris:
127       for (i=0;i<size;i++)
128         w[i] = 0.35875
129           - 0.48829 * COS(    TWO_PI*i/(size-1.0))
130           + 0.14128 * COS(2.0*TWO_PI*i/(size-1.0))
131           - 0.01168 * COS(3.0*TWO_PI*i/(size-1.0));
132       break;
133     case aubio_win_gaussian:
134       {
135         lsmp_t a, b, c = 0.5;
136         uint_t n;
137         for (n = 0; n < size; n++)
138         {
139           a = (n-c*(size-1))/(SQR(c)*(size-1));
140           b = -c*SQR(a);
141           w[n] = EXP(b);
142         }
143       }
144       break;
145     case aubio_win_welch:
146       for (i=0;i<size;i++)
147         w[i] = 1.0 - SQR((2.*i-size)/(size+1.0));
148       break;
149     case aubio_win_parzen:
150       for (i=0;i<size;i++)
151         w[i] = 1.0 - ABS((2.f*i-size)/(size+1.0f));
152       break;
153     default:
154       break;
155   }
156   return 0;
157 }
158
159 smpl_t
160 aubio_unwrap2pi (smpl_t phase)
161 {
162   /* mod(phase+pi,-2pi)+pi */
163   return phase + TWO_PI * (1. + FLOOR (-(phase + PI) / TWO_PI));
164 }
165
166 smpl_t
167 fvec_mean (fvec_t * s)
168 {
169   smpl_t tmp = 0.0;
170 #ifndef HAVE_ACCELERATE
171   uint_t j;
172   for (j = 0; j < s->length; j++) {
173     tmp += s->data[j];
174   }
175   return tmp / (smpl_t) (s->length);
176 #else
177 #if !HAVE_AUBIO_DOUBLE
178   vDSP_meanv(s->data, 1, &tmp, s->length);
179 #else /* HAVE_AUBIO_DOUBLE */
180   vDSP_meanvD(s->data, 1, &tmp, s->length);
181 #endif /* HAVE_AUBIO_DOUBLE */
182   return tmp;
183 #endif /* HAVE_ACCELERATE */
184 }
185
186 smpl_t
187 fvec_sum (fvec_t * s)
188 {
189   smpl_t tmp = 0.0;
190 #ifndef HAVE_ACCELERATE
191   uint_t j;
192   for (j = 0; j < s->length; j++) {
193     tmp += s->data[j];
194   }
195 #else
196 #if !HAVE_AUBIO_DOUBLE
197   vDSP_sve(s->data, 1, &tmp, s->length);
198 #else /* HAVE_AUBIO_DOUBLE */
199   vDSP_sveD(s->data, 1, &tmp, s->length);
200 #endif /* HAVE_AUBIO_DOUBLE */
201 #endif /* HAVE_ACCELERATE */
202   return tmp;
203 }
204
205 smpl_t
206 fvec_max (fvec_t * s)
207 {
208 #ifndef HAVE_ACCELERATE
209   uint_t j;
210   smpl_t tmp = 0.0;
211   for (j = 0; j < s->length; j++) {
212     tmp = (tmp > s->data[j]) ? tmp : s->data[j];
213   }
214 #else
215   smpl_t tmp = 0.;
216 #if !HAVE_AUBIO_DOUBLE
217   vDSP_maxv(s->data, 1, &tmp, s->length);
218 #else
219   vDSP_maxvD(s->data, 1, &tmp, s->length);
220 #endif
221 #endif
222   return tmp;
223 }
224
225 smpl_t
226 fvec_min (fvec_t * s)
227 {
228 #ifndef HAVE_ACCELERATE
229   uint_t j;
230   smpl_t tmp = s->data[0];
231   for (j = 0; j < s->length; j++) {
232     tmp = (tmp < s->data[j]) ? tmp : s->data[j];
233   }
234 #else
235   smpl_t tmp = 0.;
236 #if !HAVE_AUBIO_DOUBLE
237   vDSP_minv(s->data, 1, &tmp, s->length);
238 #else
239   vDSP_minvD(s->data, 1, &tmp, s->length);
240 #endif
241 #endif
242   return tmp;
243 }
244
245 uint_t
246 fvec_min_elem (fvec_t * s)
247 {
248 #ifndef HAVE_ACCELERATE
249   uint_t j, pos = 0.;
250   smpl_t tmp = s->data[0];
251   for (j = 0; j < s->length; j++) {
252     pos = (tmp < s->data[j]) ? pos : j;
253     tmp = (tmp < s->data[j]) ? tmp : s->data[j];
254   }
255 #else
256   smpl_t tmp = 0.;
257   uint_t pos = 0.;
258 #if !HAVE_AUBIO_DOUBLE
259   vDSP_minvi(s->data, 1, &tmp, (vDSP_Length *)&pos, s->length);
260 #else
261   vDSP_minviD(s->data, 1, &tmp, (vDSP_Length *)&pos, s->length);
262 #endif
263 #endif
264   return pos;
265 }
266
267 uint_t
268 fvec_max_elem (fvec_t * s)
269 {
270 #ifndef HAVE_ACCELERATE
271   uint_t j, pos = 0;
272   smpl_t tmp = 0.0;
273   for (j = 0; j < s->length; j++) {
274     pos = (tmp > s->data[j]) ? pos : j;
275     tmp = (tmp > s->data[j]) ? tmp : s->data[j];
276   }
277 #else
278   smpl_t tmp = 0.;
279   uint_t pos = 0.;
280 #if !HAVE_AUBIO_DOUBLE
281   vDSP_maxvi(s->data, 1, &tmp, (vDSP_Length *)&pos, s->length);
282 #else
283   vDSP_maxviD(s->data, 1, &tmp, (vDSP_Length *)&pos, s->length);
284 #endif
285 #endif
286   return pos;
287 }
288
289 void
290 fvec_shift (fvec_t * s)
291 {
292 #ifndef HAVE_ACCELERATE
293   uint_t j;
294   for (j = 0; j < s->length / 2; j++) {
295     ELEM_SWAP (s->data[j], s->data[j + s->length / 2]);
296   }
297 #else
298   uint_t half = s->length / 2;
299   aubio_cblas_xswap(half, s->data, 1, s->data + half, 1);
300 #endif
301 }
302
303 smpl_t
304 aubio_level_lin (fvec_t * f)
305 {
306   smpl_t energy = 0.;
307 #ifndef HAVE_ACCELERATE
308   uint_t j;
309   for (j = 0; j < f->length; j++) {
310     energy += SQR (f->data[j]);
311   }
312 #else
313   energy = aubio_cblas_dot(f->length, f->data, 1, f->data, 1);
314 #endif
315   return energy / f->length;
316 }
317
318 smpl_t
319 fvec_local_hfc (fvec_t * v)
320 {
321   smpl_t hfc = 0.;
322   uint_t j;
323   for (j = 0; j < v->length; j++) {
324     hfc += (j + 1) * v->data[j];
325   }
326   return hfc;
327 }
328
329 void
330 fvec_min_removal (fvec_t * v)
331 {
332   smpl_t v_min = fvec_min (v);
333   fvec_add (v,  - v_min );
334 }
335
336 smpl_t
337 fvec_alpha_norm (fvec_t * o, smpl_t alpha)
338 {
339   uint_t j;
340   smpl_t tmp = 0.;
341   for (j = 0; j < o->length; j++) {
342     tmp += POW (ABS (o->data[j]), alpha);
343   }
344   return POW (tmp / o->length, 1. / alpha);
345 }
346
347 void
348 fvec_alpha_normalise (fvec_t * o, smpl_t alpha)
349 {
350   uint_t j;
351   smpl_t norm = fvec_alpha_norm (o, alpha);
352   for (j = 0; j < o->length; j++) {
353     o->data[j] /= norm;
354   }
355 }
356
357 void
358 fvec_add (fvec_t * o, smpl_t val)
359 {
360   uint_t j;
361   for (j = 0; j < o->length; j++) {
362     o->data[j] += val;
363   }
364 }
365
366 void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
367     uint_t post, uint_t pre) {
368   uint_t length = vec->length, j;
369   for (j=0;j<length;j++) {
370     vec->data[j] -= fvec_moving_thres(vec, tmp, post, pre, j);
371   }
372 }
373
374 smpl_t
375 fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec,
376     uint_t post, uint_t pre, uint_t pos)
377 {
378   uint_t k;
379   smpl_t *medar = (smpl_t *) tmpvec->data;
380   uint_t win_length = post + pre + 1;
381   uint_t length = vec->length;
382   /* post part of the buffer does not exist */
383   if (pos < post + 1) {
384     for (k = 0; k < post + 1 - pos; k++)
385       medar[k] = 0.;            /* 0-padding at the beginning */
386     for (k = post + 1 - pos; k < win_length; k++)
387       medar[k] = vec->data[k + pos - post];
388     /* the buffer is fully defined */
389   } else if (pos + pre < length) {
390     for (k = 0; k < win_length; k++)
391       medar[k] = vec->data[k + pos - post];
392     /* pre part of the buffer does not exist */
393   } else {
394     for (k = 0; k < length - pos + post; k++)
395       medar[k] = vec->data[k + pos - post];
396     for (k = length - pos + post; k < win_length; k++)
397       medar[k] = 0.;            /* 0-padding at the end */
398   }
399   return fvec_median (tmpvec);
400 }
401
402 smpl_t fvec_median (fvec_t * input) {
403   uint_t n = input->length;
404   smpl_t * arr = (smpl_t *) input->data;
405   uint_t low, high ;
406   uint_t median;
407   uint_t middle, ll, hh;
408
409   low = 0 ; high = n-1 ; median = (low + high) / 2;
410   for (;;) {
411     if (high <= low) /* One element only */
412       return arr[median] ;
413
414     if (high == low + 1) {  /* Two elements only */
415       if (arr[low] > arr[high])
416         ELEM_SWAP(arr[low], arr[high]) ;
417       return arr[median] ;
418     }
419
420     /* Find median of low, middle and high items; swap into position low */
421     middle = (low + high) / 2;
422     if (arr[middle] > arr[high])    ELEM_SWAP(arr[middle], arr[high]);
423     if (arr[low]    > arr[high])    ELEM_SWAP(arr[low],    arr[high]);
424     if (arr[middle] > arr[low])     ELEM_SWAP(arr[middle], arr[low]) ;
425
426     /* Swap low item (now in position middle) into position (low+1) */
427     ELEM_SWAP(arr[middle], arr[low+1]) ;
428
429     /* Nibble from each end towards middle, swapping items when stuck */
430     ll = low + 1;
431     hh = high;
432     for (;;) {
433       do ll++; while (arr[low] > arr[ll]) ;
434       do hh--; while (arr[hh]  > arr[low]) ;
435
436       if (hh < ll)
437         break;
438
439       ELEM_SWAP(arr[ll], arr[hh]) ;
440     }
441
442     /* Swap middle item (in position low) back into correct position */
443     ELEM_SWAP(arr[low], arr[hh]) ;
444
445     /* Re-set active partition */
446     if (hh <= median)
447       low = ll;
448     if (hh >= median)
449       high = hh - 1;
450   }
451 }
452
453 smpl_t fvec_quadratic_peak_pos (fvec_t * x, uint_t pos) {
454   smpl_t s0, s1, s2; uint_t x0, x2;
455   if (pos == 0 || pos == x->length - 1) return pos;
456   x0 = (pos < 1) ? pos : pos - 1;
457   x2 = (pos + 1 < x->length) ? pos + 1 : pos;
458   if (x0 == pos) return (x->data[pos] <= x->data[x2]) ? pos : x2;
459   if (x2 == pos) return (x->data[pos] <= x->data[x0]) ? pos : x0;
460   s0 = x->data[x0];
461   s1 = x->data[pos];
462   s2 = x->data[x2];
463   return pos + 0.5 * (s0 - s2 ) / (s0 - 2.* s1 + s2);
464 }
465
466 smpl_t fvec_quadratic_peak_mag (fvec_t *x, smpl_t pos) {
467   smpl_t x0, x1, x2;
468   uint_t index = (uint_t)(pos - .5) + 1;
469   if (pos >= x->length || pos < 0.) return 0.;
470   if ((smpl_t)index == pos) return x->data[index];
471   x0 = x->data[index - 1];
472   x1 = x->data[index];
473   x2 = x->data[index + 1];
474   return x1 - .25 * (x0 - x2) * (pos - index);
475 }
476
477 uint_t fvec_peakpick(fvec_t * onset, uint_t pos) {
478   uint_t tmp=0;
479   tmp = (onset->data[pos] > onset->data[pos-1]
480       &&  onset->data[pos] > onset->data[pos+1]
481       &&  onset->data[pos] > 0.);
482   return tmp;
483 }
484
485 smpl_t
486 aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf)
487 {
488   smpl_t tmp =
489       s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2);
490   return tmp;
491 }
492
493 smpl_t
494 aubio_freqtomidi (smpl_t freq)
495 {
496   smpl_t midi;
497   if (freq < 2. || freq > 100000.) return 0.; // avoid nans and infs
498   /* log(freq/A-2)/log(2) */
499   midi = freq / 6.875;
500   midi = LOG (midi) / 0.69314718055995;
501   midi *= 12;
502   midi -= 3;
503   return midi;
504 }
505
506 smpl_t
507 aubio_miditofreq (smpl_t midi)
508 {
509   smpl_t freq;
510   if (midi > 140.) return 0.; // avoid infs
511   freq = (midi + 3.) / 12.;
512   freq = EXP (freq * 0.69314718055995);
513   freq *= 6.875;
514   return freq;
515 }
516
517 smpl_t
518 aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
519 {
520   smpl_t freq = samplerate / fftsize;
521   return freq * MAX(bin, 0);
522 }
523
524 smpl_t
525 aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
526 {
527   smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize);
528   return aubio_freqtomidi (midi);
529 }
530
531 smpl_t
532 aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize)
533 {
534   smpl_t bin = fftsize / samplerate;
535   return MAX(freq, 0) * bin;
536 }
537
538 smpl_t
539 aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize)
540 {
541   smpl_t freq = aubio_miditofreq (midi);
542   return aubio_freqtobin (freq, samplerate, fftsize);
543 }
544
545 uint_t
546 aubio_is_power_of_two (uint_t a)
547 {
548   if ((a & (a - 1)) == 0) {
549     return 1;
550   } else {
551     return 0;
552   }
553 }
554
555 uint_t
556 aubio_next_power_of_two (uint_t a)
557 {
558   uint_t i = 1;
559   while (i < a) i <<= 1;
560   return i;
561 }
562
563 smpl_t
564 aubio_db_spl (fvec_t * o)
565 {
566   return 10. * LOG10 (aubio_level_lin (o));
567 }
568
569 uint_t
570 aubio_silence_detection (fvec_t * o, smpl_t threshold)
571 {
572   return (aubio_db_spl (o) < threshold);
573 }
574
575 smpl_t
576 aubio_level_detection (fvec_t * o, smpl_t threshold)
577 {
578   smpl_t db_spl = aubio_db_spl (o);
579   if (db_spl < threshold) {
580     return 1.;
581   } else {
582     return db_spl;
583   }
584 }
585
586 smpl_t
587 aubio_zero_crossing_rate (fvec_t * input)
588 {
589   uint_t j;
590   uint_t zcr = 0;
591   for (j = 1; j < input->length; j++) {
592     // previous was strictly negative
593     if (input->data[j - 1] < 0.) {
594       // current is positive or null
595       if (input->data[j] >= 0.) {
596         zcr += 1;
597       }
598       // previous was positive or null
599     } else {
600       // current is strictly negative
601       if (input->data[j] < 0.) {
602         zcr += 1;
603       }
604     }
605   }
606   return zcr / (smpl_t) input->length;
607 }
608
609 void
610 aubio_autocorr (fvec_t * input, fvec_t * output)
611 {
612   uint_t i, j, length = input->length;
613   smpl_t *data, *acf;
614   smpl_t tmp = 0;
615   data = input->data;
616   acf = output->data;
617   for (i = 0; i < length; i++) {
618     tmp = 0.;
619     for (j = i; j < length; j++) {
620       tmp += data[j - i] * data[j];
621     }
622     acf[i] = tmp / (smpl_t) (length - i);
623   }
624 }
625
626 void
627 aubio_cleanup (void)
628 {
629 #ifdef HAVE_FFTW3F
630   fftwf_cleanup ();
631 #else
632 #ifdef HAVE_FFTW3
633   fftw_cleanup ();
634 #endif
635 #endif
636 }