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>, 14 years ago

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

  • Property mode set to 100644
File size: 12.5 KB
Line 
1/*
2  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
3
4  This file is part of aubio.
5
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.
10
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/>.
18
19*/
20
21/* see in mathutils.h for doc */
22
23#include "aubio_priv.h"
24#include "fvec.h"
25#include "mathutils.h"
26#include "config.h"
27
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
44fvec_t *
45new_aubio_window (char_t * window_type, uint_t size)
46{
47  // create fvec of size x 1 channel
48  fvec_t * win = new_fvec( size, 1);
49  smpl_t * w = win->data[0];
50  uint_t i;
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  }
77  switch(wintype) {
78    case aubio_win_rectangle:
79      for (i=0;i<size;i++)
80        w[i] = 0.5;
81      break;
82    case aubio_win_hamming:
83      for (i=0;i<size;i++)
84        w[i] = 0.54 - 0.46 * COS(TWO_PI * i / (size));
85      break;
86    case aubio_win_hanning:
87      for (i=0;i<size;i++)
88        w[i] = 0.5 - (0.5 * COS(TWO_PI * i / (size)));
89      break;
90    case aubio_win_hanningz:
91      for (i=0;i<size;i++)
92        w[i] = 0.5 * (1.0 - COS(TWO_PI * i / (size)));
93      break;
94    case aubio_win_blackman:
95      for (i=0;i<size;i++)
96        w[i] = 0.42
97          - 0.50 * COS(    TWO_PI*i/(size-1.0))
98          + 0.08 * COS(2.0*TWO_PI*i/(size-1.0));
99      break;
100    case aubio_win_blackman_harris:
101      for (i=0;i<size;i++)
102        w[i] = 0.35875
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;
107    case aubio_win_gaussian:
108      for (i=0;i<size;i++)
109        w[i] = EXP(- 1.0 / SQR(size) * SQR(2.0*i-size));
110      break;
111    case aubio_win_welch:
112      for (i=0;i<size;i++)
113        w[i] = 1.0 - SQR((2*i-size)/(size+1.0));
114      break;
115    case aubio_win_parzen:
116      for (i=0;i<size;i++)
117        w[i] = 1.0 - ABS((2*i-size)/(size+1.0));
118      break;
119    default:
120      break;
121  }
122  return win;
123}
124
125smpl_t
126aubio_unwrap2pi (smpl_t phase)
127{
128  /* mod(phase+pi,-2pi)+pi */
129  return phase + TWO_PI * (1. + FLOOR (-(phase + PI) / TWO_PI));
130}
131
132smpl_t
133fvec_mean (fvec_t * s)
134{
135  uint_t i, j;
136  smpl_t tmp = 0.0;
137  for (i = 0; i < s->channels; i++)
138    for (j = 0; j < s->length; j++)
139      tmp += s->data[i][j];
140  return tmp / (smpl_t) (s->length);
141}
142
143smpl_t
144fvec_sum (fvec_t * s)
145{
146  uint_t i, j;
147  smpl_t tmp = 0.0;
148  for (i = 0; i < s->channels; i++) {
149    for (j = 0; j < s->length; j++) {
150      tmp += s->data[i][j];
151    }
152  }
153  return tmp;
154}
155
156smpl_t
157fvec_max (fvec_t * s)
158{
159  uint_t i, j;
160  smpl_t tmp = 0.0;
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  }
166  return tmp;
167}
168
169smpl_t
170fvec_min (fvec_t * s)
171{
172  uint_t i, j;
173  smpl_t tmp = s->data[0][0];
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  }
179  return tmp;
180}
181
182uint_t
183fvec_min_elem (fvec_t * s)
184{
185  uint_t i, j, pos = 0.;
186  smpl_t tmp = s->data[0][0];
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];
191    }
192  }
193  return pos;
194}
195
196uint_t
197fvec_max_elem (fvec_t * s)
198{
199  uint_t i, j, pos = 0;
200  smpl_t tmp = 0.0;
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];
205    }
206  }
207  return pos;
208}
209
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]);
217    }
218  }
219}
220
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;
232}
233
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;
245}
246
247void
248fvec_min_removal (fvec_t * v)
249{
250  smpl_t v_min = fvec_min (v);
251  fvec_add (v,  - v_min );
252}
253
254smpl_t
255fvec_alpha_norm (fvec_t * o, smpl_t alpha)
256{
257  uint_t i, j;
258  smpl_t tmp = 0.;
259  for (i = 0; i < o->channels; i++) {
260    for (j = 0; j < o->length; j++) {
261      tmp += POW (ABS (o->data[i][j]), alpha);
262    }
263  }
264  return POW (tmp / o->length, 1. / alpha);
265}
266
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    }
276  }
277}
278
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    }
287  }
288}
289
290void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
291    uint_t post, uint_t pre) {
292  uint_t length = vec->length, i=0, j;
293  for (j=0;j<length;j++) {
294    vec->data[i][j] -= fvec_moving_thres(vec, tmp, post, pre, j);
295  }
296}
297
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];
303  uint_t k;
304  uint_t win_length = post + pre + 1;
305  uint_t length = vec->length;
306  /* post part of the buffer does not exist */
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 */
317  } else {
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 */
322  }
323  return fvec_median (tmpvec);
324}
325
326smpl_t fvec_median(fvec_t * input) {
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
377smpl_t fvec_quadint(fvec_t * x,uint_t pos, uint_t span) {
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];
384  s1 = x->data[0][pos];
385  s2 = x->data[0][x2];
386  return pos + 0.5 * (s2 - s0 ) / (s2 - 2.* s1 + s0);
387}
388
389uint_t fvec_peakpick(fvec_t * onset, uint_t pos) {
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;
396}
397
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{
409  /* log(freq/A-2)/log(2) */
410  smpl_t midi = freq / 6.875;
411  midi = LOG (midi) / 0.69314718055995;
412  midi *= 12;
413  midi -= 3;
414  return midi;
415}
416
417smpl_t
418aubio_miditofreq (smpl_t midi)
419{
420  smpl_t freq = (midi + 3.) / 12.;
421  freq = EXP (freq * 0.69314718055995);
422  freq *= 6.875;
423  return freq;
424}
425
426smpl_t
427aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
428{
429  smpl_t freq = samplerate / fftsize;
430  return freq * bin;
431}
432
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);
438}
439
440smpl_t
441aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize)
442{
443  smpl_t bin = fftsize / samplerate;
444  return freq * bin;
445}
446
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);
452}
453
454uint_t
455aubio_is_power_of_two (uint_t a)
456{
457  if ((a & (a - 1)) == 0) {
458    return 1;
459  } else {
460    return 0;
461  }
462}
463
464uint_t
465aubio_next_power_of_two (uint_t a)
466{
467  uint_t i;
468  a--;
469  for (i = 0; i < sizeof (uint_t) * CHAR_BIT; i++) {
470    a = a | a >> 1;
471  }
472  return a + 1;
473}
474
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);
481}
482
483uint_t
484aubio_silence_detection (fvec_t * o, smpl_t threshold)
485{
486  return (aubio_db_spl (o) < threshold);
487}
488
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) {
494    return 1.;
495  } else {
496    return db_spl;
497  }
498}
499
500smpl_t
501aubio_zero_crossing_rate (fvec_t * input)
502{
503  uint_t i = 0, j;
504  uint_t zcr = 0;
505  for (j = 1; j < input->length; j++) {
506    // previous was strictly negative
507    if (input->data[i][j - 1] < 0.) {
508      // current is positive or null
509      if (input->data[i][j] >= 0.) {
510        zcr += 1;
511      }
512      // previous was positive or null
513    } else {
514      // current is strictly negative
515      if (input->data[i][j] < 0.) {
516        zcr += 1;
517      }
518    }
519  }
520  return zcr / (smpl_t) input->length;
521}
522
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);
538    }
539  }
540}
541
542void
543aubio_cleanup (void)
544{
545#if HAVE_FFTW3
546  fftw_cleanup ();
547#else
548#if HAVE_FFTW3F
549  fftwf_cleanup ();
550#endif
551#endif
552}
Note: See TracBrowser for help on using the repository browser.