source: src/mathutils.c @ ee8a57c

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

src/mathutils.h: add fvec_push

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