source: src/mathutils.c @ 9e56228

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

src/mathutils.c: fix computation of gauss window

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