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