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 fvec_t * win = new_fvec (size);
49 smpl_t * w = win->data;
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 (j = 0; j < s->length; j++) {
140 return tmp / (smpl_t) (s->length);
144 fvec_sum (fvec_t * s)
148 for (j = 0; j < s->length; j++) {
155 fvec_max (fvec_t * s)
159 for (j = 0; j < s->length; j++) {
160 tmp = (tmp > s->data[j]) ? tmp : s->data[j];
166 fvec_min (fvec_t * s)
169 smpl_t tmp = s->data[0];
170 for (j = 0; j < s->length; j++) {
171 tmp = (tmp < s->data[j]) ? tmp : s->data[j];
177 fvec_min_elem (fvec_t * s)
180 smpl_t tmp = s->data[0];
181 for (j = 0; j < s->length; j++) {
182 pos = (tmp < s->data[j]) ? pos : j;
183 tmp = (tmp < s->data[j]) ? tmp : s->data[j];
189 fvec_max_elem (fvec_t * s)
193 for (j = 0; j < s->length; j++) {
194 pos = (tmp > s->data[j]) ? pos : j;
195 tmp = (tmp > s->data[j]) ? tmp : s->data[j];
201 fvec_shift (fvec_t * s)
204 for (j = 0; j < s->length / 2; j++) {
205 ELEM_SWAP (s->data[j], s->data[j + s->length / 2]);
210 fvec_local_energy (fvec_t * f)
214 for (j = 0; j < f->length; j++) {
215 energy += SQR (f->data[j]);
221 fvec_local_hfc (fvec_t * v)
225 for (j = 0; j < v->length; j++) {
226 hfc += (j + 1) * v->data[j];
232 fvec_min_removal (fvec_t * v)
234 smpl_t v_min = fvec_min (v);
235 fvec_add (v, - v_min );
239 fvec_alpha_norm (fvec_t * o, smpl_t alpha)
243 for (j = 0; j < o->length; j++) {
244 tmp += POW (ABS (o->data[j]), alpha);
246 return POW (tmp / o->length, 1. / alpha);
250 fvec_alpha_normalise (fvec_t * o, smpl_t alpha)
253 smpl_t norm = fvec_alpha_norm (o, alpha);
254 for (j = 0; j < o->length; j++) {
260 fvec_add (fvec_t * o, smpl_t val)
263 for (j = 0; j < o->length; j++) {
268 void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
269 uint_t post, uint_t pre) {
270 uint_t length = vec->length, j;
271 for (j=0;j<length;j++) {
272 vec->data[j] -= fvec_moving_thres(vec, tmp, post, pre, j);
277 fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec,
278 uint_t post, uint_t pre, uint_t pos)
281 smpl_t *medar = (smpl_t *) tmpvec->data;
282 uint_t win_length = post + pre + 1;
283 uint_t length = vec->length;
284 /* post part of the buffer does not exist */
285 if (pos < post + 1) {
286 for (k = 0; k < post + 1 - pos; k++)
287 medar[k] = 0.; /* 0-padding at the beginning */
288 for (k = post + 1 - pos; k < win_length; k++)
289 medar[k] = vec->data[k + pos - post];
290 /* the buffer is fully defined */
291 } else if (pos + pre < length) {
292 for (k = 0; k < win_length; k++)
293 medar[k] = vec->data[k + pos - post];
294 /* pre part of the buffer does not exist */
296 for (k = 0; k < length - pos + post; k++)
297 medar[k] = vec->data[k + pos - post];
298 for (k = length - pos + post; k < win_length; k++)
299 medar[k] = 0.; /* 0-padding at the end */
301 return fvec_median (tmpvec);
304 smpl_t fvec_median (fvec_t * input) {
305 uint_t n = input->length;
306 smpl_t * arr = (smpl_t *) input->data;
309 uint_t middle, ll, hh;
311 low = 0 ; high = n-1 ; median = (low + high) / 2;
313 if (high <= low) /* One element only */
316 if (high == low + 1) { /* Two elements only */
317 if (arr[low] > arr[high])
318 ELEM_SWAP(arr[low], arr[high]) ;
322 /* Find median of low, middle and high items; swap into position low */
323 middle = (low + high) / 2;
324 if (arr[middle] > arr[high]) ELEM_SWAP(arr[middle], arr[high]);
325 if (arr[low] > arr[high]) ELEM_SWAP(arr[low], arr[high]);
326 if (arr[middle] > arr[low]) ELEM_SWAP(arr[middle], arr[low]) ;
328 /* Swap low item (now in position middle) into position (low+1) */
329 ELEM_SWAP(arr[middle], arr[low+1]) ;
331 /* Nibble from each end towards middle, swapping items when stuck */
335 do ll++; while (arr[low] > arr[ll]) ;
336 do hh--; while (arr[hh] > arr[low]) ;
341 ELEM_SWAP(arr[ll], arr[hh]) ;
344 /* Swap middle item (in position low) back into correct position */
345 ELEM_SWAP(arr[low], arr[hh]) ;
347 /* Re-set active partition */
355 smpl_t fvec_quadint (fvec_t * x, uint_t pos) {
357 uint_t x0 = (pos < 1) ? pos : pos - 1;
358 uint_t x2 = (pos + 1 < x->length) ? pos + 1 : pos;
359 if (x0 == pos) return (x->data[pos] <= x->data[x2]) ? pos : x2;
360 if (x2 == pos) return (x->data[pos] <= x->data[x0]) ? pos : x0;
364 return pos + 0.5 * (s2 - s0 ) / (s2 - 2.* s1 + s0);
367 uint_t fvec_peakpick(fvec_t * onset, uint_t pos) {
369 tmp = (onset->data[pos] > onset->data[pos-1]
370 && onset->data[pos] > onset->data[pos+1]
371 && onset->data[pos] > 0.);
376 aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf)
379 s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2);
384 aubio_freqtomidi (smpl_t freq)
386 /* log(freq/A-2)/log(2) */
387 smpl_t midi = freq / 6.875;
388 midi = LOG (midi) / 0.69314718055995;
395 aubio_miditofreq (smpl_t midi)
397 smpl_t freq = (midi + 3.) / 12.;
398 freq = EXP (freq * 0.69314718055995);
404 aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
406 smpl_t freq = samplerate / fftsize;
411 aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
413 smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize);
414 return aubio_freqtomidi (midi);
418 aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize)
420 smpl_t bin = fftsize / samplerate;
425 aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize)
427 smpl_t freq = aubio_miditofreq (midi);
428 return aubio_freqtobin (freq, samplerate, fftsize);
432 aubio_is_power_of_two (uint_t a)
434 if ((a & (a - 1)) == 0) {
442 aubio_next_power_of_two (uint_t a)
445 while (i < a) i <<= 1;
450 aubio_db_spl (fvec_t * o)
452 smpl_t val = SQRT (fvec_local_energy (o));
453 val /= (smpl_t) o->length;
458 aubio_silence_detection (fvec_t * o, smpl_t threshold)
460 return (aubio_db_spl (o) < threshold);
464 aubio_level_detection (fvec_t * o, smpl_t threshold)
466 smpl_t db_spl = aubio_db_spl (o);
467 if (db_spl < threshold) {
475 aubio_zero_crossing_rate (fvec_t * input)
479 for (j = 1; j < input->length; j++) {
480 // previous was strictly negative
481 if (input->data[j - 1] < 0.) {
482 // current is positive or null
483 if (input->data[j] >= 0.) {
486 // previous was positive or null
488 // current is strictly negative
489 if (input->data[j] < 0.) {
494 return zcr / (smpl_t) input->length;
498 aubio_autocorr (fvec_t * input, fvec_t * output)
500 uint_t i, j, length = input->length;
505 for (i = 0; i < length; i++) {
507 for (j = i; j < length; j++) {
508 tmp += data[j - i] * data[j];
510 acf[i] = tmp / (smpl_t) (length - i);