X-Git-Url: http://git.tremily.us/?a=blobdiff_plain;f=src%2Fmathutils.c;h=7d2424e05e2ff12b1625b1400e54ae9c653b0185;hb=HEAD;hp=f82549a6dda9dbfec2e90fd7cb1338cb74c9f854;hpb=ade9afe663f3175a6ed9e75c0866fa436591e3e1;p=aubio.git diff --git a/src/mathutils.c b/src/mathutils.c index f82549a6..7d2424e0 100644 --- a/src/mathutils.c +++ b/src/mathutils.c @@ -1,31 +1,86 @@ /* - Copyright (C) 2003 Paul Brossier + Copyright (C) 2003-2013 Paul Brossier - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This file is part of aubio. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + aubio is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + aubio is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with aubio. If not, see . */ /* see in mathutils.h for doc */ #include "aubio_priv.h" -#include "sample.h" +#include "fvec.h" #include "mathutils.h" +#include "musicutils.h" #include "config.h" -void aubio_window(smpl_t *w, uint_t size, aubio_window_type wintype) { - uint_t i; + +/** Window types */ +typedef enum +{ + aubio_win_rectangle, + aubio_win_hamming, + aubio_win_hanning, + aubio_win_hanningz, + aubio_win_blackman, + aubio_win_blackman_harris, + aubio_win_gaussian, + aubio_win_welch, + aubio_win_parzen, + aubio_win_default = aubio_win_hanningz, +} aubio_window_type; + +fvec_t * +new_aubio_window (char_t * window_type, uint_t length) +{ + fvec_t * win = new_fvec (length); + fvec_set_window (win, window_type); + return win; +} + +uint_t fvec_set_window (fvec_t *win, char_t *window_type) { + smpl_t * w = win->data; + uint_t i, size = win->length; + aubio_window_type wintype; + if (window_type == NULL) { + AUBIO_ERR ("window type can not be null.\n"); + return 1; + } else if (strcmp (window_type, "rectangle") == 0) + wintype = aubio_win_rectangle; + else if (strcmp (window_type, "hamming") == 0) + wintype = aubio_win_hamming; + else if (strcmp (window_type, "hanning") == 0) + wintype = aubio_win_hanning; + else if (strcmp (window_type, "hanningz") == 0) + wintype = aubio_win_hanningz; + else if (strcmp (window_type, "blackman") == 0) + wintype = aubio_win_blackman; + else if (strcmp (window_type, "blackman_harris") == 0) + wintype = aubio_win_blackman_harris; + else if (strcmp (window_type, "gaussian") == 0) + wintype = aubio_win_gaussian; + else if (strcmp (window_type, "welch") == 0) + wintype = aubio_win_welch; + else if (strcmp (window_type, "parzen") == 0) + wintype = aubio_win_parzen; + else if (strcmp (window_type, "default") == 0) + wintype = aubio_win_default; + else { + AUBIO_ERR ("unknown window type `%s`.\n", window_type); + return 1; + } switch(wintype) { case aubio_win_rectangle: for (i=0;ichannels; i++) - for (j=0; j < s->length; j++) - tmp += s->data[i][j]; - return tmp/(smpl_t)(s->length); +smpl_t +fvec_mean (fvec_t * s) +{ + uint_t j; + smpl_t tmp = 0.0; + for (j = 0; j < s->length; j++) { + tmp += s->data[j]; + } + return tmp / (smpl_t) (s->length); } -smpl_t vec_sum(fvec_t *s) { - uint_t i,j; - smpl_t tmp = 0.0f; - for (i=0; i < s->channels; i++) - for (j=0; j < s->length; j++) - tmp += s->data[i][j]; +smpl_t +fvec_sum (fvec_t * s) +{ + uint_t j; + smpl_t tmp = 0.0; + for (j = 0; j < s->length; j++) { + tmp += s->data[j]; + } return tmp; } -smpl_t vec_max(fvec_t *s) { - uint_t i,j; - smpl_t tmp = 0.0f; - for (i=0; i < s->channels; i++) - for (j=0; j < s->length; j++) - tmp = (tmp > s->data[i][j])? tmp : s->data[i][j]; +smpl_t +fvec_max (fvec_t * s) +{ + uint_t j; + smpl_t tmp = 0.0; + for (j = 0; j < s->length; j++) { + tmp = (tmp > s->data[j]) ? tmp : s->data[j]; + } return tmp; } -smpl_t vec_min(fvec_t *s) { - uint_t i,j; - smpl_t tmp = s->data[0][0]; - for (i=0; i < s->channels; i++) - for (j=0; j < s->length; j++) - tmp = (tmp < s->data[i][j])? tmp : s->data[i][j] ; +smpl_t +fvec_min (fvec_t * s) +{ + uint_t j; + smpl_t tmp = s->data[0]; + for (j = 0; j < s->length; j++) { + tmp = (tmp < s->data[j]) ? tmp : s->data[j]; + } return tmp; } -uint_t vec_min_elem(fvec_t *s) { - uint_t i,j=0, pos=0.; - smpl_t tmp = s->data[0][0]; - for (i=0; i < s->channels; i++) - for (j=0; j < s->length; j++) { - pos = (tmp < s->data[i][j])? pos : j; - tmp = (tmp < s->data[i][j])? tmp : s->data[i][j] ; - } +uint_t +fvec_min_elem (fvec_t * s) +{ + uint_t j, pos = 0.; + smpl_t tmp = s->data[0]; + for (j = 0; j < s->length; j++) { + pos = (tmp < s->data[j]) ? pos : j; + tmp = (tmp < s->data[j]) ? tmp : s->data[j]; + } return pos; } -uint_t vec_max_elem(fvec_t *s) { - uint_t i,j=0, pos=0.; - smpl_t tmp = 0.0f; - for (i=0; i < s->channels; i++) - for (j=0; j < s->length; j++) { - pos = (tmp > s->data[i][j])? pos : j; - tmp = (tmp > s->data[i][j])? tmp : s->data[i][j] ; - } +uint_t +fvec_max_elem (fvec_t * s) +{ + uint_t j, pos = 0; + smpl_t tmp = 0.0; + for (j = 0; j < s->length; j++) { + pos = (tmp > s->data[j]) ? pos : j; + tmp = (tmp > s->data[j]) ? tmp : s->data[j]; + } return pos; } -void vec_shift(fvec_t *s) { - uint_t i,j; - //smpl_t tmp = 0.0f; - for (i=0; i < s->channels; i++) - for (j=0; j < s->length / 2 ; j++) { - //tmp = s->data[i][j]; - //s->data[i][j] = s->data[i][j+s->length/2]; - //s->data[i][j+s->length/2] = tmp; - ELEM_SWAP(s->data[i][j],s->data[i][j+s->length/2]); - } +void +fvec_shift (fvec_t * s) +{ + uint_t j; + for (j = 0; j < s->length / 2; j++) { + ELEM_SWAP (s->data[j], s->data[j + s->length / 2]); + } +} + +smpl_t +fvec_local_energy (fvec_t * f) +{ + smpl_t energy = 0.; + uint_t j; + for (j = 0; j < f->length; j++) { + energy += SQR (f->data[j]); + } + return energy / f->length; } -smpl_t vec_local_energy(fvec_t * f) { - smpl_t locE = 0.; - uint_t i,j; - for (i=0;ichannels;i++) - for (j=0;jlength;j++) - locE+=SQR(f->data[i][j]); - return locE; +smpl_t +fvec_local_hfc (fvec_t * v) +{ + smpl_t hfc = 0.; + uint_t j; + for (j = 0; j < v->length; j++) { + hfc += (j + 1) * v->data[j]; + } + return hfc; } -smpl_t vec_local_hfc(fvec_t * f) { - smpl_t locE = 0.; - uint_t i,j; - for (i=0;ichannels;i++) - for (j=0;jlength;j++) - locE+=(i+1)*f->data[i][j]; - return locE; +void +fvec_min_removal (fvec_t * v) +{ + smpl_t v_min = fvec_min (v); + fvec_add (v, - v_min ); } -smpl_t vec_alpha_norm(fvec_t * DF, smpl_t alpha) { +smpl_t +fvec_alpha_norm (fvec_t * o, smpl_t alpha) +{ + uint_t j; smpl_t tmp = 0.; - uint_t i,j; - for (i=0;ichannels;i++) - for (j=0;jlength;j++) - tmp += POW(ABS(DF->data[i][j]),alpha); - return POW(tmp/DF->length,1./alpha); -} - -void vec_dc_removal(fvec_t * mag) { - smpl_t mini = 0.; - uint_t length = mag->length, i=0, j; - mini = vec_min(mag); - for (j=0;jdata[i][j] -= mini; - } + for (j = 0; j < o->length; j++) { + tmp += POW (ABS (o->data[j]), alpha); + } + return POW (tmp / o->length, 1. / alpha); } -void vec_alpha_normalise(fvec_t * mag, uint_t alpha) { - smpl_t alphan = 1.; - uint_t length = mag->length, i=0, j; - alphan = vec_alpha_norm(mag,alpha); - for (j=0;jdata[i][j] /= alphan; +void +fvec_alpha_normalise (fvec_t * o, smpl_t alpha) +{ + uint_t j; + smpl_t norm = fvec_alpha_norm (o, alpha); + for (j = 0; j < o->length; j++) { + o->data[j] /= norm; } } -void vec_add(fvec_t * mag, smpl_t threshold) { - uint_t length = mag->length, i=0, j; - for (j=0;jdata[i][j] += threshold; +void +fvec_add (fvec_t * o, smpl_t val) +{ + uint_t j; + for (j = 0; j < o->length; j++) { + o->data[j] += val; } } -void vec_adapt_thres(fvec_t * vec, fvec_t * tmp, +void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp, uint_t post, uint_t pre) { - uint_t length = vec->length, i=0, j; + uint_t length = vec->length, j; for (j=0;jdata[i][j] -= vec_moving_thres(vec, tmp, post, pre, j); + vec->data[j] -= fvec_moving_thres(vec, tmp, post, pre, j); } } -smpl_t vec_moving_thres(fvec_t * vec, fvec_t * tmpvec, - uint_t post, uint_t pre, uint_t pos) { - smpl_t * medar = (smpl_t *)tmpvec->data[0]; +smpl_t +fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec, + uint_t post, uint_t pre, uint_t pos) +{ uint_t k; - uint_t win_length = post+pre+1; - uint_t length = vec->length; + smpl_t *medar = (smpl_t *) tmpvec->data; + uint_t win_length = post + pre + 1; + uint_t length = vec->length; /* post part of the buffer does not exist */ - if (posdata[0][k+pos-post]; - /* the buffer is fully defined */ - } else if (pos+predata[0][k+pos-post]; - /* pre part of the buffer does not exist */ + if (pos < post + 1) { + for (k = 0; k < post + 1 - pos; k++) + medar[k] = 0.; /* 0-padding at the beginning */ + for (k = post + 1 - pos; k < win_length; k++) + medar[k] = vec->data[k + pos - post]; + /* the buffer is fully defined */ + } else if (pos + pre < length) { + for (k = 0; k < win_length; k++) + medar[k] = vec->data[k + pos - post]; + /* pre part of the buffer does not exist */ } else { - for (k=0;kdata[0][k+pos-post]; - for (k=length-pos+post+1;kdata[k + pos - post]; + for (k = length - pos + post; k < win_length; k++) + medar[k] = 0.; /* 0-padding at the end */ } - return vec_median(tmpvec); + return fvec_median (tmpvec); } -smpl_t vec_median(fvec_t * input) { +smpl_t fvec_median (fvec_t * input) { uint_t n = input->length; - smpl_t * arr = (smpl_t *) input->data[0]; + smpl_t * arr = (smpl_t *) input->data; uint_t low, high ; uint_t median; uint_t middle, ll, hh; @@ -285,186 +367,185 @@ smpl_t vec_median(fvec_t * input) { } } -smpl_t vec_quadint(fvec_t * x,uint_t pos) { - uint_t span = 2; - smpl_t step = 1./200.; - /* hack : init resold to - something (in case x[pos+-span]<0)) */ - smpl_t res, frac, s0, s1, s2, exactpos = (smpl_t)pos, resold = -1000.; - if ((pos > span) && (pos < x->length-span)) { - s0 = x->data[0][pos-span]; - s1 = x->data[0][pos] ; - s2 = x->data[0][pos+span]; - /* increase frac */ - for (frac = 0.; frac < 2.; frac = frac + step) { - res = aubio_quadfrac(s0, s1, s2, frac); - if (res > resold) - resold = res; - else { - exactpos += (frac-step)*2. - 1.; - break; - } - } - } - return exactpos; -} - -smpl_t vec_quadint_min(fvec_t * x,uint_t pos, uint_t span) { - smpl_t step = 1./200.; - /* init resold to - something (in case x[pos+-span]<0)) */ - smpl_t res, frac, s0, s1, s2, exactpos = (smpl_t)pos, resold = 100000.; - if ((pos > span) && (pos < x->length-span)) { - s0 = x->data[0][pos-span]; - s1 = x->data[0][pos] ; - s2 = x->data[0][pos+span]; - /* increase frac */ - for (frac = 0.; frac < 2.; frac = frac + step) { - res = aubio_quadfrac(s0, s1, s2, frac); - if (res < resold) { - resold = res; - } else { - exactpos += (frac-step)*span - span/2.; - break; - } - } - } - return exactpos; -} - -smpl_t aubio_quadfrac(smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf) { - smpl_t tmp = s0 + (pf/2.) * (pf * ( s0 - 2.*s1 + s2 ) - 3.*s0 + 4.*s1 - s2); +smpl_t fvec_quadint (fvec_t * x, uint_t pos) { + smpl_t s0, s1, s2; + uint_t x0 = (pos < 1) ? pos : pos - 1; + uint_t x2 = (pos + 1 < x->length) ? pos + 1 : pos; + if (x0 == pos) return (x->data[pos] <= x->data[x2]) ? pos : x2; + if (x2 == pos) return (x->data[pos] <= x->data[x0]) ? pos : x0; + s0 = x->data[x0]; + s1 = x->data[pos]; + s2 = x->data[x2]; + return pos + 0.5 * (s2 - s0 ) / (s2 - 2.* s1 + s0); +} + +smpl_t fvec_quadratic_peak_pos (fvec_t * x, uint_t pos) { + smpl_t s0, s1, s2; + uint_t x0 = (pos < 1) ? pos : pos - 1; + uint_t x2 = (pos + 1 < x->length) ? pos + 1 : pos; + if (x0 == pos) return (x->data[pos] <= x->data[x2]) ? pos : x2; + if (x2 == pos) return (x->data[pos] <= x->data[x0]) ? pos : x0; + s0 = x->data[x0]; + s1 = x->data[pos]; + s2 = x->data[x2]; + return pos + 0.5 * (s0 - s2 ) / (s0 - 2.* s1 + s2); +} + +uint_t fvec_peakpick(fvec_t * onset, uint_t pos) { + uint_t tmp=0; + tmp = (onset->data[pos] > onset->data[pos-1] + && onset->data[pos] > onset->data[pos+1] + && onset->data[pos] > 0.); return tmp; } -uint_t vec_peakpick(fvec_t * onset, uint_t pos) { - uint_t i=0, tmp=0; - /*for (i=0;ichannels;i++)*/ - tmp = (onset->data[i][pos] > onset->data[i][pos-1] - && onset->data[i][pos] > onset->data[i][pos+1] - && onset->data[i][pos] > 0.); +smpl_t +aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf) +{ + smpl_t tmp = + s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2); return tmp; } -smpl_t aubio_freqtomidi(smpl_t freq) { +smpl_t +aubio_freqtomidi (smpl_t freq) +{ + if (freq < 2. || freq > 100000.) return 0.; // avoid nans and infs /* log(freq/A-2)/log(2) */ - smpl_t midi = freq/6.875; - midi = LOG(midi)/0.69314718055995; + smpl_t midi = freq / 6.875; + midi = LOG (midi) / 0.69314718055995; midi *= 12; midi -= 3; return midi; } -smpl_t aubio_miditofreq(smpl_t midi) { - smpl_t freq = (midi+3.)/12.; - freq = EXP(freq*0.69314718055995); +smpl_t +aubio_miditofreq (smpl_t midi) +{ + if (midi > 140.) return 0.; // avoid infs + smpl_t freq = (midi + 3.) / 12.; + freq = EXP (freq * 0.69314718055995); freq *= 6.875; return freq; } -smpl_t aubio_bintofreq(smpl_t bin, smpl_t samplerate, smpl_t fftsize) { - smpl_t freq = samplerate/fftsize; - return freq*bin; +smpl_t +aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize) +{ + smpl_t freq = samplerate / fftsize; + return freq * MAX(bin, 0); } -smpl_t aubio_bintomidi(smpl_t bin, smpl_t samplerate, smpl_t fftsize) { - smpl_t midi = aubio_bintofreq(bin,samplerate,fftsize); - return aubio_freqtomidi(midi); +smpl_t +aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize) +{ + smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize); + return aubio_freqtomidi (midi); } -smpl_t aubio_freqtobin(smpl_t freq, smpl_t samplerate, smpl_t fftsize) { - smpl_t bin = fftsize/samplerate; - return freq*bin; +smpl_t +aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize) +{ + smpl_t bin = fftsize / samplerate; + return MAX(freq, 0) * bin; } -smpl_t aubio_miditobin(smpl_t midi, smpl_t samplerate, smpl_t fftsize) { - smpl_t freq = aubio_miditofreq(midi); - return aubio_freqtobin(freq,samplerate,fftsize); +smpl_t +aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize) +{ + smpl_t freq = aubio_miditofreq (midi); + return aubio_freqtobin (freq, samplerate, fftsize); } -/** returns 1 if wassilence is 0 and RMS(ibuf)length;j++) { - loudness += SQR(ibuf->data[i][j]); +uint_t +aubio_is_power_of_two (uint_t a) +{ + if ((a & (a - 1)) == 0) { + return 1; + } else { + return 0; } - loudness = SQRT(loudness); - loudness /= (smpl_t)ibuf->length; - loudness = LIN2DB(loudness); +} - return (loudness < threshold); +uint_t +aubio_next_power_of_two (uint_t a) +{ + uint_t i = 1; + while (i < a) i <<= 1; + return i; } -/** returns level log(RMS(ibuf)) if < threshold, 1 otherwise - * \bug mono - */ -smpl_t aubio_level_detection(fvec_t * ibuf, smpl_t threshold) { - smpl_t loudness = 0; - uint_t i=0,j; - for (j=0;jlength;j++) { - loudness += SQR(ibuf->data[i][j]); - } - loudness = SQRT(loudness); - loudness /= (smpl_t)ibuf->length; - loudness = LIN2DB(loudness); +smpl_t +aubio_db_spl (fvec_t * o) +{ + return 10. * LOG10 (fvec_local_energy (o)); +} - if (loudness < threshold) +uint_t +aubio_silence_detection (fvec_t * o, smpl_t threshold) +{ + return (aubio_db_spl (o) < threshold); +} + +smpl_t +aubio_level_detection (fvec_t * o, smpl_t threshold) +{ + smpl_t db_spl = aubio_db_spl (o); + if (db_spl < threshold) { return 1.; - else - return loudness; + } else { + return db_spl; + } } -smpl_t aubio_zero_crossing_rate(fvec_t * input) { - uint_t i=0,j; +smpl_t +aubio_zero_crossing_rate (fvec_t * input) +{ + uint_t j; uint_t zcr = 0; - for ( j = 1; j < input->length; j++ ) { - // previous was negative - if( input->data[i][j-1] <= 0. ) { - if ( input->data[i][j] > 0. ) { + for (j = 1; j < input->length; j++) { + // previous was strictly negative + if (input->data[j - 1] < 0.) { + // current is positive or null + if (input->data[j] >= 0.) { + zcr += 1; + } + // previous was positive or null + } else { + // current is strictly negative + if (input->data[j] < 0.) { zcr += 1; } - //previous was positive - } else if ( input->data[i][j] <= 0. ) { - zcr += 1; } } - return zcr/(smpl_t)input->length; -} - -smpl_t aubio_spectral_centroid(cvec_t * spectrum, smpl_t samplerate) { - uint_t i=0, j; - smpl_t sum = 0., sc = 0.; - for ( j = 0; j < spectrum->length; j++ ) { - sum += spectrum->norm[i][j]; - } - if (sum == 0.) return 0.; - for ( j = 0; j < spectrum->length; j++ ) { - sc += (smpl_t)j * spectrum->norm[i][j]; - } - return sc / sum * samplerate / (smpl_t)(spectrum->length); -} - -void aubio_autocorr(fvec_t * input, fvec_t * output) { - uint_t i = 0, j = 0, length = input->length; - smpl_t * data = input->data[0]; - smpl_t * acf = output->data[0]; - smpl_t tmp =0.; - for(i=0;ilength; +} + +void +aubio_autocorr (fvec_t * input, fvec_t * output) +{ + uint_t i, j, length = input->length; + smpl_t *data, *acf; + smpl_t tmp = 0; + data = input->data; + acf = output->data; + for (i = 0; i < length; i++) { + tmp = 0.; + for (j = i; j < length; j++) { + tmp += data[j - i] * data[j]; } - acf[i] = tmp /(smpl_t)(length-i); - tmp = 0.0; + acf[i] = tmp / (smpl_t) (length - i); } } -void aubio_cleanup(void) { -#if FFTW3_SUPPORT - fftw_cleanup(); +void +aubio_cleanup (void) +{ +#ifdef HAVE_FFTW3F + fftwf_cleanup (); #else -#if FFTW3F_SUPPORT - fftwf_cleanup(); +#ifdef HAVE_FFTW3 + fftw_cleanup (); #endif #endif }