source: src/mathutils.c @ 407bba9

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

src/mathutils.c: use a string for window type, making enum private

  • Property mode set to 100644
File size: 12.5 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
[407bba9]28
29/** Window types */
30typedef enum
31{
32  aubio_win_rectangle,
33  aubio_win_hamming,
34  aubio_win_hanning,
35  aubio_win_hanningz,
36  aubio_win_blackman,
37  aubio_win_blackman_harris,
38  aubio_win_gaussian,
39  aubio_win_welch,
40  aubio_win_parzen,
41  aubio_win_default = aubio_win_hanningz,
42} aubio_window_type;
43
[eb7f743]44fvec_t *
[407bba9]45new_aubio_window (char_t * window_type, uint_t size)
[eb7f743]46{
[d84d19e]47  // create fvec of size x 1 channel
48  fvec_t * win = new_fvec( size, 1);
49  smpl_t * w = win->data[0];
[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{
135  uint_t i, j;
[acf7d30]136  smpl_t tmp = 0.0;
[eb7f743]137  for (i = 0; i < s->channels; i++)
138    for (j = 0; j < s->length; j++)
[96fb8ad]139      tmp += s->data[i][j];
[eb7f743]140  return tmp / (smpl_t) (s->length);
[96fb8ad]141}
142
[eb7f743]143smpl_t
144fvec_sum (fvec_t * s)
145{
146  uint_t i, j;
[acf7d30]147  smpl_t tmp = 0.0;
[eb7f743]148  for (i = 0; i < s->channels; i++) {
149    for (j = 0; j < s->length; j++) {
[96fb8ad]150      tmp += s->data[i][j];
[eb7f743]151    }
152  }
[96fb8ad]153  return tmp;
154}
155
[eb7f743]156smpl_t
157fvec_max (fvec_t * s)
158{
159  uint_t i, j;
[acf7d30]160  smpl_t tmp = 0.0;
[eb7f743]161  for (i = 0; i < s->channels; i++) {
162    for (j = 0; j < s->length; j++) {
163      tmp = (tmp > s->data[i][j]) ? tmp : s->data[i][j];
164    }
165  }
[96fb8ad]166  return tmp;
167}
168
[eb7f743]169smpl_t
170fvec_min (fvec_t * s)
171{
172  uint_t i, j;
[96fb8ad]173  smpl_t tmp = s->data[0][0];
[eb7f743]174  for (i = 0; i < s->channels; i++) {
175    for (j = 0; j < s->length; j++) {
176      tmp = (tmp < s->data[i][j]) ? tmp : s->data[i][j];
177    }
178  }
[96fb8ad]179  return tmp;
180}
181
[eb7f743]182uint_t
183fvec_min_elem (fvec_t * s)
184{
[41f4c5b]185  uint_t i, j, pos = 0.;
[96fb8ad]186  smpl_t tmp = s->data[0][0];
[eb7f743]187  for (i = 0; i < s->channels; i++) {
188    for (j = 0; j < s->length; j++) {
189      pos = (tmp < s->data[i][j]) ? pos : j;
190      tmp = (tmp < s->data[i][j]) ? tmp : s->data[i][j];
[96fb8ad]191    }
[eb7f743]192  }
[96fb8ad]193  return pos;
194}
195
[eb7f743]196uint_t
197fvec_max_elem (fvec_t * s)
198{
[41f4c5b]199  uint_t i, j, pos = 0;
[acf7d30]200  smpl_t tmp = 0.0;
[eb7f743]201  for (i = 0; i < s->channels; i++) {
202    for (j = 0; j < s->length; j++) {
203      pos = (tmp > s->data[i][j]) ? pos : j;
204      tmp = (tmp > s->data[i][j]) ? tmp : s->data[i][j];
[96fb8ad]205    }
[eb7f743]206  }
[96fb8ad]207  return pos;
208}
209
[eb7f743]210void
211fvec_shift (fvec_t * s)
212{
213  uint_t i, j;
214  for (i = 0; i < s->channels; i++) {
215    for (j = 0; j < s->length / 2; j++) {
216      ELEM_SWAP (s->data[i][j], s->data[i][j + s->length / 2]);
[96fb8ad]217    }
[eb7f743]218  }
[96fb8ad]219}
220
[eb7f743]221smpl_t
222fvec_local_energy (fvec_t * f)
223{
224  smpl_t energy = 0.;
225  uint_t i, j;
226  for (i = 0; i < f->channels; i++) {
227    for (j = 0; j < f->length; j++) {
228      energy += SQR (f->data[i][j]);
229    }
230  }
231  return energy;
[96fb8ad]232}
233
[eb7f743]234smpl_t
235fvec_local_hfc (fvec_t * v)
236{
237  smpl_t hfc = 0.;
238  uint_t i, j;
239  for (i = 0; i < v->channels; i++) {
240    for (j = 0; j < v->length; j++) {
241      hfc += (i + 1) * v->data[i][j];
242    }
243  }
244  return hfc;
[96fb8ad]245}
246
[eb7f743]247void
248fvec_min_removal (fvec_t * v)
249{
250  smpl_t v_min = fvec_min (v);
251  fvec_add (v,  - v_min );
[96fb8ad]252}
253
[eb7f743]254smpl_t
255fvec_alpha_norm (fvec_t * o, smpl_t alpha)
[c0b295c]256{
257  uint_t i, j;
[eb7f743]258  smpl_t tmp = 0.;
[c0b295c]259  for (i = 0; i < o->channels; i++) {
260    for (j = 0; j < o->length; j++) {
[eb7f743]261      tmp += POW (ABS (o->data[i][j]), alpha);
[96fb8ad]262    }
[c0b295c]263  }
[eb7f743]264  return POW (tmp / o->length, 1. / alpha);
[96fb8ad]265}
266
[eb7f743]267void
268fvec_alpha_normalise (fvec_t * o, smpl_t alpha)
269{
270  uint_t i, j;
271  smpl_t norm = fvec_alpha_norm (o, alpha);
272  for (i = 0; i < o->channels; i++) {
273    for (j = 0; j < o->length; j++) {
274      o->data[i][j] /= norm;
275    }
[96fb8ad]276  }
277}
278
[eb7f743]279void
280fvec_add (fvec_t * o, smpl_t val)
281{
282  uint_t i, j;
283  for (i = 0; i < o->channels; i++) {
284    for (j = 0; j < o->length; j++) {
285      o->data[i][j] += val;
286    }
[96fb8ad]287  }
288}
289
[5c4ec3c]290void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
[ade9afe]291    uint_t post, uint_t pre) {
[96fb8ad]292  uint_t length = vec->length, i=0, j;
293  for (j=0;j<length;j++) {
[5c4ec3c]294    vec->data[i][j] -= fvec_moving_thres(vec, tmp, post, pre, j);
[96fb8ad]295  }
296}
297
[eb7f743]298smpl_t
299fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec,
300    uint_t post, uint_t pre, uint_t pos)
301{
302  smpl_t *medar = (smpl_t *) tmpvec->data[0];
[96fb8ad]303  uint_t k;
[eb7f743]304  uint_t win_length = post + pre + 1;
305  uint_t length = vec->length;
[96fb8ad]306  /* post part of the buffer does not exist */
[eb7f743]307  if (pos < post + 1) {
308    for (k = 0; k < post + 1 - pos; k++)
309      medar[k] = 0.;            /* 0-padding at the beginning */
310    for (k = post + 1 - pos; k < win_length; k++)
311      medar[k] = vec->data[0][k + pos - post];
312    /* the buffer is fully defined */
313  } else if (pos + pre < length) {
314    for (k = 0; k < win_length; k++)
315      medar[k] = vec->data[0][k + pos - post];
316    /* pre part of the buffer does not exist */
[96fb8ad]317  } else {
[eb7f743]318    for (k = 0; k < length - pos + post; k++)
319      medar[k] = vec->data[0][k + pos - post];
320    for (k = length - pos + post; k < win_length; k++)
321      medar[k] = 0.;            /* 0-padding at the end */
[ade9afe]322  }
[eb7f743]323  return fvec_median (tmpvec);
[96fb8ad]324}
325
[5c4ec3c]326smpl_t fvec_median(fvec_t * input) {
[96fb8ad]327  uint_t n = input->length;
328  smpl_t * arr = (smpl_t *) input->data[0];
329  uint_t low, high ;
330  uint_t median;
331  uint_t middle, ll, hh;
332
333  low = 0 ; high = n-1 ; median = (low + high) / 2;
334  for (;;) {
335    if (high <= low) /* One element only */
336      return arr[median] ;
337
338    if (high == low + 1) {  /* Two elements only */
339      if (arr[low] > arr[high])
340        ELEM_SWAP(arr[low], arr[high]) ;
341      return arr[median] ;
342    }
343
344    /* Find median of low, middle and high items; swap into position low */
345    middle = (low + high) / 2;
346    if (arr[middle] > arr[high])    ELEM_SWAP(arr[middle], arr[high]);
347    if (arr[low]    > arr[high])    ELEM_SWAP(arr[low],    arr[high]);
348    if (arr[middle] > arr[low])     ELEM_SWAP(arr[middle], arr[low]) ;
349
350    /* Swap low item (now in position middle) into position (low+1) */
351    ELEM_SWAP(arr[middle], arr[low+1]) ;
352
353    /* Nibble from each end towards middle, swapping items when stuck */
354    ll = low + 1;
355    hh = high;
356    for (;;) {
357      do ll++; while (arr[low] > arr[ll]) ;
358      do hh--; while (arr[hh]  > arr[low]) ;
359
360      if (hh < ll)
361        break;
362
363      ELEM_SWAP(arr[ll], arr[hh]) ;
364    }
365
366    /* Swap middle item (in position low) back into correct position */
367    ELEM_SWAP(arr[low], arr[hh]) ;
368
369    /* Re-set active partition */
370    if (hh <= median)
371      low = ll;
372    if (hh >= median)
373      high = hh - 1;
374  }
375}
376
[5c4ec3c]377smpl_t fvec_quadint(fvec_t * x,uint_t pos, uint_t span) {
[c5c0c98]378  smpl_t s0, s1, s2;
379  uint_t x0 = (pos < span) ? pos : pos - span;
380  uint_t x2 = (pos + span < x->length) ? pos + span : pos;
381  if (x0 == pos) return (x->data[0][pos] <= x->data[0][x2]) ? pos : x2;
382  if (x2 == pos) return (x->data[0][pos] <= x->data[0][x0]) ? pos : x0;
383  s0 = x->data[0][x0];
[eb7f743]384  s1 = x->data[0][pos];
[c5c0c98]385  s2 = x->data[0][x2];
386  return pos + 0.5 * (s2 - s0 ) / (s2 - 2.* s1 + s0);
[9771488]387}
388
[5c4ec3c]389uint_t fvec_peakpick(fvec_t * onset, uint_t pos) {
[ade9afe]390  uint_t i=0, tmp=0;
391  /*for (i=0;i<onset->channels;i++)*/
392  tmp = (onset->data[i][pos] > onset->data[i][pos-1]
393      &&  onset->data[i][pos] > onset->data[i][pos+1]
394      &&  onset->data[i][pos] > 0.);
395  return tmp;
[96fb8ad]396}
397
[eb7f743]398smpl_t
399aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf)
400{
401  smpl_t tmp =
402      s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2);
403  return tmp;
404}
405
406smpl_t
407aubio_freqtomidi (smpl_t freq)
408{
[ade9afe]409  /* log(freq/A-2)/log(2) */
[eb7f743]410  smpl_t midi = freq / 6.875;
411  midi = LOG (midi) / 0.69314718055995;
[ade9afe]412  midi *= 12;
413  midi -= 3;
414  return midi;
[79c2e52]415}
416
[eb7f743]417smpl_t
418aubio_miditofreq (smpl_t midi)
419{
420  smpl_t freq = (midi + 3.) / 12.;
421  freq = EXP (freq * 0.69314718055995);
[ade9afe]422  freq *= 6.875;
423  return freq;
[96fb8ad]424}
425
[eb7f743]426smpl_t
427aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
428{
429  smpl_t freq = samplerate / fftsize;
430  return freq * bin;
[96fb8ad]431}
432
[eb7f743]433smpl_t
434aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
435{
436  smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize);
437  return aubio_freqtomidi (midi);
[96fb8ad]438}
439
[eb7f743]440smpl_t
441aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize)
442{
443  smpl_t bin = fftsize / samplerate;
444  return freq * bin;
[79c2e52]445}
446
[eb7f743]447smpl_t
448aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize)
449{
450  smpl_t freq = aubio_miditofreq (midi);
451  return aubio_freqtobin (freq, samplerate, fftsize);
[79c2e52]452}
453
[10a5413]454uint_t
[41f4c5b]455aubio_is_power_of_two (uint_t a)
456{
457  if ((a & (a - 1)) == 0) {
[10a5413]458    return 1;
459  } else {
[41f4c5b]460    return 0;
[10a5413]461  }
462}
463
464uint_t
[41f4c5b]465aubio_next_power_of_two (uint_t a)
466{
[10a5413]467  uint_t i;
468  a--;
[41f4c5b]469  for (i = 0; i < sizeof (uint_t) * CHAR_BIT; i++) {
[10a5413]470    a = a | a >> 1;
471  }
[41f4c5b]472  return a + 1;
[10a5413]473}
474
[eb7f743]475smpl_t
476aubio_db_spl (fvec_t * o)
477{
478  smpl_t val = SQRT (fvec_local_energy (o));
479  val /= (smpl_t) o->length;
480  return LIN2DB (val);
[96fb8ad]481}
482
[eb7f743]483uint_t
484aubio_silence_detection (fvec_t * o, smpl_t threshold)
485{
486  return (aubio_db_spl (o) < threshold);
487}
[96fb8ad]488
[eb7f743]489smpl_t
490aubio_level_detection (fvec_t * o, smpl_t threshold)
491{
492  smpl_t db_spl = aubio_db_spl (o);
493  if (db_spl < threshold) {
[ade9afe]494    return 1.;
[eb7f743]495  } else {
496    return db_spl;
497  }
[96fb8ad]498}
[a0fd4e4]499
[eb7f743]500smpl_t
501aubio_zero_crossing_rate (fvec_t * input)
502{
503  uint_t i = 0, j;
[fff2bee]504  uint_t zcr = 0;
[eb7f743]505  for (j = 1; j < input->length; j++) {
[7e204d01]506    // previous was strictly negative
[eb7f743]507    if (input->data[i][j - 1] < 0.) {
[7e204d01]508      // current is positive or null
[eb7f743]509      if (input->data[i][j] >= 0.) {
[7e204d01]510        zcr += 1;
511      }
[eb7f743]512      // previous was positive or null
[7e204d01]513    } else {
514      // current is strictly negative
[eb7f743]515      if (input->data[i][j] < 0.) {
[fff2bee]516        zcr += 1;
517      }
518    }
519  }
[eb7f743]520  return zcr / (smpl_t) input->length;
[fff2bee]521}
522
[eb7f743]523void
524aubio_autocorr (fvec_t * input, fvec_t * output)
525{
526  uint_t i, j, k, length = input->length;
527  smpl_t *data, *acf;
528  smpl_t tmp = 0;
529  for (k = 0; k < input->channels; k++) {
530    data = input->data[k];
531    acf = output->data[k];
532    for (i = 0; i < length; i++) {
533      tmp = 0.;
534      for (j = i; j < length; j++) {
535        tmp += data[j - i] * data[j];
536      }
537      acf[i] = tmp / (smpl_t) (length - i);
[ade9afe]538    }
539  }
[a0fd4e4]540}
541
[eb7f743]542void
543aubio_cleanup (void)
544{
[b511fa9]545#if HAVE_FFTW3
[eb7f743]546  fftw_cleanup ();
[714380d]547#else
[b511fa9]548#if HAVE_FFTW3F
[eb7f743]549  fftwf_cleanup ();
[714380d]550#endif
551#endif
552}
Note: See TracBrowser for help on using the repository browser.