source: src/mathutils.c @ 78c21cf

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 78c21cf was 5644069, checked in by Paul Brossier <piem@piem.org>, 10 years ago

src/mathutils.c: improve build with -Wdeclaration-after-statement

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