[96fb8ad] | 1 | /* |
---|
[a6db140] | 2 | Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org> |
---|
[96fb8ad] | 3 | |
---|
[a6db140] | 4 | This file is part of aubio. |
---|
[96fb8ad] | 5 | |
---|
[a6db140] | 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. |
---|
[96fb8ad] | 10 | |
---|
[a6db140] | 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. |
---|
| 15 | |
---|
| 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/>. |
---|
[96fb8ad] | 18 | |
---|
| 19 | */ |
---|
| 20 | |
---|
[2a2636a] | 21 | /** \file |
---|
| 22 | |
---|
| 23 | Various math functions |
---|
| 24 | |
---|
| 25 | \example test-mathutils.c |
---|
[f72364d] | 26 | \example test-mathutils-window.c |
---|
[2a2636a] | 27 | |
---|
[96fb8ad] | 28 | */ |
---|
| 29 | |
---|
| 30 | #ifndef MATHUTILS_H |
---|
| 31 | #define MATHUTILS_H |
---|
| 32 | |
---|
[83963b3] | 33 | #include "fvec.h" |
---|
| 34 | #include "musicutils.h" |
---|
| 35 | |
---|
[ce0e7b5] | 36 | #ifdef __cplusplus |
---|
| 37 | extern "C" { |
---|
| 38 | #endif |
---|
| 39 | |
---|
[eb7f743] | 40 | /** compute the mean of a vector |
---|
[ce0e7b5] | 41 | |
---|
[56ef7e1] | 42 | \param s vector to compute mean from |
---|
[eb7f743] | 43 | |
---|
| 44 | \return the mean of v |
---|
| 45 | |
---|
| 46 | */ |
---|
| 47 | smpl_t fvec_mean (fvec_t * s); |
---|
| 48 | |
---|
| 49 | /** find the max of a vector |
---|
| 50 | |
---|
| 51 | \param s vector to get the max from |
---|
| 52 | |
---|
| 53 | \return the value of the minimum of v |
---|
| 54 | |
---|
| 55 | */ |
---|
| 56 | smpl_t fvec_max (fvec_t * s); |
---|
| 57 | |
---|
| 58 | /** find the min of a vector |
---|
| 59 | |
---|
| 60 | \param s vector to get the min from |
---|
| 61 | |
---|
| 62 | \return the value of the maximum of v |
---|
| 63 | |
---|
| 64 | */ |
---|
| 65 | smpl_t fvec_min (fvec_t * s); |
---|
| 66 | |
---|
| 67 | /** find the index of the min of a vector |
---|
| 68 | |
---|
| 69 | \param s vector to get the index from |
---|
| 70 | |
---|
| 71 | \return the index of the minimum element of v |
---|
| 72 | |
---|
| 73 | */ |
---|
| 74 | uint_t fvec_min_elem (fvec_t * s); |
---|
| 75 | |
---|
| 76 | /** find the index of the max of a vector |
---|
| 77 | |
---|
| 78 | \param s vector to get the index from |
---|
| 79 | |
---|
| 80 | \return the index of the maximum element of v |
---|
| 81 | |
---|
| 82 | */ |
---|
| 83 | uint_t fvec_max_elem (fvec_t * s); |
---|
| 84 | |
---|
| 85 | /** swap the left and right halves of a vector |
---|
| 86 | |
---|
| 87 | This function swaps the left part of the signal with the right part of the |
---|
| 88 | signal. Therefore |
---|
| 89 | |
---|
| 90 | \f$ a[0], a[1], ..., a[\frac{N}{2}], a[\frac{N}{2}+1], ..., a[N-1], a[N] \f$ |
---|
| 91 | |
---|
| 92 | becomes |
---|
| 93 | |
---|
| 94 | \f$ a[\frac{N}{2}+1], ..., a[N-1], a[N], a[0], a[1], ..., a[\frac{N}{2}] \f$ |
---|
| 95 | |
---|
| 96 | This operation, known as 'fftshift' in the Matlab Signal Processing Toolbox, |
---|
| 97 | can be used before computing the FFT to simplify the phase relationship of the |
---|
| 98 | resulting spectrum. See Amalia de Götzen's paper referred to above. |
---|
| 99 | |
---|
| 100 | */ |
---|
| 101 | void fvec_shift (fvec_t * v); |
---|
| 102 | |
---|
| 103 | /** compute the sum of all elements of a vector |
---|
| 104 | |
---|
| 105 | \param v vector to compute the sum of |
---|
| 106 | |
---|
| 107 | \return the sum of v |
---|
| 108 | |
---|
| 109 | */ |
---|
| 110 | smpl_t fvec_sum (fvec_t * v); |
---|
| 111 | |
---|
| 112 | /** compute the energy of a vector |
---|
| 113 | |
---|
[1a74ac3] | 114 | This function compute the sum of the squared elements of a vector, normalised |
---|
| 115 | by its length. |
---|
[eb7f743] | 116 | |
---|
| 117 | \param v vector to get the energy from |
---|
| 118 | |
---|
| 119 | \return the energy of v |
---|
| 120 | |
---|
| 121 | */ |
---|
| 122 | smpl_t fvec_local_energy (fvec_t * v); |
---|
| 123 | |
---|
| 124 | /** compute the High Frequency Content of a vector |
---|
| 125 | |
---|
| 126 | The High Frequency Content is defined as \f$ \sum_0^{N-1} (k+1) v[k] \f$. |
---|
| 127 | |
---|
| 128 | \param v vector to get the energy from |
---|
| 129 | |
---|
| 130 | \return the HFC of v |
---|
| 131 | |
---|
| 132 | */ |
---|
| 133 | smpl_t fvec_local_hfc (fvec_t * v); |
---|
| 134 | |
---|
| 135 | /** computes the p-norm of a vector |
---|
| 136 | |
---|
| 137 | Computes the p-norm of a vector for \f$ p = \alpha \f$ |
---|
| 138 | |
---|
| 139 | \f$ L^p = ||x||_p = (|x_1|^p + |x_2|^p + ... + |x_n|^p ) ^ \frac{1}{p} \f$ |
---|
| 140 | |
---|
| 141 | If p = 1, the result is the Manhattan distance. |
---|
| 142 | |
---|
| 143 | If p = 2, the result is the Euclidean distance. |
---|
| 144 | |
---|
| 145 | As p tends towards large values, \f$ L^p \f$ tends towards the maximum of the |
---|
| 146 | input vector. |
---|
| 147 | |
---|
| 148 | References: |
---|
| 149 | |
---|
| 150 | - <a href="http://en.wikipedia.org/wiki/Lp_space">\f$L^p\f$ space</a> on |
---|
| 151 | Wikipedia |
---|
| 152 | |
---|
| 153 | \param v vector to compute norm from |
---|
| 154 | \param p order of the computed norm |
---|
| 155 | |
---|
| 156 | \return the p-norm of v |
---|
| 157 | |
---|
| 158 | */ |
---|
| 159 | smpl_t fvec_alpha_norm (fvec_t * v, smpl_t p); |
---|
| 160 | |
---|
| 161 | /** alpha normalisation |
---|
| 162 | |
---|
| 163 | This function divides all elements of a vector by the p-norm as computed by |
---|
| 164 | fvec_alpha_norm(). |
---|
| 165 | |
---|
| 166 | \param v vector to compute norm from |
---|
| 167 | \param p order of the computed norm |
---|
| 168 | |
---|
| 169 | */ |
---|
| 170 | void fvec_alpha_normalise (fvec_t * v, smpl_t p); |
---|
| 171 | |
---|
| 172 | /** add a constant to each elements of a vector |
---|
| 173 | |
---|
| 174 | \param v vector to add constant to |
---|
| 175 | \param c constant to add to v |
---|
| 176 | |
---|
| 177 | */ |
---|
| 178 | void fvec_add (fvec_t * v, smpl_t c); |
---|
| 179 | |
---|
| 180 | /** remove the minimum value of the vector to each elements |
---|
| 181 | |
---|
| 182 | \param v vector to remove minimum from |
---|
| 183 | |
---|
| 184 | */ |
---|
| 185 | void fvec_min_removal (fvec_t * v); |
---|
| 186 | |
---|
[2a2636a] | 187 | /** compute moving median threshold of a vector |
---|
[eb7f743] | 188 | |
---|
| 189 | This function computes the moving median threshold value of at the given |
---|
[2a2636a] | 190 | position of a vector, taking the median among post elements before and up to |
---|
[eb7f743] | 191 | pre elements after pos. |
---|
| 192 | |
---|
| 193 | \param v input vector |
---|
| 194 | \param tmp temporary vector of length post+1+pre |
---|
| 195 | \param post length of causal part to take before pos |
---|
| 196 | \param pre length of anti-causal part to take after pos |
---|
| 197 | \param pos index to compute threshold for |
---|
| 198 | |
---|
| 199 | \return moving median threshold value |
---|
| 200 | |
---|
| 201 | */ |
---|
| 202 | smpl_t fvec_moving_thres (fvec_t * v, fvec_t * tmp, uint_t post, uint_t pre, |
---|
[8e5c051] | 203 | uint_t pos); |
---|
[eb7f743] | 204 | |
---|
| 205 | /** apply adaptive threshold to a vector |
---|
| 206 | |
---|
| 207 | For each points at position p of an input vector, this function remove the |
---|
| 208 | moving median threshold computed at p. |
---|
| 209 | |
---|
| 210 | \param v input vector |
---|
| 211 | \param tmp temporary vector of length post+1+pre |
---|
| 212 | \param post length of causal part to take before pos |
---|
| 213 | \param pre length of anti-causal part to take after pos |
---|
| 214 | |
---|
| 215 | */ |
---|
[8e5c051] | 216 | void fvec_adapt_thres (fvec_t * v, fvec_t * tmp, uint_t post, uint_t pre); |
---|
[eb7f743] | 217 | |
---|
| 218 | /** returns the median of a vector |
---|
| 219 | |
---|
| 220 | The QuickSelect routine is based on the algorithm described in "Numerical |
---|
| 221 | recipes in C", Second Edition, Cambridge University Press, 1992, Section 8.5, |
---|
| 222 | ISBN 0-521-43108-5 |
---|
| 223 | |
---|
| 224 | This implementation of the QuickSelect routine is based on Nicolas |
---|
| 225 | Devillard's implementation, available at http://ndevilla.free.fr/median/median/ |
---|
| 226 | and in the Public Domain. |
---|
| 227 | |
---|
| 228 | \param v vector to get median from |
---|
| 229 | |
---|
| 230 | \return the median of v |
---|
| 231 | |
---|
| 232 | */ |
---|
[8e5c051] | 233 | smpl_t fvec_median (fvec_t * v); |
---|
[96fb8ad] | 234 | |
---|
[c5c0c98] | 235 | /** finds exact peak index by quadratic interpolation*/ |
---|
[8e5c051] | 236 | smpl_t fvec_quadint (fvec_t * x, uint_t pos); |
---|
[9771488] | 237 | |
---|
[96fb8ad] | 238 | /** Quadratic interpolation using Lagrange polynomial. |
---|
[eb7f743] | 239 | |
---|
| 240 | Inspired from ``Comparison of interpolation algorithms in real-time sound |
---|
| 241 | processing'', Vladimir Arnost, |
---|
| 242 | |
---|
| 243 | \param s0,s1,s2 are 3 consecutive samples of a curve |
---|
| 244 | \param pf is the floating point index [0;2] |
---|
| 245 | |
---|
| 246 | \return s0 + (pf/2.)*((pf-3.)*s0-2.*(pf-2.)*s1+(pf-1.)*s2); |
---|
| 247 | |
---|
| 248 | */ |
---|
| 249 | smpl_t aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf); |
---|
[96fb8ad] | 250 | |
---|
[eb7f743] | 251 | /** return 1 if v[p] is a peak and positive, 0 otherwise |
---|
| 252 | |
---|
| 253 | This function returns 1 if a peak is found at index p in the vector v. The |
---|
| 254 | peak is defined as follows: |
---|
| 255 | |
---|
| 256 | - v[p] is positive |
---|
| 257 | - v[p-1] < v[p] |
---|
| 258 | - v[p] > v[p+1] |
---|
| 259 | |
---|
| 260 | \param v input vector |
---|
| 261 | \param p position of supposed for peak |
---|
| 262 | |
---|
| 263 | \return 1 if a peak is found, 0 otherwise |
---|
| 264 | |
---|
| 265 | */ |
---|
| 266 | uint_t fvec_peakpick (fvec_t * v, uint_t p); |
---|
[96fb8ad] | 267 | |
---|
[10a5413] | 268 | /** return 1 if a is a power of 2, 0 otherwise */ |
---|
| 269 | uint_t aubio_is_power_of_two(uint_t a); |
---|
| 270 | |
---|
| 271 | /** return the next power of power of 2 greater than a */ |
---|
| 272 | uint_t aubio_next_power_of_two(uint_t a); |
---|
| 273 | |
---|
[eb7f743] | 274 | /** compute normalised autocorrelation function |
---|
| 275 | |
---|
| 276 | \param input vector to compute autocorrelation from |
---|
| 277 | \param output vector to store autocorrelation function to |
---|
| 278 | |
---|
| 279 | */ |
---|
| 280 | void aubio_autocorr (fvec_t * input, fvec_t * output); |
---|
| 281 | |
---|
[96fb8ad] | 282 | #ifdef __cplusplus |
---|
| 283 | } |
---|
| 284 | #endif |
---|
| 285 | |
---|
| 286 | #endif |
---|
| 287 | |
---|