c@119: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@119: /* c@119: Constant-Q library c@119: Copyright (c) 2013-2014 Queen Mary, University of London c@119: c@119: Permission is hereby granted, free of charge, to any person c@119: obtaining a copy of this software and associated documentation c@119: files (the "Software"), to deal in the Software without c@119: restriction, including without limitation the rights to use, copy, c@119: modify, merge, publish, distribute, sublicense, and/or sell copies c@119: of the Software, and to permit persons to whom the Software is c@119: furnished to do so, subject to the following conditions: c@119: c@119: The above copyright notice and this permission notice shall be c@119: included in all copies or substantial portions of the Software. c@119: c@119: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, c@119: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF c@119: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND c@119: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY c@119: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF c@119: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION c@119: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. c@119: c@119: Except as contained in this notice, the names of the Centre for c@119: Digital Music; Queen Mary, University of London; and Chris Cannam c@119: shall not be used in advertising or otherwise to promote the sale, c@119: use or other dealings in this Software without prior written c@119: authorization. c@119: */ c@119: c@119: #ifndef MATHUTILITIES_H c@119: #define MATHUTILITIES_H c@119: c@119: #include c@119: c@119: #include "nan-inf.h" c@179: #include "pi.h" c@119: c@119: /** c@119: * Static helper functions for simple mathematical calculations. c@119: */ c@119: class MathUtilities c@119: { c@119: public: c@119: /** c@119: * Round x to the nearest integer. c@119: */ c@119: static double round( double x ); c@119: c@119: /** c@119: * Return through min and max pointers the highest and lowest c@119: * values in the given array of the given length. c@119: */ c@119: static void getFrameMinMax( const double* data, unsigned int len, double* min, double* max ); c@119: c@119: /** c@119: * Return the mean of the given array of the given length. c@119: */ c@119: static double mean( const double* src, unsigned int len ); c@119: c@119: /** c@119: * Return the mean of the subset of the given vector identified by c@119: * start and count. c@119: */ c@119: static double mean( const std::vector &data, c@119: unsigned int start, unsigned int count ); c@119: c@119: /** c@119: * Return the sum of the values in the given array of the given c@119: * length. c@119: */ c@119: static double sum( const double* src, unsigned int len ); c@119: c@119: /** c@119: * Return the median of the values in the given array of the given c@119: * length. If the array is even in length, the returned value will c@119: * be half-way between the two values adjacent to median. c@119: */ c@119: static double median( const double* src, unsigned int len ); c@119: c@119: /** c@119: * The principle argument function. Map the phase angle ang into c@119: * the range [-pi,pi). c@119: */ c@119: static double princarg( double ang ); c@119: c@119: /** c@119: * Floating-point division modulus: return x % y. c@119: */ c@119: static double mod( double x, double y); c@119: c@119: static void getAlphaNorm(const double *data, unsigned int len, unsigned int alpha, double* ANorm); c@119: static double getAlphaNorm(const std::vector &data, unsigned int alpha ); c@119: c@119: static void circShift( double* data, int length, int shift); c@119: c@119: static int getMax( double* data, unsigned int length, double* max = 0 ); c@119: static int getMax( const std::vector &data, double* max = 0 ); c@119: static int compareInt(const void * a, const void * b); c@119: c@119: enum NormaliseType { c@119: NormaliseNone, c@119: NormaliseUnitSum, c@119: NormaliseUnitMax c@119: }; c@119: c@119: static void normalise(double *data, int length, c@119: NormaliseType n = NormaliseUnitMax); c@119: c@119: static void normalise(std::vector &data, c@119: NormaliseType n = NormaliseUnitMax); c@119: c@119: /** c@119: * Threshold the input/output vector data against a moving-mean c@119: * average filter. c@119: */ c@119: static void adaptiveThreshold(std::vector &data); c@119: c@119: /** c@119: * Return true if x is 2^n for some integer n >= 0. c@119: */ c@119: static bool isPowerOfTwo(int x); c@119: c@119: /** c@119: * Return the next higher integer power of two from x, e.g. 1300 c@119: * -> 2048, 2048 -> 2048. c@119: */ c@119: static int nextPowerOfTwo(int x); c@119: c@119: /** c@119: * Return the next lower integer power of two from x, e.g. 1300 -> c@119: * 1024, 2048 -> 2048. c@119: */ c@119: static int previousPowerOfTwo(int x); c@119: c@119: /** c@119: * Return the nearest integer power of two to x, e.g. 1300 -> 1024, c@119: * 12 -> 16 (not 8; if two are equidistant, the higher is returned). c@119: */ c@119: static int nearestPowerOfTwo(int x); c@119: c@119: /** c@119: * Return x! c@119: */ c@119: static double factorial(int x); // returns double in case it is large c@119: c@119: /** c@119: * Return the greatest common divisor of natural numbers a and b. c@119: */ c@119: static int gcd(int a, int b); c@119: }; c@119: c@119: #endif