2 Copyright (C) 2003 Paul Brossier
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.
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.
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.
20 /* see in mathutils.h for doc */
22 #include "aubio_priv.h"
24 #include "mathutils.h"
27 fvec_t * new_aubio_window(uint_t size, aubio_window_type wintype) {
28 // create fvec of size x 1 channel
29 fvec_t * win = new_fvec( size, 1);
30 smpl_t * w = win->data[0];
33 case aubio_win_rectangle:
37 case aubio_win_hamming:
39 w[i] = 0.54 - 0.46 * COS(TWO_PI * i / (size));
41 case aubio_win_hanning:
43 w[i] = 0.5 - (0.5 * COS(TWO_PI * i / (size)));
45 case aubio_win_hanningz:
47 w[i] = 0.5 * (1.0 - COS(TWO_PI * i / (size)));
49 case aubio_win_blackman:
52 - 0.50 * COS( TWO_PI*i/(size-1.0))
53 + 0.08 * COS(2.0*TWO_PI*i/(size-1.0));
55 case aubio_win_blackman_harris:
58 - 0.48829 * COS( TWO_PI*i/(size-1.0))
59 + 0.14128 * COS(2.0*TWO_PI*i/(size-1.0))
60 - 0.01168 * COS(3.0*TWO_PI*i/(size-1.0));
62 case aubio_win_gaussian:
64 w[i] = EXP(- 1.0 / SQR(size) * SQR(2.0*i-size));
68 w[i] = 1.0 - SQR((2*i-size)/(size+1.0));
70 case aubio_win_parzen:
72 w[i] = 1.0 - ABS((2*i-size)/(size+1.0));
80 smpl_t aubio_unwrap2pi(smpl_t phase) {
81 /* mod(phase+pi,-2pi)+pi */
82 return phase + TWO_PI * (1. + FLOOR(-(phase+PI)/TWO_PI));
85 smpl_t vec_mean(fvec_t *s) {
88 for (i=0; i < s->channels; i++)
89 for (j=0; j < s->length; j++)
91 return tmp/(smpl_t)(s->length);
94 smpl_t vec_sum(fvec_t *s) {
97 for (i=0; i < s->channels; i++)
98 for (j=0; j < s->length; j++)
103 smpl_t vec_max(fvec_t *s) {
106 for (i=0; i < s->channels; i++)
107 for (j=0; j < s->length; j++)
108 tmp = (tmp > s->data[i][j])? tmp : s->data[i][j];
112 smpl_t vec_min(fvec_t *s) {
114 smpl_t tmp = s->data[0][0];
115 for (i=0; i < s->channels; i++)
116 for (j=0; j < s->length; j++)
117 tmp = (tmp < s->data[i][j])? tmp : s->data[i][j] ;
121 uint_t vec_min_elem(fvec_t *s) {
122 uint_t i,j=0, pos=0.;
123 smpl_t tmp = s->data[0][0];
124 for (i=0; i < s->channels; i++)
125 for (j=0; j < s->length; j++) {
126 pos = (tmp < s->data[i][j])? pos : j;
127 tmp = (tmp < s->data[i][j])? tmp : s->data[i][j] ;
132 uint_t vec_max_elem(fvec_t *s) {
133 uint_t i,j=0, pos=0.;
135 for (i=0; i < s->channels; i++)
136 for (j=0; j < s->length; j++) {
137 pos = (tmp > s->data[i][j])? pos : j;
138 tmp = (tmp > s->data[i][j])? tmp : s->data[i][j] ;
143 void vec_shift(fvec_t *s) {
146 for (i=0; i < s->channels; i++)
147 for (j=0; j < s->length / 2 ; j++) {
148 //tmp = s->data[i][j];
149 //s->data[i][j] = s->data[i][j+s->length/2];
150 //s->data[i][j+s->length/2] = tmp;
151 ELEM_SWAP(s->data[i][j],s->data[i][j+s->length/2]);
155 smpl_t vec_local_energy(fvec_t * f) {
158 for (i=0;i<f->channels;i++)
159 for (j=0;j<f->length;j++)
160 locE+=SQR(f->data[i][j]);
164 smpl_t vec_local_hfc(fvec_t * f) {
167 for (i=0;i<f->channels;i++)
168 for (j=0;j<f->length;j++)
169 locE+=(i+1)*f->data[i][j];
173 smpl_t vec_alpha_norm(fvec_t * DF, smpl_t alpha) {
176 for (i=0;i<DF->channels;i++)
177 for (j=0;j<DF->length;j++)
178 tmp += POW(ABS(DF->data[i][j]),alpha);
179 return POW(tmp/DF->length,1./alpha);
182 void vec_dc_removal(fvec_t * mag) {
184 uint_t length = mag->length, i=0, j;
186 for (j=0;j<length;j++) {
187 mag->data[i][j] -= mini;
191 void vec_alpha_normalise(fvec_t * mag, uint_t alpha) {
193 uint_t length = mag->length, i=0, j;
194 alphan = vec_alpha_norm(mag,alpha);
195 for (j=0;j<length;j++){
196 mag->data[i][j] /= alphan;
200 void vec_add(fvec_t * mag, smpl_t threshold) {
201 uint_t length = mag->length, i=0, j;
202 for (j=0;j<length;j++) {
203 mag->data[i][j] += threshold;
207 void vec_adapt_thres(fvec_t * vec, fvec_t * tmp,
208 uint_t post, uint_t pre) {
209 uint_t length = vec->length, i=0, j;
210 for (j=0;j<length;j++) {
211 vec->data[i][j] -= vec_moving_thres(vec, tmp, post, pre, j);
215 smpl_t vec_moving_thres(fvec_t * vec, fvec_t * tmpvec,
216 uint_t post, uint_t pre, uint_t pos) {
217 smpl_t * medar = (smpl_t *)tmpvec->data[0];
219 uint_t win_length = post+pre+1;
220 uint_t length = vec->length;
221 /* post part of the buffer does not exist */
223 for (k=0;k<post+1-pos;k++)
224 medar[k] = 0.; /* 0-padding at the beginning */
225 for (k=post+1-pos;k<win_length;k++)
226 medar[k] = vec->data[0][k+pos-post];
227 /* the buffer is fully defined */
228 } else if (pos+pre<length) {
229 for (k=0;k<win_length;k++)
230 medar[k] = vec->data[0][k+pos-post];
231 /* pre part of the buffer does not exist */
233 for (k=0;k<length-pos+post;k++)
234 medar[k] = vec->data[0][k+pos-post];
235 for (k=length-pos+post;k<win_length;k++)
236 medar[k] = 0.; /* 0-padding at the end */
238 return vec_median(tmpvec);
241 smpl_t vec_median(fvec_t * input) {
242 uint_t n = input->length;
243 smpl_t * arr = (smpl_t *) input->data[0];
246 uint_t middle, ll, hh;
248 low = 0 ; high = n-1 ; median = (low + high) / 2;
250 if (high <= low) /* One element only */
253 if (high == low + 1) { /* Two elements only */
254 if (arr[low] > arr[high])
255 ELEM_SWAP(arr[low], arr[high]) ;
259 /* Find median of low, middle and high items; swap into position low */
260 middle = (low + high) / 2;
261 if (arr[middle] > arr[high]) ELEM_SWAP(arr[middle], arr[high]);
262 if (arr[low] > arr[high]) ELEM_SWAP(arr[low], arr[high]);
263 if (arr[middle] > arr[low]) ELEM_SWAP(arr[middle], arr[low]) ;
265 /* Swap low item (now in position middle) into position (low+1) */
266 ELEM_SWAP(arr[middle], arr[low+1]) ;
268 /* Nibble from each end towards middle, swapping items when stuck */
272 do ll++; while (arr[low] > arr[ll]) ;
273 do hh--; while (arr[hh] > arr[low]) ;
278 ELEM_SWAP(arr[ll], arr[hh]) ;
281 /* Swap middle item (in position low) back into correct position */
282 ELEM_SWAP(arr[low], arr[hh]) ;
284 /* Re-set active partition */
292 smpl_t vec_quadint(fvec_t * x,uint_t pos, uint_t span) {
294 uint_t x0 = (pos < span) ? pos : pos - span;
295 uint_t x2 = (pos + span < x->length) ? pos + span : pos;
296 if (x0 == pos) return (x->data[0][pos] <= x->data[0][x2]) ? pos : x2;
297 if (x2 == pos) return (x->data[0][pos] <= x->data[0][x0]) ? pos : x0;
299 s1 = x->data[0][pos] ;
301 return pos + 0.5 * (s2 - s0 ) / (s2 - 2.* s1 + s0);
304 smpl_t aubio_quadfrac(smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf) {
305 smpl_t tmp = s0 + (pf/2.) * (pf * ( s0 - 2.*s1 + s2 ) - 3.*s0 + 4.*s1 - s2);
309 uint_t vec_peakpick(fvec_t * onset, uint_t pos) {
311 /*for (i=0;i<onset->channels;i++)*/
312 tmp = (onset->data[i][pos] > onset->data[i][pos-1]
313 && onset->data[i][pos] > onset->data[i][pos+1]
314 && onset->data[i][pos] > 0.);
318 smpl_t aubio_freqtomidi(smpl_t freq) {
319 /* log(freq/A-2)/log(2) */
320 smpl_t midi = freq/6.875;
321 midi = LOG(midi)/0.69314718055995;
327 smpl_t aubio_miditofreq(smpl_t midi) {
328 smpl_t freq = (midi+3.)/12.;
329 freq = EXP(freq*0.69314718055995);
334 smpl_t aubio_bintofreq(smpl_t bin, smpl_t samplerate, smpl_t fftsize) {
335 smpl_t freq = samplerate/fftsize;
339 smpl_t aubio_bintomidi(smpl_t bin, smpl_t samplerate, smpl_t fftsize) {
340 smpl_t midi = aubio_bintofreq(bin,samplerate,fftsize);
341 return aubio_freqtomidi(midi);
344 smpl_t aubio_freqtobin(smpl_t freq, smpl_t samplerate, smpl_t fftsize) {
345 smpl_t bin = fftsize/samplerate;
349 smpl_t aubio_miditobin(smpl_t midi, smpl_t samplerate, smpl_t fftsize) {
350 smpl_t freq = aubio_miditofreq(midi);
351 return aubio_freqtobin(freq,samplerate,fftsize);
354 /** returns 1 if wassilence is 0 and RMS(ibuf)<threshold
357 uint_t aubio_silence_detection(fvec_t * ibuf, smpl_t threshold) {
360 for (j=0;j<ibuf->length;j++) {
361 loudness += SQR(ibuf->data[i][j]);
363 loudness = SQRT(loudness);
364 loudness /= (smpl_t)ibuf->length;
365 loudness = LIN2DB(loudness);
367 return (loudness < threshold);
370 /** returns level log(RMS(ibuf)) if < threshold, 1 otherwise
373 smpl_t aubio_level_detection(fvec_t * ibuf, smpl_t threshold) {
376 for (j=0;j<ibuf->length;j++) {
377 loudness += SQR(ibuf->data[i][j]);
379 loudness = SQRT(loudness);
380 loudness /= (smpl_t)ibuf->length;
381 loudness = LIN2DB(loudness);
383 if (loudness < threshold)
389 smpl_t aubio_zero_crossing_rate(fvec_t * input) {
392 for ( j = 1; j < input->length; j++ ) {
393 // previous was strictly negative
394 if( input->data[i][j-1] < 0. ) {
395 // current is positive or null
396 if ( input->data[i][j] >= 0. ) {
399 // previous was positive or null
401 // current is strictly negative
402 if ( input->data[i][j] < 0. ) {
407 return zcr/(smpl_t)input->length;
410 void aubio_autocorr(fvec_t * input, fvec_t * output) {
411 uint_t i = 0, j = 0, length = input->length;
412 smpl_t * data = input->data[0];
413 smpl_t * acf = output->data[0];
415 for(i=0;i<length;i++){
416 for(j=i;j<length;j++){
417 tmp += data[j-i]*data[j];
419 acf[i] = tmp /(smpl_t)(length-i);
424 void aubio_cleanup(void) {