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