2 Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
4 This file is part of aubio.
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.
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.
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/>.
21 /* see in mathutils.h for doc */
23 #include "aubio_priv.h"
25 #include "mathutils.h"
29 new_aubio_window (uint_t size, aubio_window_type wintype)
31 // create fvec of size x 1 channel
32 fvec_t * win = new_fvec( size, 1);
33 smpl_t * w = win->data[0];
36 case aubio_win_rectangle:
40 case aubio_win_hamming:
42 w[i] = 0.54 - 0.46 * COS(TWO_PI * i / (size));
44 case aubio_win_hanning:
46 w[i] = 0.5 - (0.5 * COS(TWO_PI * i / (size)));
48 case aubio_win_hanningz:
50 w[i] = 0.5 * (1.0 - COS(TWO_PI * i / (size)));
52 case aubio_win_blackman:
55 - 0.50 * COS( TWO_PI*i/(size-1.0))
56 + 0.08 * COS(2.0*TWO_PI*i/(size-1.0));
58 case aubio_win_blackman_harris:
61 - 0.48829 * COS( TWO_PI*i/(size-1.0))
62 + 0.14128 * COS(2.0*TWO_PI*i/(size-1.0))
63 - 0.01168 * COS(3.0*TWO_PI*i/(size-1.0));
65 case aubio_win_gaussian:
67 w[i] = EXP(- 1.0 / SQR(size) * SQR(2.0*i-size));
71 w[i] = 1.0 - SQR((2*i-size)/(size+1.0));
73 case aubio_win_parzen:
75 w[i] = 1.0 - ABS((2*i-size)/(size+1.0));
84 aubio_unwrap2pi (smpl_t phase)
86 /* mod(phase+pi,-2pi)+pi */
87 return phase + TWO_PI * (1. + FLOOR (-(phase + PI) / TWO_PI));
91 fvec_mean (fvec_t * s)
95 for (i = 0; i < s->channels; i++)
96 for (j = 0; j < s->length; j++)
98 return tmp / (smpl_t) (s->length);
102 fvec_sum (fvec_t * s)
106 for (i = 0; i < s->channels; i++) {
107 for (j = 0; j < s->length; j++) {
108 tmp += s->data[i][j];
115 fvec_max (fvec_t * s)
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];
128 fvec_min (fvec_t * s)
131 smpl_t tmp = s->data[0][0];
132 for (i = 0; i < s->channels; i++) {
133 for (j = 0; j < s->length; j++) {
134 tmp = (tmp < s->data[i][j]) ? tmp : s->data[i][j];
141 fvec_min_elem (fvec_t * s)
143 uint_t i, j = 0, pos = 0.;
144 smpl_t tmp = s->data[0][0];
145 for (i = 0; i < s->channels; i++) {
146 for (j = 0; j < s->length; j++) {
147 pos = (tmp < s->data[i][j]) ? pos : j;
148 tmp = (tmp < s->data[i][j]) ? tmp : s->data[i][j];
155 fvec_max_elem (fvec_t * s)
159 for (i = 0; i < s->channels; i++) {
160 for (j = 0; j < s->length; j++) {
161 pos = (tmp > s->data[i][j]) ? pos : j;
162 tmp = (tmp > s->data[i][j]) ? tmp : s->data[i][j];
169 fvec_shift (fvec_t * s)
172 for (i = 0; i < s->channels; i++) {
173 for (j = 0; j < s->length / 2; j++) {
174 ELEM_SWAP (s->data[i][j], s->data[i][j + s->length / 2]);
180 fvec_local_energy (fvec_t * f)
184 for (i = 0; i < f->channels; i++) {
185 for (j = 0; j < f->length; j++) {
186 energy += SQR (f->data[i][j]);
193 fvec_local_hfc (fvec_t * v)
197 for (i = 0; i < v->channels; i++) {
198 for (j = 0; j < v->length; j++) {
199 hfc += (i + 1) * v->data[i][j];
206 fvec_min_removal (fvec_t * v)
208 smpl_t v_min = fvec_min (v);
209 fvec_add (v, - v_min );
213 fvec_alpha_norm (fvec_t * o, smpl_t alpha)
217 for (i = 0; i < o->channels; i++) {
218 for (j = 0; j < o->length; j++) {
219 tmp += POW (ABS (o->data[i][j]), alpha);
222 return POW (tmp / o->length, 1. / alpha);
226 fvec_alpha_normalise (fvec_t * o, smpl_t alpha)
229 smpl_t norm = fvec_alpha_norm (o, alpha);
230 for (i = 0; i < o->channels; i++) {
231 for (j = 0; j < o->length; j++) {
232 o->data[i][j] /= norm;
238 fvec_add (fvec_t * o, smpl_t val)
241 for (i = 0; i < o->channels; i++) {
242 for (j = 0; j < o->length; j++) {
243 o->data[i][j] += val;
248 void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
249 uint_t post, uint_t pre) {
250 uint_t length = vec->length, i=0, j;
251 for (j=0;j<length;j++) {
252 vec->data[i][j] -= fvec_moving_thres(vec, tmp, post, pre, j);
257 fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec,
258 uint_t post, uint_t pre, uint_t pos)
260 smpl_t *medar = (smpl_t *) tmpvec->data[0];
262 uint_t win_length = post + pre + 1;
263 uint_t length = vec->length;
264 /* post part of the buffer does not exist */
265 if (pos < post + 1) {
266 for (k = 0; k < post + 1 - pos; k++)
267 medar[k] = 0.; /* 0-padding at the beginning */
268 for (k = post + 1 - pos; k < win_length; k++)
269 medar[k] = vec->data[0][k + pos - post];
270 /* the buffer is fully defined */
271 } else if (pos + pre < length) {
272 for (k = 0; k < win_length; k++)
273 medar[k] = vec->data[0][k + pos - post];
274 /* pre part of the buffer does not exist */
276 for (k = 0; k < length - pos + post; k++)
277 medar[k] = vec->data[0][k + pos - post];
278 for (k = length - pos + post; k < win_length; k++)
279 medar[k] = 0.; /* 0-padding at the end */
281 return fvec_median (tmpvec);
284 smpl_t fvec_median(fvec_t * input) {
285 uint_t n = input->length;
286 smpl_t * arr = (smpl_t *) input->data[0];
289 uint_t middle, ll, hh;
291 low = 0 ; high = n-1 ; median = (low + high) / 2;
293 if (high <= low) /* One element only */
296 if (high == low + 1) { /* Two elements only */
297 if (arr[low] > arr[high])
298 ELEM_SWAP(arr[low], arr[high]) ;
302 /* Find median of low, middle and high items; swap into position low */
303 middle = (low + high) / 2;
304 if (arr[middle] > arr[high]) ELEM_SWAP(arr[middle], arr[high]);
305 if (arr[low] > arr[high]) ELEM_SWAP(arr[low], arr[high]);
306 if (arr[middle] > arr[low]) ELEM_SWAP(arr[middle], arr[low]) ;
308 /* Swap low item (now in position middle) into position (low+1) */
309 ELEM_SWAP(arr[middle], arr[low+1]) ;
311 /* Nibble from each end towards middle, swapping items when stuck */
315 do ll++; while (arr[low] > arr[ll]) ;
316 do hh--; while (arr[hh] > arr[low]) ;
321 ELEM_SWAP(arr[ll], arr[hh]) ;
324 /* Swap middle item (in position low) back into correct position */
325 ELEM_SWAP(arr[low], arr[hh]) ;
327 /* Re-set active partition */
335 smpl_t fvec_quadint(fvec_t * x,uint_t pos, uint_t span) {
337 uint_t x0 = (pos < span) ? pos : pos - span;
338 uint_t x2 = (pos + span < x->length) ? pos + span : pos;
339 if (x0 == pos) return (x->data[0][pos] <= x->data[0][x2]) ? pos : x2;
340 if (x2 == pos) return (x->data[0][pos] <= x->data[0][x0]) ? pos : x0;
342 s1 = x->data[0][pos];
344 return pos + 0.5 * (s2 - s0 ) / (s2 - 2.* s1 + s0);
347 uint_t fvec_peakpick(fvec_t * onset, uint_t pos) {
349 /*for (i=0;i<onset->channels;i++)*/
350 tmp = (onset->data[i][pos] > onset->data[i][pos-1]
351 && onset->data[i][pos] > onset->data[i][pos+1]
352 && onset->data[i][pos] > 0.);
357 aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf)
360 s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2);
365 aubio_freqtomidi (smpl_t freq)
367 /* log(freq/A-2)/log(2) */
368 smpl_t midi = freq / 6.875;
369 midi = LOG (midi) / 0.69314718055995;
376 aubio_miditofreq (smpl_t midi)
378 smpl_t freq = (midi + 3.) / 12.;
379 freq = EXP (freq * 0.69314718055995);
385 aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
387 smpl_t freq = samplerate / fftsize;
392 aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
394 smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize);
395 return aubio_freqtomidi (midi);
399 aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize)
401 smpl_t bin = fftsize / samplerate;
406 aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize)
408 smpl_t freq = aubio_miditofreq (midi);
409 return aubio_freqtobin (freq, samplerate, fftsize);
413 aubio_is_power_of_two(uint_t a) {
414 if ((a & a-1) == 0) {
422 aubio_next_power_of_two(uint_t a) {
425 for (i = 0; i < sizeof(uint_t) * CHAR_BIT; i++ ) {
432 aubio_db_spl (fvec_t * o)
434 smpl_t val = SQRT (fvec_local_energy (o));
435 val /= (smpl_t) o->length;
440 aubio_silence_detection (fvec_t * o, smpl_t threshold)
442 return (aubio_db_spl (o) < threshold);
446 aubio_level_detection (fvec_t * o, smpl_t threshold)
448 smpl_t db_spl = aubio_db_spl (o);
449 if (db_spl < threshold) {
457 aubio_zero_crossing_rate (fvec_t * input)
461 for (j = 1; j < input->length; j++) {
462 // previous was strictly negative
463 if (input->data[i][j - 1] < 0.) {
464 // current is positive or null
465 if (input->data[i][j] >= 0.) {
468 // previous was positive or null
470 // current is strictly negative
471 if (input->data[i][j] < 0.) {
476 return zcr / (smpl_t) input->length;
480 aubio_autocorr (fvec_t * input, fvec_t * output)
482 uint_t i, j, k, length = input->length;
485 for (k = 0; k < input->channels; k++) {
486 data = input->data[k];
487 acf = output->data[k];
488 for (i = 0; i < length; i++) {
490 for (j = i; j < length; j++) {
491 tmp += data[j - i] * data[j];
493 acf[i] = tmp / (smpl_t) (length - i);