source: src/mathutils.c @ 10a5413

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

src/mathutils.c: add aubio_is_power_of_two and aubio_next_power_of_two

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