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