andrew@0: /* andrew@0: Copyright (C) 2003 Paul Brossier andrew@0: andrew@0: This program is free software; you can redistribute it and/or modify andrew@0: it under the terms of the GNU General Public License as published by andrew@0: the Free Software Foundation; either version 2 of the License, or andrew@0: (at your option) any later version. andrew@0: andrew@0: This program is distributed in the hope that it will be useful, andrew@0: but WITHOUT ANY WARRANTY; without even the implied warranty of andrew@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrew@0: GNU General Public License for more details. andrew@0: andrew@0: You should have received a copy of the GNU General Public License andrew@0: along with this program; if not, write to the Free Software andrew@0: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. andrew@0: andrew@0: */ andrew@0: andrew@0: /** @file andrew@0: * various math functions andrew@0: * andrew@0: * \todo multichannel (each function should return -or set- an array sized to andrew@0: * the number of channel in the input vector) andrew@0: * andrew@0: * \todo appropriate switches depending on types.h content andrew@0: */ andrew@0: andrew@0: #ifndef MATHUTILS_H andrew@0: #define MATHUTILS_H andrew@0: andrew@0: /** Window types andrew@0: * andrew@0: * inspired from andrew@0: * andrew@0: * - dafx : http://profs.sci.univr.it/%7Edafx/Final-Papers/ps/Bernardini.ps.gz andrew@0: * - freqtweak : http://freqtweak.sf.net/ andrew@0: * - extace : http://extace.sf.net/ andrew@0: */ andrew@0: andrew@0: #ifdef __cplusplus andrew@0: extern "C" { andrew@0: #endif andrew@0: andrew@0: typedef enum { andrew@0: aubio_win_rectangle, andrew@0: aubio_win_hamming, andrew@0: aubio_win_hanning, andrew@0: aubio_win_hanningz, andrew@0: aubio_win_blackman, andrew@0: aubio_win_blackman_harris, andrew@0: aubio_win_gaussian, andrew@0: aubio_win_welch, andrew@0: aubio_win_parzen andrew@0: } aubio_window_type; andrew@0: andrew@0: /** create window */ andrew@0: void aubio_window(smpl_t *w, ba_uint_t size, aubio_window_type wintype); andrew@0: andrew@0: /** principal argument andrew@0: * andrew@0: * mod(phase+PI,-TWO_PI)+PI andrew@0: */ andrew@0: smpl_t aubio_unwrap2pi (smpl_t phase); andrew@0: andrew@0: /** calculates the mean of a vector andrew@0: * andrew@0: * \bug mono andrew@0: */ andrew@0: smpl_t vec_mean(fvec_t *s); andrew@0: /** returns the max of a vector andrew@0: * andrew@0: * \bug mono andrew@0: */ andrew@0: smpl_t vec_max(fvec_t *s); andrew@0: /** returns the min of a vector andrew@0: * andrew@0: * \bug mono andrew@0: */ andrew@0: smpl_t vec_min(fvec_t *s); andrew@0: /** returns the index of the min of a vector andrew@0: * andrew@0: * \bug mono andrew@0: */ andrew@0: ba_uint_t vec_min_elem(fvec_t *s); andrew@0: /** returns the index of the max of a vector andrew@0: * andrew@0: * \bug mono andrew@0: */ andrew@0: ba_uint_t vec_max_elem(fvec_t *s); andrew@0: /** implement 'fftshift' like function andrew@0: * andrew@0: * a[0]...,a[n/2],a[n/2+1],...a[n] andrew@0: * andrew@0: * becomes andrew@0: * andrew@0: * a[n/2+1],...a[n],a[0]...,a[n/2] andrew@0: */ andrew@0: void vec_shift(fvec_t *s); andrew@0: /** returns sum */ andrew@0: smpl_t vec_sum(fvec_t *s); andrew@0: /** returns energy andrew@0: * andrew@0: * \bug mono andrew@0: */ andrew@0: smpl_t vec_local_energy(fvec_t * f); andrew@0: /** returns High Frequency Energy Content andrew@0: * andrew@0: * \bug mono */ andrew@0: smpl_t vec_local_hfc(fvec_t * f); andrew@0: /** return alpha norm. andrew@0: * andrew@0: * alpha=2 means normalise variance. andrew@0: * alpha=1 means normalise abs value. andrew@0: * as alpha goes large, tends to normalisation andrew@0: * by max value. andrew@0: * andrew@0: * \bug should not use POW :( andrew@0: */ andrew@0: smpl_t vec_alpha_norm(fvec_t * DF, smpl_t alpha); andrew@0: /** dc(min) removal */ andrew@0: void vec_dc_removal(fvec_t * mag); andrew@0: /** alpha normalisation */ andrew@0: void vec_alpha_normalise(fvec_t * mag, ba_uint_t alpha); andrew@0: /** add a constant to all members of a vector */ andrew@0: void vec_add(fvec_t * mag, smpl_t threshold); andrew@0: andrew@0: /** compute adaptive threshold of input vector */ andrew@0: void vec_adapt_thres(fvec_t * vec, fvec_t * tmp, andrew@0: ba_uint_t win_post, ba_uint_t win_pre); andrew@0: /** adaptative thresholding andrew@0: * andrew@0: * y=fn_thresh(fn,x,post,pre) andrew@0: * compute adaptive threshold at each time andrew@0: * fn : a function name or pointer, eg 'median' andrew@0: * x: signal vector andrew@0: * post: window length, causal part andrew@0: * pre: window length, anti-causal part andrew@0: * Returns: andrew@0: * y: signal the same length as x andrew@0: * andrew@0: * Formerly median_thresh, used compute median over a andrew@0: * window of post+pre+1 samples, but now works with any andrew@0: * function that takes a vector or matrix and returns a andrew@0: * 'representative' value for each column, eg andrew@0: * medians=fn_thresh(median,x,8,8) andrew@0: * minima=fn_thresh(min,x,8,8) andrew@0: * see SPARMS for explanation of post and pre andrew@0: */ andrew@0: smpl_t vec_moving_thres(fvec_t * vec, fvec_t * tmp, andrew@0: ba_uint_t win_post, ba_uint_t win_pre, ba_uint_t win_pos); andrew@0: andrew@0: /** returns the median of the vector andrew@0: * andrew@0: * This Quickselect routine is based on the algorithm described in andrew@0: * "Numerical recipes in C", Second Edition, andrew@0: * Cambridge University Press, 1992, Section 8.5, ISBN 0-521-43108-5 andrew@0: * andrew@0: * This code by Nicolas Devillard - 1998. Public domain, andrew@0: * available at http://ndevilla.free.fr/median/median/ andrew@0: */ andrew@0: smpl_t vec_median(fvec_t * input); andrew@0: andrew@0: /** finds exact maximum position by quadratic interpolation*/ andrew@0: smpl_t vec_quadint(fvec_t * x,ba_uint_t pos); andrew@0: andrew@0: /** finds exact minimum position by quadratic interpolation*/ andrew@0: smpl_t vec_quadint_min(fvec_t * x,ba_uint_t pos, ba_uint_t span); andrew@0: andrew@0: /** Quadratic interpolation using Lagrange polynomial. andrew@0: * andrew@0: * inspired from ``Comparison of interpolation algorithms in real-time sound andrew@0: * processing'', Vladimir Arnost, andrew@0: * andrew@0: * estimate = s0 + (pf/2.)*((pf-3.)*s0-2.*(pf-2.)*s1+(pf-1.)*s2); andrew@0: * where andrew@0: * \param s0,s1,s2 are 3 known points on the curve, andrew@0: * \param pf is the floating point index [0;2] andrew@0: */ andrew@0: smpl_t aubio_quadfrac(smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf); andrew@0: andrew@0: /** returns 1 if X1 is a peak and positive */ andrew@0: ba_uint_t vec_peakpick(fvec_t * input, ba_uint_t pos); andrew@0: andrew@0: /** convert frequency bin to midi value */ andrew@0: smpl_t aubio_bintomidi(smpl_t bin, smpl_t samplerate, smpl_t fftsize); andrew@0: /** convert midi value to frequency bin */ andrew@0: smpl_t aubio_miditobin(smpl_t midi, smpl_t samplerate, smpl_t fftsize); andrew@0: /** convert frequency bin to frequency (Hz) */ andrew@0: smpl_t aubio_bintofreq(smpl_t bin, smpl_t samplerate, smpl_t fftsize); andrew@0: /** convert frequency (Hz) to frequency bin */ andrew@0: smpl_t aubio_freqtobin(smpl_t freq, smpl_t samplerate, smpl_t fftsize); andrew@0: /** convert frequency (Hz) to midi value (0-128) */ andrew@0: smpl_t aubio_freqtomidi(smpl_t freq); andrew@0: /** convert midi value (0-128) to frequency (Hz) */ andrew@0: smpl_t aubio_miditofreq(smpl_t midi); andrew@0: andrew@0: /** check if current buffer level is under a given threshold */ andrew@0: ba_uint_t aubio_silence_detection(fvec_t * ibuf, smpl_t threshold); andrew@0: /** get the current buffer level */ andrew@0: smpl_t aubio_level_detection(fvec_t * ibuf, smpl_t threshold); andrew@0: /** andrew@0: * calculate normalised autocorrelation function andrew@0: */ andrew@0: void aubio_autocorr(fvec_t * input, fvec_t * output); andrew@0: /** andrew@0: * clean up cached memory at the end of program andrew@0: * andrew@0: * use this function at the end of programs to purge all andrew@0: * cached memory. so far this function is only used to clean andrew@0: * fftw cache. andrew@0: */ andrew@0: void aubio_cleanup(void); andrew@0: andrew@0: #ifdef __cplusplus andrew@0: } andrew@0: #endif andrew@0: andrew@0: #endif andrew@0: