source: src/mathutils.c @ 714380d

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

add aubio_cleanup to clean fftw cached memory
add aubio_cleanup to clean fftw cached memory

  • Property mode set to 100644
File size: 11.6 KB
Line 
1/*
2   Copyright (C) 2003 Paul Brossier
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18*/
19
20/* see in mathutils.h for doc */
21
22#include "aubio_priv.h"
23#include "sample.h"
24#include "mathutils.h"
25#include "config.h"
26
27void aubio_window(smpl_t *w, uint_t size, aubio_window_type wintype) {
28  uint_t i;
29  switch(wintype) {
30    case aubio_win_rectangle:
31      for (i=0;i<size;i++)
32        w[i] = 0.5; 
33      break;
34    case aubio_win_hamming:
35      for (i=0;i<size;i++)
36        w[i] = 0.54 - 0.46 * COS(TWO_PI * i / (size));
37      break;
38    case aubio_win_hanning:
39      for (i=0;i<size;i++)
40        w[i] = 0.5 - (0.5 * COS(TWO_PI * i / (size)));
41      break;
42    case aubio_win_hanningz:
43      for (i=0;i<size;i++)
44        w[i] = 0.5 * (1.0 - COS(TWO_PI * i / (size)));
45      break;
46    case aubio_win_blackman:
47      for (i=0;i<size;i++)
48        w[i] = 0.42
49          - 0.50 * COS(    TWO_PI*i/(size-1.0))
50          +     0.08 * COS(2.0*TWO_PI*i/(size-1.0));
51      break;
52    case aubio_win_blackman_harris:
53      for (i=0;i<size;i++)
54        w[i] = 0.35875 
55          - 0.48829 * COS(    TWO_PI*i/(size-1.0))
56          + 0.14128 * COS(2.0*TWO_PI*i/(size-1.0))
57          - 0.01168 * COS(3.0*TWO_PI*i/(size-1.0));
58      break;
59    case aubio_win_gaussian:
60      for (i=0;i<size;i++)
61        w[i] = EXP(- 1.0 / SQR(size) * SQR(2.0*i-size));
62      break;
63    case aubio_win_welch:
64      for (i=0;i<size;i++)
65        w[i] = 1.0 - SQR((2*i-size)/(size+1.0));
66      break;
67    case aubio_win_parzen:
68      for (i=0;i<size;i++)
69        w[i] = 1.0 - ABS((2*i-size)/(size+1.0));
70      break;
71    default:
72      break;
73  }
74}
75
76
77smpl_t aubio_unwrap2pi(smpl_t phase) {
78  /* mod(phase+pi,-2pi)+pi */
79  return phase + TWO_PI * (1. + FLOOR(-(phase+PI)/TWO_PI));
80}
81
82
83smpl_t vec_mean(fvec_t *s) 
84{
85  uint_t i,j;
86  smpl_t tmp = 0.0f;
87  for (i=0; i < s->channels; i++)
88    for (j=0; j < s->length; j++)
89      tmp += s->data[i][j];
90  return tmp/(smpl_t)(s->length);
91}
92
93
94smpl_t vec_sum(fvec_t *s) 
95{
96  uint_t i,j;
97  smpl_t tmp = 0.0f;
98  for (i=0; i < s->channels; i++)
99    for (j=0; j < s->length; j++)
100      tmp += s->data[i][j];
101  return tmp;
102}
103
104
105smpl_t vec_max(fvec_t *s) 
106{
107  uint_t i,j;
108  smpl_t tmp = 0.0f;
109  for (i=0; i < s->channels; i++)
110    for (j=0; j < s->length; j++)
111      tmp = (tmp > s->data[i][j])? tmp : s->data[i][j];
112  return tmp;
113}
114
115smpl_t vec_min(fvec_t *s) 
116{
117  uint_t i,j;
118  smpl_t tmp = s->data[0][0];
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  return tmp;
123}
124
125
126uint_t vec_min_elem(fvec_t *s) 
127{
128  uint_t i,j=0, pos=0.;
129  smpl_t tmp = s->data[0][0];
130  for (i=0; i < s->channels; i++)
131    for (j=0; j < s->length; j++) {
132      pos = (tmp < s->data[i][j])? pos : j;
133      tmp = (tmp < s->data[i][j])? tmp : s->data[i][j]  ;
134    }
135  return pos;
136}
137
138uint_t vec_max_elem(fvec_t *s) 
139{
140  uint_t i,j=0, pos=0.;
141  smpl_t tmp = 0.0f;
142  for (i=0; i < s->channels; i++)
143    for (j=0; j < s->length; j++) {
144      pos = (tmp > s->data[i][j])? pos : j;
145      tmp = (tmp > s->data[i][j])? tmp : s->data[i][j]  ;
146    }
147  return pos;
148}
149
150void vec_shift(fvec_t *s)
151{
152  uint_t i,j;
153  //smpl_t tmp = 0.0f;
154  for (i=0; i < s->channels; i++)
155    for (j=0; j < s->length / 2 ; j++) {
156      //tmp = s->data[i][j];
157      //s->data[i][j] = s->data[i][j+s->length/2];
158      //s->data[i][j+s->length/2] = tmp;
159      ELEM_SWAP(s->data[i][j],s->data[i][j+s->length/2]);
160    }
161}
162
163smpl_t vec_local_energy(fvec_t * f)
164{
165  smpl_t locE = 0.;
166  uint_t i,j;
167  for (i=0;i<f->channels;i++)
168    for (j=0;j<f->length;j++)
169      locE+=SQR(f->data[i][j]);
170  return locE;
171}
172
173smpl_t vec_local_hfc(fvec_t * f)
174{
175  smpl_t locE = 0.;
176  uint_t i,j;
177  for (i=0;i<f->channels;i++)
178    for (j=0;j<f->length;j++)
179      locE+=(i+1)*f->data[i][j];
180  return locE;
181}
182
183smpl_t vec_alpha_norm(fvec_t * DF, smpl_t alpha)
184{
185  smpl_t tmp = 0.;
186  uint_t i,j;
187  for (i=0;i<DF->channels;i++)
188    for (j=0;j<DF->length;j++)
189      tmp += POW(ABS(DF->data[i][j]),alpha);
190  return POW(tmp/DF->length,1./alpha);
191}
192
193
194void vec_dc_removal(fvec_t * mag)
195{
196    smpl_t mini = 0.;
197    uint_t length = mag->length, i=0, j;
198    mini = vec_min(mag);
199    for (j=0;j<length;j++) {
200      mag->data[i][j] -= mini;
201    }
202}
203
204
205void vec_alpha_normalise(fvec_t * mag, uint_t alpha)
206{
207  smpl_t alphan = 1.;
208  uint_t length = mag->length, i=0, j;
209  alphan = vec_alpha_norm(mag,alpha);
210  for (j=0;j<length;j++){
211    mag->data[i][j] /= alphan;
212  }
213}
214
215
216void vec_add(fvec_t * mag, smpl_t threshold) {
217  uint_t length = mag->length, i=0, j;
218  for (j=0;j<length;j++) {
219    mag->data[i][j] += threshold;
220  }
221}
222
223
224void vec_adapt_thres(fvec_t * vec, fvec_t * tmp, 
225    uint_t post, uint_t pre) 
226{
227  uint_t length = vec->length, i=0, j;
228  for (j=0;j<length;j++) {
229    vec->data[i][j] -= vec_moving_thres(vec, tmp, post, pre, j);
230  }
231}
232
233smpl_t vec_moving_thres(fvec_t * vec, fvec_t * tmpvec,
234    uint_t post, uint_t pre, uint_t pos) 
235{
236  smpl_t * medar = (smpl_t *)tmpvec->data[0];
237  uint_t k;
238  uint_t win_length =  post+pre+1;
239  uint_t length =  vec->length;
240  /* post part of the buffer does not exist */
241  if (pos<post+1) {
242    for (k=0;k<post+1-pos;k++) 
243      medar[k] = 0.; /* 0-padding at the beginning */
244    for (k=post+1-pos;k<win_length;k++)
245      medar[k] = vec->data[0][k+pos-post];
246  /* the buffer is fully defined */
247  } else if (pos+pre<length) {
248    for (k=0;k<win_length;k++)
249      medar[k] = vec->data[0][k+pos-post];
250  /* pre part of the buffer does not exist */
251  } else {
252    for (k=0;k<length-pos+post+1;k++)
253      medar[k] = vec->data[0][k+pos-post];
254    for (k=length-pos+post+1;k<win_length;k++) 
255      medar[k] = 0.; /* 0-padding at the end */
256  } 
257  return vec_median(tmpvec);
258}
259
260smpl_t vec_median(fvec_t * input) {
261  uint_t n = input->length;
262  smpl_t * arr = (smpl_t *) input->data[0];
263  uint_t low, high ;
264  uint_t median;
265  uint_t middle, ll, hh;
266
267  low = 0 ; high = n-1 ; median = (low + high) / 2;
268  for (;;) {
269    if (high <= low) /* One element only */
270      return arr[median] ;
271
272    if (high == low + 1) {  /* Two elements only */
273      if (arr[low] > arr[high])
274        ELEM_SWAP(arr[low], arr[high]) ;
275      return arr[median] ;
276    }
277
278    /* Find median of low, middle and high items; swap into position low */
279    middle = (low + high) / 2;
280    if (arr[middle] > arr[high])    ELEM_SWAP(arr[middle], arr[high]);
281    if (arr[low]    > arr[high])    ELEM_SWAP(arr[low],    arr[high]);
282    if (arr[middle] > arr[low])     ELEM_SWAP(arr[middle], arr[low]) ;
283
284    /* Swap low item (now in position middle) into position (low+1) */
285    ELEM_SWAP(arr[middle], arr[low+1]) ;
286
287    /* Nibble from each end towards middle, swapping items when stuck */
288    ll = low + 1;
289    hh = high;
290    for (;;) {
291      do ll++; while (arr[low] > arr[ll]) ;
292      do hh--; while (arr[hh]  > arr[low]) ;
293
294      if (hh < ll)
295        break;
296
297      ELEM_SWAP(arr[ll], arr[hh]) ;
298    }
299
300    /* Swap middle item (in position low) back into correct position */
301    ELEM_SWAP(arr[low], arr[hh]) ;
302
303    /* Re-set active partition */
304    if (hh <= median)
305      low = ll;
306    if (hh >= median)
307      high = hh - 1;
308  }
309}
310
311smpl_t vec_quadint(fvec_t * x,uint_t pos) {
312  uint_t span = 2;
313  smpl_t step = 1./200.;
314  /* hack : init resold to - something (in case x[pos+-span]<0)) */
315  smpl_t res, frac, s0, s1, s2, exactpos = (smpl_t)pos, resold = -1000.;
316  if ((pos > span) && (pos < x->length-span)) {
317    s0 = x->data[0][pos-span];
318    s1 = x->data[0][pos]     ;
319    s2 = x->data[0][pos+span];
320    /* increase frac */
321    for (frac = 0.; frac < 2.; frac = frac + step) {
322      res = aubio_quadfrac(s0, s1, s2, frac);
323      if (res > resold) 
324        resold = res;
325      else {                           
326        exactpos += (frac-step)*2. - 1.;
327        break;
328      }
329    }
330  }
331  return exactpos;
332}
333
334smpl_t vec_quadint_min(fvec_t * x,uint_t pos, uint_t span) {
335  smpl_t step = 1./200.;
336  /* init resold to - something (in case x[pos+-span]<0)) */
337  smpl_t res, frac, s0, s1, s2, exactpos = (smpl_t)pos, resold = 100000.;
338  if ((pos > span) && (pos < x->length-span)) {
339    s0 = x->data[0][pos-span];
340    s1 = x->data[0][pos]     ;
341    s2 = x->data[0][pos+span];
342    /* increase frac */
343    for (frac = 0.; frac < 2.; frac = frac + step) {
344      res = aubio_quadfrac(s0, s1, s2, frac);
345      if (res < resold) {
346        resold = res;
347      } else {                         
348        exactpos += (frac-step)*span - span/2.;
349        break;
350      }
351    }
352  }
353  return exactpos;
354}
355
356smpl_t aubio_quadfrac(smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf) {
357  smpl_t tmp = s0 + (pf/2.) * (pf * ( s0 - 2.*s1 + s2 ) - 3.*s0 + 4.*s1 - s2);
358  return tmp;
359}
360
361uint_t vec_peakpick(fvec_t * onset, uint_t pos) {
362        uint_t i=0, tmp=0;
363        /*for (i=0;i<onset->channels;i++)*/
364                tmp = (onset->data[i][pos] > onset->data[i][pos-1]
365                        &&  onset->data[i][pos] > onset->data[i][pos+1]
366                        &&      onset->data[i][pos] > 0.);
367        return tmp;
368}
369
370smpl_t aubio_freqtomidi(smpl_t freq) {
371        /* log(freq/A-2)/log(2) */
372        smpl_t midi = freq/6.875;
373        midi = LOG(midi)/0.69314718055995;
374        midi *= 12;
375        midi -= 3; 
376        return midi;
377}
378
379smpl_t aubio_miditofreq(smpl_t midi) {
380        smpl_t freq = (midi+3.)/12.;
381        freq = EXP(freq*0.69314718055995);
382        freq *= 6.875;
383        return freq;
384}
385
386smpl_t aubio_bintofreq(smpl_t bin, smpl_t samplerate, smpl_t fftsize) {
387  smpl_t freq = samplerate/fftsize;
388  return freq*bin;
389}
390
391smpl_t aubio_bintomidi(smpl_t bin, smpl_t samplerate, smpl_t fftsize) {
392  smpl_t midi = aubio_bintofreq(bin,samplerate,fftsize);
393  return aubio_freqtomidi(midi);
394}
395
396smpl_t aubio_freqtobin(smpl_t freq, smpl_t samplerate, smpl_t fftsize) {
397  smpl_t bin = fftsize/samplerate;
398  return freq*bin;
399}
400
401smpl_t aubio_miditobin(smpl_t midi, smpl_t samplerate, smpl_t fftsize) {
402  smpl_t freq = aubio_miditofreq(midi);
403  return aubio_freqtobin(freq,samplerate,fftsize);
404}
405
406
407
408/** returns 1 if wassilence is 0 and RMS(ibuf)<threshold
409 * \bug mono
410 */
411uint_t aubio_silence_detection(fvec_t * ibuf, smpl_t threshold) {
412  smpl_t loudness = 0;
413  uint_t i=0,j;
414  for (j=0;j<ibuf->length;j++) {
415    loudness += SQR(ibuf->data[i][j]);
416  }
417  loudness = SQRT(loudness);
418  loudness /= (smpl_t)ibuf->length;
419  loudness = LIN2DB(loudness);
420
421  return (loudness < threshold);
422}
423
424/** returns level log(RMS(ibuf)) if < threshold, 1 otherwise
425 * \bug mono
426 */
427smpl_t aubio_level_detection(fvec_t * ibuf, smpl_t threshold) {
428  smpl_t loudness = 0;
429  uint_t i=0,j;
430  for (j=0;j<ibuf->length;j++) {
431    loudness += SQR(ibuf->data[i][j]);
432  }
433  loudness = SQRT(loudness);
434  loudness /= (smpl_t)ibuf->length;
435  loudness = LIN2DB(loudness);
436
437  if (loudness < threshold)
438      return 1.;
439  else
440      return loudness;
441}
442
443void aubio_autocorr(fvec_t * input, fvec_t * output){
444        uint_t i = 0, j = 0, length = input->length;
445        smpl_t * data = input->data[0];
446        smpl_t * acf = output->data[0];
447        smpl_t tmp =0.;
448        for(i=0;i<length;i++){
449                for(j=i;j<length;j++){
450                        tmp += data[j-i]*data[j]; 
451                }
452                acf[i] = tmp /(smpl_t)(length-i);
453                tmp = 0.0;
454        }
455}
456
457void aubio_cleanup(void)
458{
459#if FFTW3_SUPPORT
460        fftw_cleanup();
461#else
462#if FFTW3F_SUPPORT
463        fftwf_cleanup();
464#endif
465#endif
466}
Note: See TracBrowser for help on using the repository browser.