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"
28 fvec_t * new_aubio_window(uint_t size, aubio_window_type wintype) {
29 // create fvec of size x 1 channel
30 fvec_t * win = new_fvec( size, 1);
31 smpl_t * w = win->data[0];
34 case aubio_win_rectangle:
38 case aubio_win_hamming:
40 w[i] = 0.54 - 0.46 * COS(TWO_PI * i / (size));
42 case aubio_win_hanning:
44 w[i] = 0.5 - (0.5 * COS(TWO_PI * i / (size)));
46 case aubio_win_hanningz:
48 w[i] = 0.5 * (1.0 - COS(TWO_PI * i / (size)));
50 case aubio_win_blackman:
53 - 0.50 * COS( TWO_PI*i/(size-1.0))
54 + 0.08 * COS(2.0*TWO_PI*i/(size-1.0));
56 case aubio_win_blackman_harris:
59 - 0.48829 * COS( TWO_PI*i/(size-1.0))
60 + 0.14128 * COS(2.0*TWO_PI*i/(size-1.0))
61 - 0.01168 * COS(3.0*TWO_PI*i/(size-1.0));
63 case aubio_win_gaussian:
65 w[i] = EXP(- 1.0 / SQR(size) * SQR(2.0*i-size));
69 w[i] = 1.0 - SQR((2*i-size)/(size+1.0));
71 case aubio_win_parzen:
73 w[i] = 1.0 - ABS((2*i-size)/(size+1.0));
81 smpl_t aubio_unwrap2pi(smpl_t phase) {
82 /* mod(phase+pi,-2pi)+pi */
83 return phase + TWO_PI * (1. + FLOOR(-(phase+PI)/TWO_PI));
86 smpl_t fvec_mean(fvec_t *s) {
89 for (i=0; i < s->channels; i++)
90 for (j=0; j < s->length; j++)
92 return tmp/(smpl_t)(s->length);
95 smpl_t fvec_sum(fvec_t *s) {
98 for (i=0; i < s->channels; i++)
99 for (j=0; j < s->length; j++)
100 tmp += s->data[i][j];
104 smpl_t fvec_max(fvec_t *s) {
107 for (i=0; i < s->channels; i++)
108 for (j=0; j < s->length; j++)
109 tmp = (tmp > s->data[i][j])? tmp : s->data[i][j];
113 smpl_t fvec_min(fvec_t *s) {
115 smpl_t tmp = s->data[0][0];
116 for (i=0; i < s->channels; i++)
117 for (j=0; j < s->length; j++)
118 tmp = (tmp < s->data[i][j])? tmp : s->data[i][j] ;
122 uint_t fvec_min_elem(fvec_t *s) {
123 uint_t i,j=0, pos=0.;
124 smpl_t tmp = s->data[0][0];
125 for (i=0; i < s->channels; i++)
126 for (j=0; j < s->length; j++) {
127 pos = (tmp < s->data[i][j])? pos : j;
128 tmp = (tmp < s->data[i][j])? tmp : s->data[i][j] ;
133 uint_t fvec_max_elem(fvec_t *s) {
134 uint_t i,j=0, pos=0.;
136 for (i=0; i < s->channels; i++)
137 for (j=0; j < s->length; j++) {
138 pos = (tmp > s->data[i][j])? pos : j;
139 tmp = (tmp > s->data[i][j])? tmp : s->data[i][j] ;
144 void fvec_shift(fvec_t *s) {
147 for (i=0; i < s->channels; i++)
148 for (j=0; j < s->length / 2 ; j++) {
149 //tmp = s->data[i][j];
150 //s->data[i][j] = s->data[i][j+s->length/2];
151 //s->data[i][j+s->length/2] = tmp;
152 ELEM_SWAP(s->data[i][j],s->data[i][j+s->length/2]);
156 smpl_t fvec_local_energy(fvec_t * f) {
159 for (i=0;i<f->channels;i++)
160 for (j=0;j<f->length;j++)
161 locE+=SQR(f->data[i][j]);
165 smpl_t fvec_local_hfc(fvec_t * f) {
168 for (i=0;i<f->channels;i++)
169 for (j=0;j<f->length;j++)
170 locE+=(i+1)*f->data[i][j];
174 smpl_t fvec_alpha_norm(fvec_t * DF, smpl_t alpha) {
177 for (i=0;i<DF->channels;i++)
178 for (j=0;j<DF->length;j++)
179 tmp += POW(ABS(DF->data[i][j]),alpha);
180 return POW(tmp/DF->length,1./alpha);
183 void fvec_dc_removal(fvec_t * mag) {
185 uint_t length = mag->length, i=0, j;
186 mini = fvec_min(mag);
187 for (j=0;j<length;j++) {
188 mag->data[i][j] -= mini;
192 void fvec_alpha_normalise(fvec_t * mag, uint_t alpha) {
194 uint_t length = mag->length, i=0, j;
195 alphan = fvec_alpha_norm(mag,alpha);
196 for (j=0;j<length;j++){
197 mag->data[i][j] /= alphan;
201 void fvec_add(fvec_t * mag, smpl_t threshold) {
202 uint_t length = mag->length, i=0, j;
203 for (j=0;j<length;j++) {
204 mag->data[i][j] += threshold;
208 void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
209 uint_t post, uint_t pre) {
210 uint_t length = vec->length, i=0, j;
211 for (j=0;j<length;j++) {
212 vec->data[i][j] -= fvec_moving_thres(vec, tmp, post, pre, j);
216 smpl_t fvec_moving_thres(fvec_t * vec, fvec_t * tmpvec,
217 uint_t post, uint_t pre, uint_t pos) {
218 smpl_t * medar = (smpl_t *)tmpvec->data[0];
220 uint_t win_length = post+pre+1;
221 uint_t length = vec->length;
222 /* post part of the buffer does not exist */
224 for (k=0;k<post+1-pos;k++)
225 medar[k] = 0.; /* 0-padding at the beginning */
226 for (k=post+1-pos;k<win_length;k++)
227 medar[k] = vec->data[0][k+pos-post];
228 /* the buffer is fully defined */
229 } else if (pos+pre<length) {
230 for (k=0;k<win_length;k++)
231 medar[k] = vec->data[0][k+pos-post];
232 /* pre part of the buffer does not exist */
234 for (k=0;k<length-pos+post;k++)
235 medar[k] = vec->data[0][k+pos-post];
236 for (k=length-pos+post;k<win_length;k++)
237 medar[k] = 0.; /* 0-padding at the end */
239 return fvec_median(tmpvec);
242 smpl_t fvec_median(fvec_t * input) {
243 uint_t n = input->length;
244 smpl_t * arr = (smpl_t *) input->data[0];
247 uint_t middle, ll, hh;
249 low = 0 ; high = n-1 ; median = (low + high) / 2;
251 if (high <= low) /* One element only */
254 if (high == low + 1) { /* Two elements only */
255 if (arr[low] > arr[high])
256 ELEM_SWAP(arr[low], arr[high]) ;
260 /* Find median of low, middle and high items; swap into position low */
261 middle = (low + high) / 2;
262 if (arr[middle] > arr[high]) ELEM_SWAP(arr[middle], arr[high]);
263 if (arr[low] > arr[high]) ELEM_SWAP(arr[low], arr[high]);
264 if (arr[middle] > arr[low]) ELEM_SWAP(arr[middle], arr[low]) ;
266 /* Swap low item (now in position middle) into position (low+1) */
267 ELEM_SWAP(arr[middle], arr[low+1]) ;
269 /* Nibble from each end towards middle, swapping items when stuck */
273 do ll++; while (arr[low] > arr[ll]) ;
274 do hh--; while (arr[hh] > arr[low]) ;
279 ELEM_SWAP(arr[ll], arr[hh]) ;
282 /* Swap middle item (in position low) back into correct position */
283 ELEM_SWAP(arr[low], arr[hh]) ;
285 /* Re-set active partition */
293 smpl_t fvec_quadint(fvec_t * x,uint_t pos, uint_t span) {
295 uint_t x0 = (pos < span) ? pos : pos - span;
296 uint_t x2 = (pos + span < x->length) ? pos + span : pos;
297 if (x0 == pos) return (x->data[0][pos] <= x->data[0][x2]) ? pos : x2;
298 if (x2 == pos) return (x->data[0][pos] <= x->data[0][x0]) ? pos : x0;
300 s1 = x->data[0][pos] ;
302 return pos + 0.5 * (s2 - s0 ) / (s2 - 2.* s1 + s0);
305 smpl_t aubio_quadfrac(smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf) {
306 smpl_t tmp = s0 + (pf/2.) * (pf * ( s0 - 2.*s1 + s2 ) - 3.*s0 + 4.*s1 - s2);
310 uint_t fvec_peakpick(fvec_t * onset, uint_t pos) {
312 /*for (i=0;i<onset->channels;i++)*/
313 tmp = (onset->data[i][pos] > onset->data[i][pos-1]
314 && onset->data[i][pos] > onset->data[i][pos+1]
315 && onset->data[i][pos] > 0.);
319 smpl_t aubio_freqtomidi(smpl_t freq) {
320 /* log(freq/A-2)/log(2) */
321 smpl_t midi = freq/6.875;
322 midi = LOG(midi)/0.69314718055995;
328 smpl_t aubio_miditofreq(smpl_t midi) {
329 smpl_t freq = (midi+3.)/12.;
330 freq = EXP(freq*0.69314718055995);
335 smpl_t aubio_bintofreq(smpl_t bin, smpl_t samplerate, smpl_t fftsize) {
336 smpl_t freq = samplerate/fftsize;
340 smpl_t aubio_bintomidi(smpl_t bin, smpl_t samplerate, smpl_t fftsize) {
341 smpl_t midi = aubio_bintofreq(bin,samplerate,fftsize);
342 return aubio_freqtomidi(midi);
345 smpl_t aubio_freqtobin(smpl_t freq, smpl_t samplerate, smpl_t fftsize) {
346 smpl_t bin = fftsize/samplerate;
350 smpl_t aubio_miditobin(smpl_t midi, smpl_t samplerate, smpl_t fftsize) {
351 smpl_t freq = aubio_miditofreq(midi);
352 return aubio_freqtobin(freq,samplerate,fftsize);
355 /** returns 1 if wassilence is 0 and RMS(ibuf)<threshold
358 uint_t aubio_silence_detection(fvec_t * ibuf, smpl_t threshold) {
361 for (j=0;j<ibuf->length;j++) {
362 loudness += SQR(ibuf->data[i][j]);
364 loudness = SQRT(loudness);
365 loudness /= (smpl_t)ibuf->length;
366 loudness = LIN2DB(loudness);
368 return (loudness < threshold);
371 /** returns level log(RMS(ibuf)) if < threshold, 1 otherwise
374 smpl_t aubio_level_detection(fvec_t * ibuf, smpl_t threshold) {
377 for (j=0;j<ibuf->length;j++) {
378 loudness += SQR(ibuf->data[i][j]);
380 loudness = SQRT(loudness);
381 loudness /= (smpl_t)ibuf->length;
382 loudness = LIN2DB(loudness);
384 if (loudness < threshold)
390 smpl_t aubio_zero_crossing_rate(fvec_t * input) {
393 for ( j = 1; j < input->length; j++ ) {
394 // previous was strictly negative
395 if( input->data[i][j-1] < 0. ) {
396 // current is positive or null
397 if ( input->data[i][j] >= 0. ) {
400 // previous was positive or null
402 // current is strictly negative
403 if ( input->data[i][j] < 0. ) {
408 return zcr/(smpl_t)input->length;
411 void aubio_autocorr(fvec_t * input, fvec_t * output) {
412 uint_t i = 0, j = 0, length = input->length;
413 smpl_t * data = input->data[0];
414 smpl_t * acf = output->data[0];
416 for(i=0;i<length;i++){
417 for(j=i;j<length;j++){
418 tmp += data[j-i]*data[j];
420 acf[i] = tmp /(smpl_t)(length-i);
425 void aubio_cleanup(void) {