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"
37 aubio_win_blackman_harris,
41 aubio_win_default = aubio_win_hanningz,
45 new_aubio_window (char_t * window_type, uint_t size)
47 // create fvec of size x 1 channel
48 fvec_t * win = new_fvec( size, 1);
49 smpl_t * w = win->data[0];
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;
73 AUBIO_ERR ("unknown window type %s, using default.\n", window_type);
74 wintype = aubio_win_default;
78 case aubio_win_rectangle:
82 case aubio_win_hamming:
84 w[i] = 0.54 - 0.46 * COS(TWO_PI * i / (size));
86 case aubio_win_hanning:
88 w[i] = 0.5 - (0.5 * COS(TWO_PI * i / (size)));
90 case aubio_win_hanningz:
92 w[i] = 0.5 * (1.0 - COS(TWO_PI * i / (size)));
94 case aubio_win_blackman:
97 - 0.50 * COS( TWO_PI*i/(size-1.0))
98 + 0.08 * COS(2.0*TWO_PI*i/(size-1.0));
100 case aubio_win_blackman_harris:
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));
107 case aubio_win_gaussian:
109 w[i] = EXP(- 1.0 / SQR(size) * SQR(2.0*i-size));
111 case aubio_win_welch:
113 w[i] = 1.0 - SQR((2*i-size)/(size+1.0));
115 case aubio_win_parzen:
117 w[i] = 1.0 - ABS((2*i-size)/(size+1.0));
126 aubio_unwrap2pi (smpl_t phase)
128 /* mod(phase+pi,-2pi)+pi */
129 return phase + TWO_PI * (1. + FLOOR (-(phase + PI) / TWO_PI));
133 fvec_mean (fvec_t * s)
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);
144 fvec_mean_channel (fvec_t * s, uint_t i)
148 for (j = 0; j < s->length; j++)
149 tmp += s->data[i][j];
150 return tmp / (smpl_t) (s->length);
154 fvec_sum (fvec_t * s)
158 for (i = 0; i < s->channels; i++) {
159 for (j = 0; j < s->length; j++) {
160 tmp += s->data[i][j];
167 fvec_max (fvec_t * s)
171 for (i = 0; i < s->channels; i++) {
172 for (j = 0; j < s->length; j++) {
173 tmp = (tmp > s->data[i][j]) ? tmp : s->data[i][j];
180 fvec_min (fvec_t * s)
183 smpl_t tmp = s->data[0][0];
184 for (i = 0; i < s->channels; i++) {
185 for (j = 0; j < s->length; j++) {
186 tmp = (tmp < s->data[i][j]) ? tmp : s->data[i][j];
193 fvec_min_elem (fvec_t * s)
195 uint_t i, j, pos = 0.;
196 smpl_t tmp = s->data[0][0];
197 for (i = 0; i < s->channels; i++) {
198 for (j = 0; j < s->length; j++) {
199 pos = (tmp < s->data[i][j]) ? pos : j;
200 tmp = (tmp < s->data[i][j]) ? tmp : s->data[i][j];
207 fvec_max_elem (fvec_t * s)
209 uint_t i, j, pos = 0;
211 for (i = 0; i < s->channels; i++) {
212 for (j = 0; j < s->length; j++) {
213 pos = (tmp > s->data[i][j]) ? pos : j;
214 tmp = (tmp > s->data[i][j]) ? tmp : s->data[i][j];
221 fvec_shift (fvec_t * s)
224 for (i = 0; i < s->channels; i++) {
225 for (j = 0; j < s->length / 2; j++) {
226 ELEM_SWAP (s->data[i][j], s->data[i][j + s->length / 2]);
232 fvec_local_energy (fvec_t * f)
236 for (i = 0; i < f->channels; i++) {
237 for (j = 0; j < f->length; j++) {
238 energy += SQR (f->data[i][j]);
245 fvec_local_hfc (fvec_t * v)
249 for (i = 0; i < v->channels; i++) {
250 for (j = 0; j < v->length; j++) {
251 hfc += (i + 1) * v->data[i][j];
258 fvec_min_removal (fvec_t * v)
260 smpl_t v_min = fvec_min (v);
261 fvec_add (v, - v_min );
265 fvec_alpha_norm (fvec_t * o, smpl_t alpha)
269 for (i = 0; i < o->channels; i++) {
270 for (j = 0; j < o->length; j++) {
271 tmp += POW (ABS (o->data[i][j]), alpha);
274 return POW (tmp / o->length, 1. / alpha);
278 fvec_alpha_normalise (fvec_t * o, smpl_t alpha)
281 smpl_t norm = fvec_alpha_norm (o, alpha);
282 for (i = 0; i < o->channels; i++) {
283 for (j = 0; j < o->length; j++) {
284 o->data[i][j] /= norm;
290 fvec_add (fvec_t * o, smpl_t val)
293 for (i = 0; i < o->channels; i++) {
294 for (j = 0; j < o->length; j++) {
295 o->data[i][j] += val;
300 void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
301 uint_t post, uint_t pre, uint_t channel) {
302 uint_t length = vec->length, i=channel, j;
303 for (j=0;j<length;j++) {
304 vec->data[i][j] -= fvec_moving_thres(vec, tmp, post, pre, j, i);
309 fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec,
310 uint_t post, uint_t pre, uint_t pos, uint_t channel)
312 uint_t i = channel, k;
313 smpl_t *medar = (smpl_t *) tmpvec->data[i];
314 uint_t win_length = post + pre + 1;
315 uint_t length = vec->length;
316 /* post part of the buffer does not exist */
317 if (pos < post + 1) {
318 for (k = 0; k < post + 1 - pos; k++)
319 medar[k] = 0.; /* 0-padding at the beginning */
320 for (k = post + 1 - pos; k < win_length; k++)
321 medar[k] = vec->data[0][k + pos - post];
322 /* the buffer is fully defined */
323 } else if (pos + pre < length) {
324 for (k = 0; k < win_length; k++)
325 medar[k] = vec->data[0][k + pos - post];
326 /* pre part of the buffer does not exist */
328 for (k = 0; k < length - pos + post; k++)
329 medar[k] = vec->data[0][k + pos - post];
330 for (k = length - pos + post; k < win_length; k++)
331 medar[k] = 0.; /* 0-padding at the end */
333 return fvec_median_channel (tmpvec, i);
336 smpl_t fvec_median_channel (fvec_t * input, uint_t channel) {
337 uint_t n = input->length;
338 smpl_t * arr = (smpl_t *) input->data[channel];
341 uint_t middle, ll, hh;
343 low = 0 ; high = n-1 ; median = (low + high) / 2;
345 if (high <= low) /* One element only */
348 if (high == low + 1) { /* Two elements only */
349 if (arr[low] > arr[high])
350 ELEM_SWAP(arr[low], arr[high]) ;
354 /* Find median of low, middle and high items; swap into position low */
355 middle = (low + high) / 2;
356 if (arr[middle] > arr[high]) ELEM_SWAP(arr[middle], arr[high]);
357 if (arr[low] > arr[high]) ELEM_SWAP(arr[low], arr[high]);
358 if (arr[middle] > arr[low]) ELEM_SWAP(arr[middle], arr[low]) ;
360 /* Swap low item (now in position middle) into position (low+1) */
361 ELEM_SWAP(arr[middle], arr[low+1]) ;
363 /* Nibble from each end towards middle, swapping items when stuck */
367 do ll++; while (arr[low] > arr[ll]) ;
368 do hh--; while (arr[hh] > arr[low]) ;
373 ELEM_SWAP(arr[ll], arr[hh]) ;
376 /* Swap middle item (in position low) back into correct position */
377 ELEM_SWAP(arr[low], arr[hh]) ;
379 /* Re-set active partition */
387 smpl_t fvec_quadint (fvec_t * x, uint_t pos, uint_t i) {
389 uint_t x0 = (pos < 1) ? pos : pos - 1;
390 uint_t x2 = (pos + 1 < x->length) ? pos + 1 : pos;
391 if (x0 == pos) return (x->data[i][pos] <= x->data[i][x2]) ? pos : x2;
392 if (x2 == pos) return (x->data[i][pos] <= x->data[i][x0]) ? pos : x0;
394 s1 = x->data[i][pos];
396 return pos + 0.5 * (s2 - s0 ) / (s2 - 2.* s1 + s0);
399 uint_t fvec_peakpick(fvec_t * onset, uint_t pos) {
401 /*for (i=0;i<onset->channels;i++)*/
402 tmp = (onset->data[i][pos] > onset->data[i][pos-1]
403 && onset->data[i][pos] > onset->data[i][pos+1]
404 && onset->data[i][pos] > 0.);
409 aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf)
412 s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2);
417 aubio_freqtomidi (smpl_t freq)
419 /* log(freq/A-2)/log(2) */
420 smpl_t midi = freq / 6.875;
421 midi = LOG (midi) / 0.69314718055995;
428 aubio_miditofreq (smpl_t midi)
430 smpl_t freq = (midi + 3.) / 12.;
431 freq = EXP (freq * 0.69314718055995);
437 aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
439 smpl_t freq = samplerate / fftsize;
444 aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
446 smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize);
447 return aubio_freqtomidi (midi);
451 aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize)
453 smpl_t bin = fftsize / samplerate;
458 aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize)
460 smpl_t freq = aubio_miditofreq (midi);
461 return aubio_freqtobin (freq, samplerate, fftsize);
465 aubio_is_power_of_two (uint_t a)
467 if ((a & (a - 1)) == 0) {
475 aubio_next_power_of_two (uint_t a)
479 for (i = 0; i < sizeof (uint_t) * CHAR_BIT; i++) {
486 aubio_db_spl (fvec_t * o)
488 smpl_t val = SQRT (fvec_local_energy (o));
489 val /= (smpl_t) o->length;
494 aubio_silence_detection (fvec_t * o, smpl_t threshold)
496 return (aubio_db_spl (o) < threshold);
500 aubio_level_detection (fvec_t * o, smpl_t threshold)
502 smpl_t db_spl = aubio_db_spl (o);
503 if (db_spl < threshold) {
511 aubio_zero_crossing_rate (fvec_t * input)
515 for (j = 1; j < input->length; j++) {
516 // previous was strictly negative
517 if (input->data[i][j - 1] < 0.) {
518 // current is positive or null
519 if (input->data[i][j] >= 0.) {
522 // previous was positive or null
524 // current is strictly negative
525 if (input->data[i][j] < 0.) {
530 return zcr / (smpl_t) input->length;
534 aubio_autocorr (fvec_t * input, fvec_t * output)
536 uint_t i, j, k, length = input->length;
539 for (k = 0; k < input->channels; k++) {
540 data = input->data[k];
541 acf = output->data[k];
542 for (i = 0; i < length; i++) {
544 for (j = i; j < length; j++) {
545 tmp += data[j - i] * data[j];
547 acf[i] = tmp / (smpl_t) (length - i);