source: src/mathutils.c @ faeec7c

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since faeec7c was 986131d, checked in by Eduard Müller <mueller.eduard@googlemail.com>, 7 years ago

Intel IPP support for aubio

See emuell/aubio/ intel_ipp2 for details please

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