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