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