source: src/mathutils.c @ a6b5bf1

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since a6b5bf1 was a6b5bf1, checked in by Paul Brossier <piem@piem.org>, 7 years ago

src/aubio_priv.h: include ipp headers from here

  • Property mode set to 100644
File size: 15.9 KB
Line 
1/*
2  Copyright (C) 2003-2014 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
28/** Window types */
29typedef enum
30{
31  aubio_win_ones,
32  aubio_win_rectangle,
33  aubio_win_hamming,
34  aubio_win_hanning,
35  aubio_win_hanningz,
36  aubio_win_blackman,
37  aubio_win_blackman_harris,
38  aubio_win_gaussian,
39  aubio_win_welch,
40  aubio_win_parzen,
41  aubio_win_default = aubio_win_hanningz,
42} aubio_window_type;
43
44fvec_t *
45new_aubio_window (char_t * window_type, uint_t length)
46{
47  fvec_t * win = new_fvec (length);
48  uint_t err;
49  if (win == NULL) {
50    return NULL;
51  }
52  err = fvec_set_window (win, window_type);
53  if (err != 0) {
54    del_fvec(win);
55    return NULL;
56  }
57  return win;
58}
59
60uint_t fvec_set_window (fvec_t *win, char_t *window_type) {
61  smpl_t * w = win->data;
62  uint_t i, size = win->length;
63  aubio_window_type wintype;
64  if (window_type == NULL) {
65      AUBIO_ERR ("window type can not be null.\n");
66      return 1;
67  } else if (strcmp (window_type, "ones") == 0)
68      wintype = aubio_win_ones;
69  else if (strcmp (window_type, "rectangle") == 0)
70      wintype = aubio_win_rectangle;
71  else if (strcmp (window_type, "hamming") == 0)
72      wintype = aubio_win_hamming;
73  else if (strcmp (window_type, "hanning") == 0)
74      wintype = aubio_win_hanning;
75  else if (strcmp (window_type, "hanningz") == 0)
76      wintype = aubio_win_hanningz;
77  else if (strcmp (window_type, "blackman") == 0)
78      wintype = aubio_win_blackman;
79  else if (strcmp (window_type, "blackman_harris") == 0)
80      wintype = aubio_win_blackman_harris;
81  else if (strcmp (window_type, "gaussian") == 0)
82      wintype = aubio_win_gaussian;
83  else if (strcmp (window_type, "welch") == 0)
84      wintype = aubio_win_welch;
85  else if (strcmp (window_type, "parzen") == 0)
86      wintype = aubio_win_parzen;
87  else if (strcmp (window_type, "default") == 0)
88      wintype = aubio_win_default;
89  else {
90      AUBIO_ERR ("unknown window type `%s`.\n", window_type);
91      return 1;
92  }
93  switch(wintype) {
94    case aubio_win_ones:
95      fvec_ones(win);
96      break;
97    case aubio_win_rectangle:
98      fvec_set_all(win, .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.f*i-size)/(size+1.0f));
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  smpl_t tmp = 0.0;
162#if defined(HAVE_INTEL_IPP)
163  #if HAVE_AUBIO_DOUBLE
164    ippsMean_64f(s->data, (int)s->length, &tmp);
165  #else
166    ippsMean_32f(s->data, (int)s->length, &tmp, ippAlgHintFast);
167  #endif
168    return tmp;
169#elif defined(HAVE_ACCELERATE)
170  aubio_vDSP_meanv(s->data, 1, &tmp, s->length);
171  return tmp;
172#else
173  uint_t j;
174  for (j = 0; j < s->length; j++) {
175    tmp += s->data[j];
176  }
177  return tmp / (smpl_t)(s->length);
178#endif
179}
180
181smpl_t
182fvec_sum (fvec_t * s)
183{
184  smpl_t tmp = 0.0;
185#if defined(HAVE_INTEL_IPP)
186  #if HAVE_AUBIO_DOUBLE
187    ippsSum_64f(s->data, (int)s->length, &tmp);
188  #else
189    ippsSum_32f(s->data, (int)s->length, &tmp, ippAlgHintFast);
190  #endif
191#elif defined(HAVE_ACCELERATE)
192  aubio_vDSP_sve(s->data, 1, &tmp, s->length);
193#else
194  uint_t j;
195  for (j = 0; j < s->length; j++) {
196    tmp += s->data[j];
197  }
198#endif
199  return tmp;
200}
201
202smpl_t
203fvec_max (fvec_t * s)
204{
205#if defined(HAVE_INTEL_IPP)
206  smpl_t tmp = 0.;
207  #if HAVE_AUBIO_DOUBLE
208    ippsMax_64f( s->data, (int)s->length, &tmp);
209  #else
210    ippsMax_32f( s->data, (int)s->length, &tmp);
211#endif
212#elif defined(HAVE_ACCELERATE)
213  smpl_t tmp = 0.;
214  aubio_vDSP_maxv( s->data, 1, &tmp, s->length );
215#else
216  uint_t j;
217  smpl_t tmp = s->data[0];
218  for (j = 1; j < s->length; j++) {
219    tmp = (tmp > s->data[j]) ? tmp : s->data[j];
220  }
221#endif
222  return tmp;
223}
224
225smpl_t
226fvec_min (fvec_t * s)
227{
228#if defined(HAVE_INTEL_IPP)
229  smpl_t tmp = 0.;
230  #if HAVE_AUBIO_DOUBLE
231    ippsMin_64f(s->data, (int)s->length, &tmp);
232  #else
233    ippsMin_32f(s->data, (int)s->length, &tmp);
234#endif
235#elif defined(HAVE_ACCELERATE)
236  smpl_t tmp = 0.;
237  aubio_vDSP_minv(s->data, 1, &tmp, s->length);
238#else
239  uint_t j;
240  smpl_t tmp = s->data[0];
241  for (j = 1; j < s->length; j++) {
242    tmp = (tmp < s->data[j]) ? tmp : s->data[j];
243  }
244#endif
245  return tmp;
246}
247
248uint_t
249fvec_min_elem (fvec_t * s)
250{
251#ifndef HAVE_ACCELERATE
252  uint_t j, pos = 0.;
253  smpl_t tmp = s->data[0];
254  for (j = 0; j < s->length; j++) {
255    pos = (tmp < s->data[j]) ? pos : j;
256    tmp = (tmp < s->data[j]) ? tmp : s->data[j];
257  }
258#else
259  smpl_t tmp = 0.;
260  vDSP_Length pos = 0;
261  aubio_vDSP_minvi(s->data, 1, &tmp, &pos, s->length);
262#endif
263  return (uint_t)pos;
264}
265
266uint_t
267fvec_max_elem (fvec_t * s)
268{
269#ifndef HAVE_ACCELERATE
270  uint_t j, pos = 0;
271  smpl_t tmp = 0.0;
272  for (j = 0; j < s->length; j++) {
273    pos = (tmp > s->data[j]) ? pos : j;
274    tmp = (tmp > s->data[j]) ? tmp : s->data[j];
275  }
276#else
277  smpl_t tmp = 0.;
278  vDSP_Length pos = 0;
279  aubio_vDSP_maxvi(s->data, 1, &tmp, &pos, s->length);
280#endif
281  return (uint_t)pos;
282}
283
284void
285fvec_shift (fvec_t * s)
286{
287  uint_t half = s->length / 2, start = half, j;
288  // if length is odd, middle element is moved to the end
289  if (2 * half < s->length) start ++;
290#ifndef HAVE_ATLAS
291  for (j = 0; j < half; j++) {
292    ELEM_SWAP (s->data[j], s->data[j + start]);
293  }
294#else
295  aubio_cblas_swap(half, s->data, 1, s->data + start, 1);
296#endif
297  if (start != half) {
298    for (j = 0; j < half; j++) {
299      ELEM_SWAP (s->data[j + start - 1], s->data[j + start]);
300    }
301  }
302}
303
304void
305fvec_ishift (fvec_t * s)
306{
307  uint_t half = s->length / 2, start = half, j;
308  // if length is odd, middle element is moved to the beginning
309  if (2 * half < s->length) start ++;
310#ifndef HAVE_ATLAS
311  for (j = 0; j < half; j++) {
312    ELEM_SWAP (s->data[j], s->data[j + start]);
313  }
314#else
315  aubio_cblas_swap(half, s->data, 1, s->data + start, 1);
316#endif
317  if (start != half) {
318    for (j = 0; j < half; j++) {
319      ELEM_SWAP (s->data[half], s->data[j]);
320    }
321  }
322}
323
324void fvec_push(fvec_t *in, smpl_t new_elem) {
325  uint_t i;
326  for (i = 0; i < in->length - 1; i++) {
327    in->data[i] = in->data[i + 1];
328  }
329  in->data[in->length - 1] = new_elem;
330}
331
332void fvec_clamp(fvec_t *in, smpl_t absmax) {
333  uint_t i;
334  for (i = 0; i < in->length; i++) {
335    if (in->data[i] > 0 && in->data[i] > ABS(absmax)) {
336      in->data[i] = absmax;
337    } else if (in->data[i] < 0 && in->data[i] < -ABS(absmax)) {
338      in->data[i] = -absmax;
339    }
340  }
341}
342
343smpl_t
344aubio_level_lin (const fvec_t * f)
345{
346  smpl_t energy = 0.;
347#ifndef HAVE_ATLAS
348  uint_t j;
349  for (j = 0; j < f->length; j++) {
350    energy += SQR (f->data[j]);
351  }
352#else
353  energy = aubio_cblas_dot(f->length, f->data, 1, f->data, 1);
354#endif
355  return energy / f->length;
356}
357
358smpl_t
359fvec_local_hfc (fvec_t * v)
360{
361  smpl_t hfc = 0.;
362  uint_t j;
363  for (j = 0; j < v->length; j++) {
364    hfc += (j + 1) * v->data[j];
365  }
366  return hfc;
367}
368
369void
370fvec_min_removal (fvec_t * v)
371{
372  smpl_t v_min = fvec_min (v);
373  fvec_add (v,  - v_min );
374}
375
376smpl_t
377fvec_alpha_norm (fvec_t * o, smpl_t alpha)
378{
379  uint_t j;
380  smpl_t tmp = 0.;
381  for (j = 0; j < o->length; j++) {
382    tmp += POW (ABS (o->data[j]), alpha);
383  }
384  return POW (tmp / o->length, 1. / alpha);
385}
386
387void
388fvec_alpha_normalise (fvec_t * o, smpl_t alpha)
389{
390  uint_t j;
391  smpl_t norm = fvec_alpha_norm (o, alpha);
392  for (j = 0; j < o->length; j++) {
393    o->data[j] /= norm;
394  }
395}
396
397void
398fvec_add (fvec_t * o, smpl_t val)
399{
400  uint_t j;
401  for (j = 0; j < o->length; j++) {
402    o->data[j] += val;
403  }
404}
405
406void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
407    uint_t post, uint_t pre) {
408  uint_t length = vec->length, j;
409  for (j=0;j<length;j++) {
410    vec->data[j] -= fvec_moving_thres(vec, tmp, post, pre, j);
411  }
412}
413
414smpl_t
415fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec,
416    uint_t post, uint_t pre, uint_t pos)
417{
418  uint_t k;
419  smpl_t *medar = (smpl_t *) tmpvec->data;
420  uint_t win_length = post + pre + 1;
421  uint_t length = vec->length;
422  /* post part of the buffer does not exist */
423  if (pos < post + 1) {
424    for (k = 0; k < post + 1 - pos; k++)
425      medar[k] = 0.;            /* 0-padding at the beginning */
426    for (k = post + 1 - pos; k < win_length; k++)
427      medar[k] = vec->data[k + pos - post];
428    /* the buffer is fully defined */
429  } else if (pos + pre < length) {
430    for (k = 0; k < win_length; k++)
431      medar[k] = vec->data[k + pos - post];
432    /* pre part of the buffer does not exist */
433  } else {
434    for (k = 0; k < length - pos + post; k++)
435      medar[k] = vec->data[k + pos - post];
436    for (k = length - pos + post; k < win_length; k++)
437      medar[k] = 0.;            /* 0-padding at the end */
438  }
439  return fvec_median (tmpvec);
440}
441
442smpl_t fvec_median (fvec_t * input) {
443  uint_t n = input->length;
444  smpl_t * arr = (smpl_t *) input->data;
445  uint_t low, high ;
446  uint_t median;
447  uint_t middle, ll, hh;
448
449  low = 0 ; high = n-1 ; median = (low + high) / 2;
450  for (;;) {
451    if (high <= low) /* One element only */
452      return arr[median] ;
453
454    if (high == low + 1) {  /* Two elements only */
455      if (arr[low] > arr[high])
456        ELEM_SWAP(arr[low], arr[high]) ;
457      return arr[median] ;
458    }
459
460    /* Find median of low, middle and high items; swap into position low */
461    middle = (low + high) / 2;
462    if (arr[middle] > arr[high])    ELEM_SWAP(arr[middle], arr[high]);
463    if (arr[low]    > arr[high])    ELEM_SWAP(arr[low],    arr[high]);
464    if (arr[middle] > arr[low])     ELEM_SWAP(arr[middle], arr[low]) ;
465
466    /* Swap low item (now in position middle) into position (low+1) */
467    ELEM_SWAP(arr[middle], arr[low+1]) ;
468
469    /* Nibble from each end towards middle, swapping items when stuck */
470    ll = low + 1;
471    hh = high;
472    for (;;) {
473      do ll++; while (arr[low] > arr[ll]) ;
474      do hh--; while (arr[hh]  > arr[low]) ;
475
476      if (hh < ll)
477        break;
478
479      ELEM_SWAP(arr[ll], arr[hh]) ;
480    }
481
482    /* Swap middle item (in position low) back into correct position */
483    ELEM_SWAP(arr[low], arr[hh]) ;
484
485    /* Re-set active partition */
486    if (hh <= median)
487      low = ll;
488    if (hh >= median)
489      high = hh - 1;
490  }
491}
492
493smpl_t fvec_quadratic_peak_pos (const fvec_t * x, uint_t pos) {
494  smpl_t s0, s1, s2; uint_t x0, x2;
495  smpl_t half = .5, two = 2.;
496  if (pos == 0 || pos == x->length - 1) return pos;
497  x0 = (pos < 1) ? pos : pos - 1;
498  x2 = (pos + 1 < x->length) ? pos + 1 : pos;
499  if (x0 == pos) return (x->data[pos] <= x->data[x2]) ? pos : x2;
500  if (x2 == pos) return (x->data[pos] <= x->data[x0]) ? pos : x0;
501  s0 = x->data[x0];
502  s1 = x->data[pos];
503  s2 = x->data[x2];
504  return pos + half * (s0 - s2 ) / (s0 - two * s1 + s2);
505}
506
507smpl_t fvec_quadratic_peak_mag (fvec_t *x, smpl_t pos) {
508  smpl_t x0, x1, x2;
509  uint_t index = (uint_t)(pos - .5) + 1;
510  if (pos >= x->length || pos < 0.) return 0.;
511  if ((smpl_t)index == pos) return x->data[index];
512  x0 = x->data[index - 1];
513  x1 = x->data[index];
514  x2 = x->data[index + 1];
515  return x1 - .25 * (x0 - x2) * (pos - index);
516}
517
518uint_t fvec_peakpick(const fvec_t * onset, uint_t pos) {
519  uint_t tmp=0;
520  tmp = (onset->data[pos] > onset->data[pos-1]
521      &&  onset->data[pos] > onset->data[pos+1]
522      &&  onset->data[pos] > 0.);
523  return tmp;
524}
525
526smpl_t
527aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf)
528{
529  smpl_t tmp =
530      s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2);
531  return tmp;
532}
533
534smpl_t
535aubio_freqtomidi (smpl_t freq)
536{
537  smpl_t midi;
538  if (freq < 2. || freq > 100000.) return 0.; // avoid nans and infs
539  /* log(freq/A-2)/log(2) */
540  midi = freq / 6.875;
541  midi = LOG (midi) / 0.69314718055995;
542  midi *= 12;
543  midi -= 3;
544  return midi;
545}
546
547smpl_t
548aubio_miditofreq (smpl_t midi)
549{
550  smpl_t freq;
551  if (midi > 140.) return 0.; // avoid infs
552  freq = (midi + 3.) / 12.;
553  freq = EXP (freq * 0.69314718055995);
554  freq *= 6.875;
555  return freq;
556}
557
558smpl_t
559aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
560{
561  smpl_t freq = samplerate / fftsize;
562  return freq * MAX(bin, 0);
563}
564
565smpl_t
566aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
567{
568  smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize);
569  return aubio_freqtomidi (midi);
570}
571
572smpl_t
573aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize)
574{
575  smpl_t bin = fftsize / samplerate;
576  return MAX(freq, 0) * bin;
577}
578
579smpl_t
580aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize)
581{
582  smpl_t freq = aubio_miditofreq (midi);
583  return aubio_freqtobin (freq, samplerate, fftsize);
584}
585
586uint_t
587aubio_is_power_of_two (uint_t a)
588{
589  if ((a & (a - 1)) == 0) {
590    return 1;
591  } else {
592    return 0;
593  }
594}
595
596uint_t
597aubio_next_power_of_two (uint_t a)
598{
599  uint_t i = 1;
600  while (i < a) i <<= 1;
601  return i;
602}
603
604uint_t
605aubio_power_of_two_order (uint_t a)
606{
607  int order = 0; 
608  int temp = aubio_next_power_of_two(a);
609  while (temp >>= 1) {
610    ++order;
611  }
612  return order;
613}
614
615smpl_t
616aubio_db_spl (const fvec_t * o)
617{
618  return 10. * LOG10 (aubio_level_lin (o));
619}
620
621uint_t
622aubio_silence_detection (const fvec_t * o, smpl_t threshold)
623{
624  return (aubio_db_spl (o) < threshold);
625}
626
627smpl_t
628aubio_level_detection (const fvec_t * o, smpl_t threshold)
629{
630  smpl_t db_spl = aubio_db_spl (o);
631  if (db_spl < threshold) {
632    return 1.;
633  } else {
634    return db_spl;
635  }
636}
637
638smpl_t
639aubio_zero_crossing_rate (fvec_t * input)
640{
641  uint_t j;
642  uint_t zcr = 0;
643  for (j = 1; j < input->length; j++) {
644    // previous was strictly negative
645    if (input->data[j - 1] < 0.) {
646      // current is positive or null
647      if (input->data[j] >= 0.) {
648        zcr += 1;
649      }
650      // previous was positive or null
651    } else {
652      // current is strictly negative
653      if (input->data[j] < 0.) {
654        zcr += 1;
655      }
656    }
657  }
658  return zcr / (smpl_t) input->length;
659}
660
661void
662aubio_autocorr (const fvec_t * input, fvec_t * output)
663{
664  uint_t i, j, length = input->length;
665  smpl_t *data, *acf;
666  smpl_t tmp = 0;
667  data = input->data;
668  acf = output->data;
669  for (i = 0; i < length; i++) {
670    tmp = 0.;
671    for (j = i; j < length; j++) {
672      tmp += data[j - i] * data[j];
673    }
674    acf[i] = tmp / (smpl_t) (length - i);
675  }
676}
677
678void
679aubio_init (void)
680{
681/* initialize intel IPP */
682#ifdef HAVE_INTEL_IPP
683  IppStatus status = ippInit();
684  if (status != ippStsNoErr) {
685    fprintf (stderr, "Error: failed to initialize Intel IPP - status %d\n", status);
686  }
687#endif
688}
689
690void
691aubio_cleanup (void)
692{
693#ifdef HAVE_FFTW3F
694  fftwf_cleanup ();
695#else
696#ifdef HAVE_FFTW3
697  fftw_cleanup ();
698#endif
699#endif
700}
Note: See TracBrowser for help on using the repository browser.