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
RevLine 
[96fb8ad]1/*
[7380327]2  Copyright (C) 2003-2014 Paul Brossier <piem@aubio.org>
[96fb8ad]3
[a6db140]4  This file is part of aubio.
[96fb8ad]5
[a6db140]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.
[96fb8ad]10
[a6db140]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/>.
[96fb8ad]18
19*/
20
21/* see in mathutils.h for doc */
22
23#include "aubio_priv.h"
[6c7d49b]24#include "fvec.h"
[96fb8ad]25#include "mathutils.h"
[83963b3]26#include "musicutils.h"
[96fb8ad]27
[986131d]28#if defined HAVE_INTEL_IPP
29#include <ippcore.h>
30#include <ippvm.h>
31#include <ipps.h>
32#endif
33
[407bba9]34/** Window types */
35typedef enum
36{
[e1d9cc3]37  aubio_win_ones,
[407bba9]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
[eb7f743]50fvec_t *
[33cd81f]51new_aubio_window (char_t * window_type, uint_t length)
[eb7f743]52{
[33cd81f]53  fvec_t * win = new_fvec (length);
[c21acb9]54  uint_t err;
[ca45e58]55  if (win == NULL) {
56    return NULL;
57  }
[c21acb9]58  err = fvec_set_window (win, window_type);
[ca45e58]59  if (err != 0) {
60    del_fvec(win);
61    return NULL;
62  }
[33cd81f]63  return win;
64}
65
66uint_t fvec_set_window (fvec_t *win, char_t *window_type) {
[8e5c051]67  smpl_t * w = win->data;
[33cd81f]68  uint_t i, size = win->length;
[407bba9]69  aubio_window_type wintype;
[33cd81f]70  if (window_type == NULL) {
71      AUBIO_ERR ("window type can not be null.\n");
72      return 1;
[e1d9cc3]73  } else if (strcmp (window_type, "ones") == 0)
74      wintype = aubio_win_ones;
75  else if (strcmp (window_type, "rectangle") == 0)
[407bba9]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 {
[33cd81f]96      AUBIO_ERR ("unknown window type `%s`.\n", window_type);
97      return 1;
[407bba9]98  }
[96fb8ad]99  switch(wintype) {
[e1d9cc3]100    case aubio_win_ones:
101      fvec_ones(win);
102      break;
[b4b0324]103    case aubio_win_rectangle:
[e1d9cc3]104      fvec_set_all(win, .5);
[96fb8ad]105      break;
[b4b0324]106    case aubio_win_hamming:
[96fb8ad]107      for (i=0;i<size;i++)
108        w[i] = 0.54 - 0.46 * COS(TWO_PI * i / (size));
109      break;
[b4b0324]110    case aubio_win_hanning:
[96fb8ad]111      for (i=0;i<size;i++)
112        w[i] = 0.5 - (0.5 * COS(TWO_PI * i / (size)));
113      break;
[b4b0324]114    case aubio_win_hanningz:
[96fb8ad]115      for (i=0;i<size;i++)
116        w[i] = 0.5 * (1.0 - COS(TWO_PI * i / (size)));
117      break;
[b4b0324]118    case aubio_win_blackman:
[96fb8ad]119      for (i=0;i<size;i++)
120        w[i] = 0.42
121          - 0.50 * COS(    TWO_PI*i/(size-1.0))
[ade9afe]122          + 0.08 * COS(2.0*TWO_PI*i/(size-1.0));
[96fb8ad]123      break;
[b4b0324]124    case aubio_win_blackman_harris:
[96fb8ad]125      for (i=0;i<size;i++)
[ade9afe]126        w[i] = 0.35875
[96fb8ad]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;
[b4b0324]131    case aubio_win_gaussian:
[9e56228]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      }
[96fb8ad]142      break;
[b4b0324]143    case aubio_win_welch:
[96fb8ad]144      for (i=0;i<size;i++)
[f9d5346]145        w[i] = 1.0 - SQR((2.*i-size)/(size+1.0));
[96fb8ad]146      break;
[b4b0324]147    case aubio_win_parzen:
[96fb8ad]148      for (i=0;i<size;i++)
[053495b]149        w[i] = 1.0 - ABS((2.f*i-size)/(size+1.0f));
[96fb8ad]150      break;
151    default:
152      break;
153  }
[33cd81f]154  return 0;
[96fb8ad]155}
156
[eb7f743]157smpl_t
158aubio_unwrap2pi (smpl_t phase)
159{
[96fb8ad]160  /* mod(phase+pi,-2pi)+pi */
[eb7f743]161  return phase + TWO_PI * (1. + FLOOR (-(phase + PI) / TWO_PI));
[96fb8ad]162}
163
[eb7f743]164smpl_t
165fvec_mean (fvec_t * s)
166{
[56ef7e1]167  smpl_t tmp = 0.0;
[986131d]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
[98d8d2b]179  uint_t j;
[8e5c051]180  for (j = 0; j < s->length; j++) {
181    tmp += s->data[j];
182  }
[986131d]183  return tmp / (smpl_t)(s->length);
184#endif
[56ef7e1]185}
186
187smpl_t
[eb7f743]188fvec_sum (fvec_t * s)
189{
[acf7d30]190  smpl_t tmp = 0.0;
[986131d]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
[98d8d2b]200  uint_t j;
[8e5c051]201  for (j = 0; j < s->length; j++) {
202    tmp += s->data[j];
[eb7f743]203  }
[986131d]204#endif
[96fb8ad]205  return tmp;
206}
207
[eb7f743]208smpl_t
209fvec_max (fvec_t * s)
210{
[986131d]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
[8e5c051]222  uint_t j;
[986131d]223  smpl_t tmp = s->data[0];
224  for (j = 1; j < s->length; j++) {
[8e5c051]225    tmp = (tmp > s->data[j]) ? tmp : s->data[j];
[eb7f743]226  }
[7c07af2]227#endif
[96fb8ad]228  return tmp;
229}
230
[eb7f743]231smpl_t
232fvec_min (fvec_t * s)
233{
[986131d]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
[8e5c051]245  uint_t j;
246  smpl_t tmp = s->data[0];
[986131d]247  for (j = 1; j < s->length; j++) {
[8e5c051]248    tmp = (tmp < s->data[j]) ? tmp : s->data[j];
[eb7f743]249  }
[7c07af2]250#endif
[96fb8ad]251  return tmp;
252}
253
[eb7f743]254uint_t
255fvec_min_elem (fvec_t * s)
256{
[7c07af2]257#ifndef HAVE_ACCELERATE
[8e5c051]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];
[eb7f743]263  }
[7c07af2]264#else
265  smpl_t tmp = 0.;
[d6befbe]266  vDSP_Length pos = 0;
267  aubio_vDSP_minvi(s->data, 1, &tmp, &pos, s->length);
[7c07af2]268#endif
[d6befbe]269  return (uint_t)pos;
[96fb8ad]270}
271
[eb7f743]272uint_t
273fvec_max_elem (fvec_t * s)
274{
[7c07af2]275#ifndef HAVE_ACCELERATE
[8e5c051]276  uint_t j, pos = 0;
[acf7d30]277  smpl_t tmp = 0.0;
[8e5c051]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];
[eb7f743]281  }
[7c07af2]282#else
283  smpl_t tmp = 0.;
[d6befbe]284  vDSP_Length pos = 0;
285  aubio_vDSP_maxvi(s->data, 1, &tmp, &pos, s->length);
[7c07af2]286#endif
[d6befbe]287  return (uint_t)pos;
[96fb8ad]288}
289
[eb7f743]290void
291fvec_shift (fvec_t * s)
292{
[4bf3731]293  uint_t half = s->length / 2, start = half, j;
[116bd1b]294  // if length is odd, middle element is moved to the end
295  if (2 * half < s->length) start ++;
[0a0d9b0]296#ifndef HAVE_ATLAS
[4bf3731]297  for (j = 0; j < half; j++) {
[116bd1b]298    ELEM_SWAP (s->data[j], s->data[j + start]);
[eb7f743]299  }
[8982328]300#else
[116bd1b]301  aubio_cblas_swap(half, s->data, 1, s->data + start, 1);
[8982328]302#endif
[116bd1b]303  if (start != half) {
[4bf3731]304    for (j = 0; j < half; j++) {
[116bd1b]305      ELEM_SWAP (s->data[j + start - 1], s->data[j + start]);
306    }
307  }
308}
309
310void
311fvec_ishift (fvec_t * s)
312{
[4bf3731]313  uint_t half = s->length / 2, start = half, j;
[116bd1b]314  // if length is odd, middle element is moved to the beginning
315  if (2 * half < s->length) start ++;
316#ifndef HAVE_ATLAS
[4bf3731]317  for (j = 0; j < half; j++) {
[116bd1b]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) {
[4bf3731]324    for (j = 0; j < half; j++) {
[116bd1b]325      ELEM_SWAP (s->data[half], s->data[j]);
326    }
327  }
[96fb8ad]328}
329
[ee8a57c]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
[b799241]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
[eb7f743]349smpl_t
[ad1df9b]350aubio_level_lin (const fvec_t * f)
[eb7f743]351{
352  smpl_t energy = 0.;
[0a0d9b0]353#ifndef HAVE_ATLAS
[8e5c051]354  uint_t j;
355  for (j = 0; j < f->length; j++) {
356    energy += SQR (f->data[j]);
[eb7f743]357  }
[8982328]358#else
359  energy = aubio_cblas_dot(f->length, f->data, 1, f->data, 1);
360#endif
[1a74ac3]361  return energy / f->length;
[96fb8ad]362}
363
[eb7f743]364smpl_t
365fvec_local_hfc (fvec_t * v)
366{
367  smpl_t hfc = 0.;
[8e5c051]368  uint_t j;
369  for (j = 0; j < v->length; j++) {
370    hfc += (j + 1) * v->data[j];
[eb7f743]371  }
372  return hfc;
[96fb8ad]373}
374
[eb7f743]375void
376fvec_min_removal (fvec_t * v)
377{
378  smpl_t v_min = fvec_min (v);
379  fvec_add (v,  - v_min );
[96fb8ad]380}
381
[eb7f743]382smpl_t
383fvec_alpha_norm (fvec_t * o, smpl_t alpha)
[c0b295c]384{
[8e5c051]385  uint_t j;
[eb7f743]386  smpl_t tmp = 0.;
[8e5c051]387  for (j = 0; j < o->length; j++) {
388    tmp += POW (ABS (o->data[j]), alpha);
[c0b295c]389  }
[eb7f743]390  return POW (tmp / o->length, 1. / alpha);
[96fb8ad]391}
392
[eb7f743]393void
394fvec_alpha_normalise (fvec_t * o, smpl_t alpha)
395{
[8e5c051]396  uint_t j;
[eb7f743]397  smpl_t norm = fvec_alpha_norm (o, alpha);
[8e5c051]398  for (j = 0; j < o->length; j++) {
399    o->data[j] /= norm;
[96fb8ad]400  }
401}
402
[eb7f743]403void
404fvec_add (fvec_t * o, smpl_t val)
405{
[8e5c051]406  uint_t j;
407  for (j = 0; j < o->length; j++) {
408    o->data[j] += val;
[96fb8ad]409  }
410}
411
[5c4ec3c]412void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
[8e5c051]413    uint_t post, uint_t pre) {
414  uint_t length = vec->length, j;
[96fb8ad]415  for (j=0;j<length;j++) {
[8e5c051]416    vec->data[j] -= fvec_moving_thres(vec, tmp, post, pre, j);
[96fb8ad]417  }
418}
419
[eb7f743]420smpl_t
421fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec,
[8e5c051]422    uint_t post, uint_t pre, uint_t pos)
[eb7f743]423{
[8e5c051]424  uint_t k;
425  smpl_t *medar = (smpl_t *) tmpvec->data;
[eb7f743]426  uint_t win_length = post + pre + 1;
427  uint_t length = vec->length;
[96fb8ad]428  /* post part of the buffer does not exist */
[eb7f743]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++)
[8e5c051]433      medar[k] = vec->data[k + pos - post];
[eb7f743]434    /* the buffer is fully defined */
435  } else if (pos + pre < length) {
436    for (k = 0; k < win_length; k++)
[8e5c051]437      medar[k] = vec->data[k + pos - post];
[eb7f743]438    /* pre part of the buffer does not exist */
[96fb8ad]439  } else {
[eb7f743]440    for (k = 0; k < length - pos + post; k++)
[8e5c051]441      medar[k] = vec->data[k + pos - post];
[eb7f743]442    for (k = length - pos + post; k < win_length; k++)
443      medar[k] = 0.;            /* 0-padding at the end */
[ade9afe]444  }
[8e5c051]445  return fvec_median (tmpvec);
[96fb8ad]446}
447
[8e5c051]448smpl_t fvec_median (fvec_t * input) {
[96fb8ad]449  uint_t n = input->length;
[8e5c051]450  smpl_t * arr = (smpl_t *) input->data;
[96fb8ad]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
[ad1df9b]499smpl_t fvec_quadratic_peak_pos (const fvec_t * x, uint_t pos) {
[5644069]500  smpl_t s0, s1, s2; uint_t x0, x2;
[6ab6611]501  smpl_t half = .5, two = 2.;
[acd97d1]502  if (pos == 0 || pos == x->length - 1) return pos;
[5644069]503  x0 = (pos < 1) ? pos : pos - 1;
504  x2 = (pos + 1 < x->length) ? pos + 1 : pos;
[9499eefb]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];
[6ab6611]510  return pos + half * (s0 - s2 ) / (s0 - two * s1 + s2);
[9499eefb]511}
512
[7380327]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
[ad1df9b]524uint_t fvec_peakpick(const fvec_t * onset, uint_t pos) {
[8e5c051]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.);
[ade9afe]529  return tmp;
[96fb8ad]530}
531
[eb7f743]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{
[5644069]543  smpl_t midi;
[037319a]544  if (freq < 2. || freq > 100000.) return 0.; // avoid nans and infs
[ade9afe]545  /* log(freq/A-2)/log(2) */
[5644069]546  midi = freq / 6.875;
[eb7f743]547  midi = LOG (midi) / 0.69314718055995;
[ade9afe]548  midi *= 12;
549  midi -= 3;
550  return midi;
[79c2e52]551}
552
[eb7f743]553smpl_t
554aubio_miditofreq (smpl_t midi)
555{
[5644069]556  smpl_t freq;
[037319a]557  if (midi > 140.) return 0.; // avoid infs
[5644069]558  freq = (midi + 3.) / 12.;
[eb7f743]559  freq = EXP (freq * 0.69314718055995);
[ade9afe]560  freq *= 6.875;
561  return freq;
[96fb8ad]562}
563
[eb7f743]564smpl_t
565aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
566{
567  smpl_t freq = samplerate / fftsize;
[c965b33]568  return freq * MAX(bin, 0);
[96fb8ad]569}
570
[eb7f743]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);
[96fb8ad]576}
577
[eb7f743]578smpl_t
579aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize)
580{
581  smpl_t bin = fftsize / samplerate;
[c965b33]582  return MAX(freq, 0) * bin;
[79c2e52]583}
584
[eb7f743]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);
[79c2e52]590}
591
[10a5413]592uint_t
[41f4c5b]593aubio_is_power_of_two (uint_t a)
594{
595  if ((a & (a - 1)) == 0) {
[10a5413]596    return 1;
597  } else {
[41f4c5b]598    return 0;
[10a5413]599  }
600}
601
602uint_t
[41f4c5b]603aubio_next_power_of_two (uint_t a)
604{
[7581185]605  uint_t i = 1;
606  while (i < a) i <<= 1;
607  return i;
[10a5413]608}
609
[986131d]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
[eb7f743]621smpl_t
[ad1df9b]622aubio_db_spl (const fvec_t * o)
[eb7f743]623{
[5b41ef9]624  return 10. * LOG10 (aubio_level_lin (o));
[96fb8ad]625}
626
[eb7f743]627uint_t
[ad1df9b]628aubio_silence_detection (const fvec_t * o, smpl_t threshold)
[eb7f743]629{
630  return (aubio_db_spl (o) < threshold);
631}
[96fb8ad]632
[eb7f743]633smpl_t
[24e9b0a]634aubio_level_detection (const fvec_t * o, smpl_t threshold)
[eb7f743]635{
636  smpl_t db_spl = aubio_db_spl (o);
637  if (db_spl < threshold) {
[ade9afe]638    return 1.;
[eb7f743]639  } else {
640    return db_spl;
641  }
[96fb8ad]642}
[a0fd4e4]643
[eb7f743]644smpl_t
645aubio_zero_crossing_rate (fvec_t * input)
646{
[8e5c051]647  uint_t j;
[fff2bee]648  uint_t zcr = 0;
[eb7f743]649  for (j = 1; j < input->length; j++) {
[7e204d01]650    // previous was strictly negative
[8e5c051]651    if (input->data[j - 1] < 0.) {
[7e204d01]652      // current is positive or null
[8e5c051]653      if (input->data[j] >= 0.) {
[7e204d01]654        zcr += 1;
655      }
[eb7f743]656      // previous was positive or null
[7e204d01]657    } else {
658      // current is strictly negative
[8e5c051]659      if (input->data[j] < 0.) {
[fff2bee]660        zcr += 1;
661      }
662    }
663  }
[eb7f743]664  return zcr / (smpl_t) input->length;
[fff2bee]665}
666
[eb7f743]667void
[ad1df9b]668aubio_autocorr (const fvec_t * input, fvec_t * output)
[eb7f743]669{
[8e5c051]670  uint_t i, j, length = input->length;
[eb7f743]671  smpl_t *data, *acf;
672  smpl_t tmp = 0;
[8e5c051]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];
[ade9afe]679    }
[8e5c051]680    acf[i] = tmp / (smpl_t) (length - i);
[ade9afe]681  }
[a0fd4e4]682}
683
[eb7f743]684void
[986131d]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
[eb7f743]697aubio_cleanup (void)
698{
[729a3c0]699#ifdef HAVE_FFTW3F
[eb7f743]700  fftwf_cleanup ();
[729a3c0]701#else
702#ifdef HAVE_FFTW3
703  fftw_cleanup ();
[714380d]704#endif
705#endif
706}
Note: See TracBrowser for help on using the repository browser.