source: src/mathutils.c @ 1a74ac3

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

src/mathutils.c: fix aubio_db_spl, thanks to Eric, closes #15

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