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