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"
26 #include "musicutils.h"
38 aubio_win_blackman_harris,
42 aubio_win_default = aubio_win_hanningz,
46 new_aubio_window (char_t * window_type, uint_t size)
48 // create fvec of size x 1 channel
49 fvec_t * win = new_fvec( size, 1);
50 smpl_t * w = win->data[0];
52 aubio_window_type wintype;
53 if (strcmp (window_type, "rectangle") == 0)
54 wintype = aubio_win_rectangle;
55 else if (strcmp (window_type, "hamming") == 0)
56 wintype = aubio_win_hamming;
57 else if (strcmp (window_type, "hanning") == 0)
58 wintype = aubio_win_hanning;
59 else if (strcmp (window_type, "hanningz") == 0)
60 wintype = aubio_win_hanningz;
61 else if (strcmp (window_type, "blackman") == 0)
62 wintype = aubio_win_blackman;
63 else if (strcmp (window_type, "blackman_harris") == 0)
64 wintype = aubio_win_blackman_harris;
65 else if (strcmp (window_type, "gaussian") == 0)
66 wintype = aubio_win_gaussian;
67 else if (strcmp (window_type, "welch") == 0)
68 wintype = aubio_win_welch;
69 else if (strcmp (window_type, "parzen") == 0)
70 wintype = aubio_win_parzen;
71 else if (strcmp (window_type, "default") == 0)
72 wintype = aubio_win_default;
74 AUBIO_ERR ("unknown window type %s, using default.\n", window_type);
75 wintype = aubio_win_default;
79 case aubio_win_rectangle:
83 case aubio_win_hamming:
85 w[i] = 0.54 - 0.46 * COS(TWO_PI * i / (size));
87 case aubio_win_hanning:
89 w[i] = 0.5 - (0.5 * COS(TWO_PI * i / (size)));
91 case aubio_win_hanningz:
93 w[i] = 0.5 * (1.0 - COS(TWO_PI * i / (size)));
95 case aubio_win_blackman:
98 - 0.50 * COS( TWO_PI*i/(size-1.0))
99 + 0.08 * COS(2.0*TWO_PI*i/(size-1.0));
101 case aubio_win_blackman_harris:
104 - 0.48829 * COS( TWO_PI*i/(size-1.0))
105 + 0.14128 * COS(2.0*TWO_PI*i/(size-1.0))
106 - 0.01168 * COS(3.0*TWO_PI*i/(size-1.0));
108 case aubio_win_gaussian:
110 w[i] = EXP(- 1.0 / SQR(size) * SQR(2.0*i-size));
112 case aubio_win_welch:
114 w[i] = 1.0 - SQR((2*i-size)/(size+1.0));
116 case aubio_win_parzen:
118 w[i] = 1.0 - ABS((2*i-size)/(size+1.0));
127 aubio_unwrap2pi (smpl_t phase)
129 /* mod(phase+pi,-2pi)+pi */
130 return phase + TWO_PI * (1. + FLOOR (-(phase + PI) / TWO_PI));
134 fvec_mean (fvec_t * s)
138 for (i = 0; i < s->channels; i++)
139 for (j = 0; j < s->length; j++)
140 tmp += s->data[i][j];
141 return tmp / (smpl_t) (s->length);
145 fvec_mean_channel (fvec_t * s, uint_t i)
149 for (j = 0; j < s->length; j++)
150 tmp += s->data[i][j];
151 return tmp / (smpl_t) (s->length);
155 fvec_sum (fvec_t * s)
159 for (i = 0; i < s->channels; i++) {
160 for (j = 0; j < s->length; j++) {
161 tmp += s->data[i][j];
168 fvec_max (fvec_t * s)
172 for (i = 0; i < s->channels; i++) {
173 for (j = 0; j < s->length; j++) {
174 tmp = (tmp > s->data[i][j]) ? tmp : s->data[i][j];
181 fvec_min (fvec_t * s)
184 smpl_t tmp = s->data[0][0];
185 for (i = 0; i < s->channels; i++) {
186 for (j = 0; j < s->length; j++) {
187 tmp = (tmp < s->data[i][j]) ? tmp : s->data[i][j];
194 fvec_min_elem (fvec_t * s)
196 uint_t i, j, pos = 0.;
197 smpl_t tmp = s->data[0][0];
198 for (i = 0; i < s->channels; i++) {
199 for (j = 0; j < s->length; j++) {
200 pos = (tmp < s->data[i][j]) ? pos : j;
201 tmp = (tmp < s->data[i][j]) ? tmp : s->data[i][j];
208 fvec_max_elem (fvec_t * s)
210 uint_t i, j, pos = 0;
212 for (i = 0; i < s->channels; i++) {
213 for (j = 0; j < s->length; j++) {
214 pos = (tmp > s->data[i][j]) ? pos : j;
215 tmp = (tmp > s->data[i][j]) ? tmp : s->data[i][j];
222 fvec_shift (fvec_t * s)
225 for (i = 0; i < s->channels; i++) {
226 for (j = 0; j < s->length / 2; j++) {
227 ELEM_SWAP (s->data[i][j], s->data[i][j + s->length / 2]);
233 fvec_local_energy (fvec_t * f)
237 for (i = 0; i < f->channels; i++) {
238 for (j = 0; j < f->length; j++) {
239 energy += SQR (f->data[i][j]);
246 fvec_local_hfc (fvec_t * v)
250 for (i = 0; i < v->channels; i++) {
251 for (j = 0; j < v->length; j++) {
252 hfc += (i + 1) * v->data[i][j];
259 fvec_min_removal (fvec_t * v)
261 smpl_t v_min = fvec_min (v);
262 fvec_add (v, - v_min );
266 fvec_alpha_norm (fvec_t * o, smpl_t alpha)
270 for (i = 0; i < o->channels; i++) {
271 for (j = 0; j < o->length; j++) {
272 tmp += POW (ABS (o->data[i][j]), alpha);
275 return POW (tmp / o->length, 1. / alpha);
279 fvec_alpha_normalise (fvec_t * o, smpl_t alpha)
282 smpl_t norm = fvec_alpha_norm (o, alpha);
283 for (i = 0; i < o->channels; i++) {
284 for (j = 0; j < o->length; j++) {
285 o->data[i][j] /= norm;
291 fvec_add (fvec_t * o, smpl_t val)
294 for (i = 0; i < o->channels; i++) {
295 for (j = 0; j < o->length; j++) {
296 o->data[i][j] += val;
301 void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
302 uint_t post, uint_t pre, uint_t channel) {
303 uint_t length = vec->length, i=channel, j;
304 for (j=0;j<length;j++) {
305 vec->data[i][j] -= fvec_moving_thres(vec, tmp, post, pre, j, i);
310 fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec,
311 uint_t post, uint_t pre, uint_t pos, uint_t channel)
313 uint_t i = channel, k;
314 smpl_t *medar = (smpl_t *) tmpvec->data[i];
315 uint_t win_length = post + pre + 1;
316 uint_t length = vec->length;
317 /* post part of the buffer does not exist */
318 if (pos < post + 1) {
319 for (k = 0; k < post + 1 - pos; k++)
320 medar[k] = 0.; /* 0-padding at the beginning */
321 for (k = post + 1 - pos; k < win_length; k++)
322 medar[k] = vec->data[0][k + pos - post];
323 /* the buffer is fully defined */
324 } else if (pos + pre < length) {
325 for (k = 0; k < win_length; k++)
326 medar[k] = vec->data[0][k + pos - post];
327 /* pre part of the buffer does not exist */
329 for (k = 0; k < length - pos + post; k++)
330 medar[k] = vec->data[0][k + pos - post];
331 for (k = length - pos + post; k < win_length; k++)
332 medar[k] = 0.; /* 0-padding at the end */
334 return fvec_median_channel (tmpvec, i);
337 smpl_t fvec_median_channel (fvec_t * input, uint_t channel) {
338 uint_t n = input->length;
339 smpl_t * arr = (smpl_t *) input->data[channel];
342 uint_t middle, ll, hh;
344 low = 0 ; high = n-1 ; median = (low + high) / 2;
346 if (high <= low) /* One element only */
349 if (high == low + 1) { /* Two elements only */
350 if (arr[low] > arr[high])
351 ELEM_SWAP(arr[low], arr[high]) ;
355 /* Find median of low, middle and high items; swap into position low */
356 middle = (low + high) / 2;
357 if (arr[middle] > arr[high]) ELEM_SWAP(arr[middle], arr[high]);
358 if (arr[low] > arr[high]) ELEM_SWAP(arr[low], arr[high]);
359 if (arr[middle] > arr[low]) ELEM_SWAP(arr[middle], arr[low]) ;
361 /* Swap low item (now in position middle) into position (low+1) */
362 ELEM_SWAP(arr[middle], arr[low+1]) ;
364 /* Nibble from each end towards middle, swapping items when stuck */
368 do ll++; while (arr[low] > arr[ll]) ;
369 do hh--; while (arr[hh] > arr[low]) ;
374 ELEM_SWAP(arr[ll], arr[hh]) ;
377 /* Swap middle item (in position low) back into correct position */
378 ELEM_SWAP(arr[low], arr[hh]) ;
380 /* Re-set active partition */
388 smpl_t fvec_quadint (fvec_t * x, uint_t pos, uint_t i) {
390 uint_t x0 = (pos < 1) ? pos : pos - 1;
391 uint_t x2 = (pos + 1 < x->length) ? pos + 1 : pos;
392 if (x0 == pos) return (x->data[i][pos] <= x->data[i][x2]) ? pos : x2;
393 if (x2 == pos) return (x->data[i][pos] <= x->data[i][x0]) ? pos : x0;
395 s1 = x->data[i][pos];
397 return pos + 0.5 * (s2 - s0 ) / (s2 - 2.* s1 + s0);
400 uint_t fvec_peakpick(fvec_t * onset, uint_t pos) {
402 /*for (i=0;i<onset->channels;i++)*/
403 tmp = (onset->data[i][pos] > onset->data[i][pos-1]
404 && onset->data[i][pos] > onset->data[i][pos+1]
405 && onset->data[i][pos] > 0.);
410 aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf)
413 s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2);
418 aubio_freqtomidi (smpl_t freq)
420 /* log(freq/A-2)/log(2) */
421 smpl_t midi = freq / 6.875;
422 midi = LOG (midi) / 0.69314718055995;
429 aubio_miditofreq (smpl_t midi)
431 smpl_t freq = (midi + 3.) / 12.;
432 freq = EXP (freq * 0.69314718055995);
438 aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
440 smpl_t freq = samplerate / fftsize;
445 aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
447 smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize);
448 return aubio_freqtomidi (midi);
452 aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize)
454 smpl_t bin = fftsize / samplerate;
459 aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize)
461 smpl_t freq = aubio_miditofreq (midi);
462 return aubio_freqtobin (freq, samplerate, fftsize);
466 aubio_is_power_of_two (uint_t a)
468 if ((a & (a - 1)) == 0) {
476 aubio_next_power_of_two (uint_t a)
480 for (i = 0; i < sizeof (uint_t) * CHAR_BIT; i++) {
487 aubio_db_spl (fvec_t * o)
489 smpl_t val = SQRT (fvec_local_energy (o));
490 val /= (smpl_t) o->length;
495 aubio_silence_detection (fvec_t * o, smpl_t threshold)
497 return (aubio_db_spl (o) < threshold);
501 aubio_level_detection (fvec_t * o, smpl_t threshold)
503 smpl_t db_spl = aubio_db_spl (o);
504 if (db_spl < threshold) {
512 aubio_zero_crossing_rate (fvec_t * input)
516 for (j = 1; j < input->length; j++) {
517 // previous was strictly negative
518 if (input->data[i][j - 1] < 0.) {
519 // current is positive or null
520 if (input->data[i][j] >= 0.) {
523 // previous was positive or null
525 // current is strictly negative
526 if (input->data[i][j] < 0.) {
531 return zcr / (smpl_t) input->length;
535 aubio_autocorr (fvec_t * input, fvec_t * output)
537 uint_t i, j, k, length = input->length;
540 for (k = 0; k < input->channels; k++) {
541 data = input->data[k];
542 acf = output->data[k];
543 for (i = 0; i < length; i++) {
545 for (j = i; j < length; j++) {
546 tmp += data[j - i] * data[j];
548 acf[i] = tmp / (smpl_t) (length - i);