source: src/mathutils.c @ 81b3910

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

Merge branch 'intel_ipp_pull' of https://github.com/emuell/aubio into emuell-intel_ipp_pull

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