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