[source] simplify and avoid unrequired checks
[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
28 /** Window types */
29 typedef enum
30 {
31   aubio_win_ones,
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, "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;
89   else {
90       AUBIO_ERR ("unknown window type `%s`.\n", window_type);
91       return 1;
92   }
93   switch(wintype) {
94     case aubio_win_ones:
95       fvec_ones(win);
96       break;
97     case aubio_win_rectangle:
98       fvec_set_all(win, .5);
99       break;
100     case aubio_win_hamming:
101       for (i=0;i<size;i++)
102         w[i] = 0.54 - 0.46 * COS(TWO_PI * i / (size));
103       break;
104     case aubio_win_hanning:
105       for (i=0;i<size;i++)
106         w[i] = 0.5 - (0.5 * COS(TWO_PI * i / (size)));
107       break;
108     case aubio_win_hanningz:
109       for (i=0;i<size;i++)
110         w[i] = 0.5 * (1.0 - COS(TWO_PI * i / (size)));
111       break;
112     case aubio_win_blackman:
113       for (i=0;i<size;i++)
114         w[i] = 0.42
115           - 0.50 * COS(    TWO_PI*i/(size-1.0))
116           + 0.08 * COS(2.0*TWO_PI*i/(size-1.0));
117       break;
118     case aubio_win_blackman_harris:
119       for (i=0;i<size;i++)
120         w[i] = 0.35875
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));
124       break;
125     case aubio_win_gaussian:
126       {
127         lsmp_t a, b, c = 0.5;
128         uint_t n;
129         for (n = 0; n < size; n++)
130         {
131           a = (n-c*(size-1))/(SQR(c)*(size-1));
132           b = -c*SQR(a);
133           w[n] = EXP(b);
134         }
135       }
136       break;
137     case aubio_win_welch:
138       for (i=0;i<size;i++)
139         w[i] = 1.0 - SQR((2.*i-size)/(size+1.0));
140       break;
141     case aubio_win_parzen:
142       for (i=0;i<size;i++)
143         w[i] = 1.0 - ABS((2.f*i-size)/(size+1.0f));
144       break;
145     default:
146       break;
147   }
148   return 0;
149 }
150
151 smpl_t
152 aubio_unwrap2pi (smpl_t phase)
153 {
154   /* mod(phase+pi,-2pi)+pi */
155   return phase + TWO_PI * (1. + FLOOR (-(phase + PI) / TWO_PI));
156 }
157
158 smpl_t
159 fvec_mean (fvec_t * s)
160 {
161   smpl_t tmp = 0.0;
162 #if defined(HAVE_INTEL_IPP)
163   aubio_ippsMean(s->data, (int)s->length, &tmp);
164   return tmp;
165 #elif defined(HAVE_ACCELERATE)
166   aubio_vDSP_meanv(s->data, 1, &tmp, s->length);
167   return tmp;
168 #else
169   uint_t j;
170   for (j = 0; j < s->length; j++) {
171     tmp += s->data[j];
172   }
173   return tmp / (smpl_t)(s->length);
174 #endif
175 }
176
177 smpl_t
178 fvec_sum (fvec_t * s)
179 {
180   smpl_t tmp = 0.0;
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);
185 #else
186   uint_t j;
187   for (j = 0; j < s->length; j++) {
188     tmp += s->data[j];
189   }
190 #endif
191   return tmp;
192 }
193
194 smpl_t
195 fvec_max (fvec_t * s)
196 {
197 #if defined(HAVE_INTEL_IPP)
198   smpl_t tmp = 0.;
199   aubio_ippsMax( s->data, (int)s->length, &tmp);
200 #elif defined(HAVE_ACCELERATE)
201   smpl_t tmp = 0.;
202   aubio_vDSP_maxv( s->data, 1, &tmp, s->length );
203 #else
204   uint_t j;
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];
208   }
209 #endif
210   return tmp;
211 }
212
213 smpl_t
214 fvec_min (fvec_t * s)
215 {
216 #if defined(HAVE_INTEL_IPP)
217   smpl_t tmp = 0.;
218   aubio_ippsMin(s->data, (int)s->length, &tmp);
219 #elif defined(HAVE_ACCELERATE)
220   smpl_t tmp = 0.;
221   aubio_vDSP_minv(s->data, 1, &tmp, s->length);
222 #else
223   uint_t j;
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];
227   }
228 #endif
229   return tmp;
230 }
231
232 uint_t
233 fvec_min_elem (fvec_t * s)
234 {
235 #ifndef HAVE_ACCELERATE
236   uint_t j, pos = 0.;
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];
241   }
242 #else
243   smpl_t tmp = 0.;
244   vDSP_Length pos = 0;
245   aubio_vDSP_minvi(s->data, 1, &tmp, &pos, s->length);
246 #endif
247   return (uint_t)pos;
248 }
249
250 uint_t
251 fvec_max_elem (fvec_t * s)
252 {
253 #ifndef HAVE_ACCELERATE
254   uint_t j, pos = 0;
255   smpl_t tmp = 0.0;
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];
259   }
260 #else
261   smpl_t tmp = 0.;
262   vDSP_Length pos = 0;
263   aubio_vDSP_maxvi(s->data, 1, &tmp, &pos, s->length);
264 #endif
265   return (uint_t)pos;
266 }
267
268 void
269 fvec_shift (fvec_t * s)
270 {
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 ++;
274 #ifndef HAVE_BLAS
275   for (j = 0; j < half; j++) {
276     ELEM_SWAP (s->data[j], s->data[j + start]);
277   }
278 #else
279   aubio_cblas_swap(half, s->data, 1, s->data + start, 1);
280 #endif
281   if (start != half) {
282     for (j = 0; j < half; j++) {
283       ELEM_SWAP (s->data[j + start - 1], s->data[j + start]);
284     }
285   }
286 }
287
288 void
289 fvec_ishift (fvec_t * s)
290 {
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 ++;
294 #ifndef HAVE_BLAS
295   for (j = 0; j < half; j++) {
296     ELEM_SWAP (s->data[j], s->data[j + start]);
297   }
298 #else
299   aubio_cblas_swap(half, s->data, 1, s->data + start, 1);
300 #endif
301   if (start != half) {
302     for (j = 0; j < half; j++) {
303       ELEM_SWAP (s->data[half], s->data[j]);
304     }
305   }
306 }
307
308 void fvec_push(fvec_t *in, smpl_t new_elem) {
309   uint_t i;
310   for (i = 0; i < in->length - 1; i++) {
311     in->data[i] = in->data[i + 1];
312   }
313   in->data[in->length - 1] = new_elem;
314 }
315
316 void fvec_clamp(fvec_t *in, smpl_t absmax) {
317   uint_t i;
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;
323     }
324   }
325 }
326
327 smpl_t
328 aubio_level_lin (const fvec_t * f)
329 {
330   smpl_t energy = 0.;
331 #ifndef HAVE_BLAS
332   uint_t j;
333   for (j = 0; j < f->length; j++) {
334     energy += SQR (f->data[j]);
335   }
336 #else
337   energy = aubio_cblas_dot(f->length, f->data, 1, f->data, 1);
338 #endif
339   return energy / f->length;
340 }
341
342 smpl_t
343 fvec_local_hfc (fvec_t * v)
344 {
345   smpl_t hfc = 0.;
346   uint_t j;
347   for (j = 0; j < v->length; j++) {
348     hfc += (j + 1) * v->data[j];
349   }
350   return hfc;
351 }
352
353 void
354 fvec_min_removal (fvec_t * v)
355 {
356   smpl_t v_min = fvec_min (v);
357   fvec_add (v,  - v_min );
358 }
359
360 smpl_t
361 fvec_alpha_norm (fvec_t * o, smpl_t alpha)
362 {
363   uint_t j;
364   smpl_t tmp = 0.;
365   for (j = 0; j < o->length; j++) {
366     tmp += POW (ABS (o->data[j]), alpha);
367   }
368   return POW (tmp / o->length, 1. / alpha);
369 }
370
371 void
372 fvec_alpha_normalise (fvec_t * o, smpl_t alpha)
373 {
374   uint_t j;
375   smpl_t norm = fvec_alpha_norm (o, alpha);
376   for (j = 0; j < o->length; j++) {
377     o->data[j] /= norm;
378   }
379 }
380
381 void
382 fvec_add (fvec_t * o, smpl_t val)
383 {
384   uint_t j;
385   for (j = 0; j < o->length; j++) {
386     o->data[j] += val;
387   }
388 }
389
390 void
391 fvec_mul (fvec_t *o, smpl_t val)
392 {
393   uint_t j;
394   for (j = 0; j < o->length; j++) {
395     o->data[j] *= val;
396   }
397 }
398
399 void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
400     uint_t post, uint_t pre) {
401   uint_t length = vec->length, j;
402   for (j=0;j<length;j++) {
403     vec->data[j] -= fvec_moving_thres(vec, tmp, post, pre, j);
404   }
405 }
406
407 smpl_t
408 fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec,
409     uint_t post, uint_t pre, uint_t pos)
410 {
411   uint_t k;
412   smpl_t *medar = (smpl_t *) tmpvec->data;
413   uint_t win_length = post + pre + 1;
414   uint_t length = vec->length;
415   /* post part of the buffer does not exist */
416   if (pos < post + 1) {
417     for (k = 0; k < post + 1 - pos; k++)
418       medar[k] = 0.;            /* 0-padding at the beginning */
419     for (k = post + 1 - pos; k < win_length; k++)
420       medar[k] = vec->data[k + pos - post];
421     /* the buffer is fully defined */
422   } else if (pos + pre < length) {
423     for (k = 0; k < win_length; k++)
424       medar[k] = vec->data[k + pos - post];
425     /* pre part of the buffer does not exist */
426   } else {
427     for (k = 0; k < length - pos + post; k++)
428       medar[k] = vec->data[k + pos - post];
429     for (k = length - pos + post; k < win_length; k++)
430       medar[k] = 0.;            /* 0-padding at the end */
431   }
432   return fvec_median (tmpvec);
433 }
434
435 smpl_t fvec_median (fvec_t * input) {
436   uint_t n = input->length;
437   smpl_t * arr = (smpl_t *) input->data;
438   uint_t low, high ;
439   uint_t median;
440   uint_t middle, ll, hh;
441
442   low = 0 ; high = n-1 ; median = (low + high) / 2;
443   for (;;) {
444     if (high <= low) /* One element only */
445       return arr[median] ;
446
447     if (high == low + 1) {  /* Two elements only */
448       if (arr[low] > arr[high])
449         ELEM_SWAP(arr[low], arr[high]) ;
450       return arr[median] ;
451     }
452
453     /* Find median of low, middle and high items; swap into position low */
454     middle = (low + high) / 2;
455     if (arr[middle] > arr[high])    ELEM_SWAP(arr[middle], arr[high]);
456     if (arr[low]    > arr[high])    ELEM_SWAP(arr[low],    arr[high]);
457     if (arr[middle] > arr[low])     ELEM_SWAP(arr[middle], arr[low]) ;
458
459     /* Swap low item (now in position middle) into position (low+1) */
460     ELEM_SWAP(arr[middle], arr[low+1]) ;
461
462     /* Nibble from each end towards middle, swapping items when stuck */
463     ll = low + 1;
464     hh = high;
465     for (;;) {
466       do ll++; while (arr[low] > arr[ll]) ;
467       do hh--; while (arr[hh]  > arr[low]) ;
468
469       if (hh < ll)
470         break;
471
472       ELEM_SWAP(arr[ll], arr[hh]) ;
473     }
474
475     /* Swap middle item (in position low) back into correct position */
476     ELEM_SWAP(arr[low], arr[hh]) ;
477
478     /* Re-set active partition */
479     if (hh <= median)
480       low = ll;
481     if (hh >= median)
482       high = hh - 1;
483   }
484 }
485
486 smpl_t fvec_quadratic_peak_pos (const fvec_t * x, uint_t pos) {
487   smpl_t s0, s1, s2; uint_t x0, x2;
488   smpl_t half = .5, two = 2.;
489   if (pos == 0 || pos == x->length - 1) return pos;
490   x0 = (pos < 1) ? pos : pos - 1;
491   x2 = (pos + 1 < x->length) ? pos + 1 : pos;
492   if (x0 == pos) return (x->data[pos] <= x->data[x2]) ? pos : x2;
493   if (x2 == pos) return (x->data[pos] <= x->data[x0]) ? pos : x0;
494   s0 = x->data[x0];
495   s1 = x->data[pos];
496   s2 = x->data[x2];
497   return pos + half * (s0 - s2 ) / (s0 - two * s1 + s2);
498 }
499
500 smpl_t fvec_quadratic_peak_mag (fvec_t *x, smpl_t pos) {
501   smpl_t x0, x1, x2;
502   uint_t index = (uint_t)(pos - .5) + 1;
503   if (pos >= x->length || pos < 0.) return 0.;
504   if ((smpl_t)index == pos) return x->data[index];
505   x0 = x->data[index - 1];
506   x1 = x->data[index];
507   x2 = x->data[index + 1];
508   return x1 - .25 * (x0 - x2) * (pos - index);
509 }
510
511 uint_t fvec_peakpick(const fvec_t * onset, uint_t pos) {
512   uint_t tmp=0;
513   tmp = (onset->data[pos] > onset->data[pos-1]
514       &&  onset->data[pos] > onset->data[pos+1]
515       &&  onset->data[pos] > 0.);
516   return tmp;
517 }
518
519 smpl_t
520 aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf)
521 {
522   smpl_t tmp =
523       s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2);
524   return tmp;
525 }
526
527 smpl_t
528 aubio_freqtomidi (smpl_t freq)
529 {
530   smpl_t midi;
531   if (freq < 2. || freq > 100000.) return 0.; // avoid nans and infs
532   /* log(freq/A-2)/log(2) */
533   midi = freq / 6.875;
534   midi = LOG (midi) / 0.6931471805599453;
535   midi *= 12;
536   midi -= 3;
537   return midi;
538 }
539
540 smpl_t
541 aubio_miditofreq (smpl_t midi)
542 {
543   smpl_t freq;
544   if (midi > 140.) return 0.; // avoid infs
545   freq = (midi + 3.) / 12.;
546   freq = EXP (freq * 0.6931471805599453);
547   freq *= 6.875;
548   return freq;
549 }
550
551 smpl_t
552 aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
553 {
554   smpl_t freq = samplerate / fftsize;
555   return freq * MAX(bin, 0);
556 }
557
558 smpl_t
559 aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
560 {
561   smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize);
562   return aubio_freqtomidi (midi);
563 }
564
565 smpl_t
566 aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize)
567 {
568   smpl_t bin = fftsize / samplerate;
569   return MAX(freq, 0) * bin;
570 }
571
572 smpl_t
573 aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize)
574 {
575   smpl_t freq = aubio_miditofreq (midi);
576   return aubio_freqtobin (freq, samplerate, fftsize);
577 }
578
579 uint_t
580 aubio_is_power_of_two (uint_t a)
581 {
582   if ((a & (a - 1)) == 0) {
583     return 1;
584   } else {
585     return 0;
586   }
587 }
588
589 uint_t
590 aubio_next_power_of_two (uint_t a)
591 {
592   uint_t i = 1;
593   while (i < a) i <<= 1;
594   return i;
595 }
596
597 uint_t
598 aubio_power_of_two_order (uint_t a)
599 {
600   int order = 0;
601   int temp = aubio_next_power_of_two(a);
602   while (temp >>= 1) {
603     ++order;
604   }
605   return order;
606 }
607
608 smpl_t
609 aubio_db_spl (const fvec_t * o)
610 {
611   return 10. * LOG10 (aubio_level_lin (o));
612 }
613
614 uint_t
615 aubio_silence_detection (const fvec_t * o, smpl_t threshold)
616 {
617   return (aubio_db_spl (o) < threshold);
618 }
619
620 smpl_t
621 aubio_level_detection (const fvec_t * o, smpl_t threshold)
622 {
623   smpl_t db_spl = aubio_db_spl (o);
624   if (db_spl < threshold) {
625     return 1.;
626   } else {
627     return db_spl;
628   }
629 }
630
631 smpl_t
632 aubio_zero_crossing_rate (fvec_t * input)
633 {
634   uint_t j;
635   uint_t zcr = 0;
636   for (j = 1; j < input->length; j++) {
637     // previous was strictly negative
638     if (input->data[j - 1] < 0.) {
639       // current is positive or null
640       if (input->data[j] >= 0.) {
641         zcr += 1;
642       }
643       // previous was positive or null
644     } else {
645       // current is strictly negative
646       if (input->data[j] < 0.) {
647         zcr += 1;
648       }
649     }
650   }
651   return zcr / (smpl_t) input->length;
652 }
653
654 void
655 aubio_autocorr (const fvec_t * input, fvec_t * output)
656 {
657   uint_t i, j, length = input->length;
658   smpl_t *data, *acf;
659   smpl_t tmp = 0;
660   data = input->data;
661   acf = output->data;
662   for (i = 0; i < length; i++) {
663     tmp = 0.;
664     for (j = i; j < length; j++) {
665       tmp += data[j - i] * data[j];
666     }
667     acf[i] = tmp / (smpl_t) (length - i);
668   }
669 }
670
671 void
672 aubio_cleanup (void)
673 {
674 #ifdef HAVE_FFTW3F
675   fftwf_cleanup ();
676 #else
677 #ifdef HAVE_FFTW3
678   fftw_cleanup ();
679 #endif
680 #endif
681 }