cannam@0: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ cannam@0: cannam@0: /* cannam@0: QM DSP Library cannam@0: cannam@0: Centre for Digital Music, Queen Mary, University of London. Chris@84: This file 2005-2006 Christian Landone. Chris@84: Chris@84: This program is free software; you can redistribute it and/or Chris@84: modify it under the terms of the GNU General Public License as Chris@84: published by the Free Software Foundation; either version 2 of the Chris@84: License, or (at your option) any later version. See the file Chris@84: COPYING included with this distribution for more information. cannam@0: */ cannam@0: cannam@0: #ifndef MATHUTILITIES_H cannam@0: #define MATHUTILITIES_H cannam@0: cannam@0: #include cannam@0: cannam@79: #include "nan-inf.h" cannam@79: Chris@152: /** Chris@152: * Static helper functions for simple mathematical calculations. Chris@152: */ cannam@0: class MathUtilities cannam@0: { cannam@0: public: Chris@152: /** Chris@152: * Round x to the nearest integer. Chris@152: */ cannam@0: static double round( double x ); cannam@34: Chris@152: /** Chris@152: * Return through min and max pointers the highest and lowest Chris@152: * values in the given array of the given length. Chris@152: */ cannam@0: static void getFrameMinMax( const double* data, unsigned int len, double* min, double* max ); cannam@34: Chris@152: /** Chris@152: * Return the mean of the given array of the given length. Chris@152: */ cannam@0: static double mean( const double* src, unsigned int len ); Chris@152: Chris@152: /** Chris@152: * Return the mean of the subset of the given vector identified by Chris@152: * start and count. Chris@152: */ cannam@54: static double mean( const std::vector &data, cannam@54: unsigned int start, unsigned int count ); Chris@152: Chris@152: /** Chris@152: * Return the sum of the values in the given array of the given Chris@152: * length. Chris@152: */ cannam@0: static double sum( const double* src, unsigned int len ); Chris@152: Chris@152: /** Chris@152: * Return the median of the values in the given array of the given Chris@152: * length. If the array is even in length, the returned value will Chris@152: * be half-way between the two values adjacent to median. Chris@152: */ cannam@34: static double median( const double* src, unsigned int len ); cannam@34: Chris@152: /** Chris@152: * The principle argument function. Map the phase angle ang into Chris@152: * the range [-pi,pi). Chris@152: */ cannam@0: static double princarg( double ang ); Chris@152: Chris@152: /** Chris@152: * Floating-point division modulus: return x % y. Chris@152: */ cannam@0: static double mod( double x, double y); cannam@34: cannam@0: static void getAlphaNorm(const double *data, unsigned int len, unsigned int alpha, double* ANorm); cannam@0: static double getAlphaNorm(const std::vector &data, unsigned int alpha ); cannam@34: cannam@7: static void circShift( double* data, int length, int shift); cannam@34: cannam@54: static int getMax( double* data, unsigned int length, double* max = 0 ); cannam@54: static int getMax( const std::vector &data, double* max = 0 ); cannam@7: static int compareInt(const void * a, const void * b); cannam@34: cannam@34: enum NormaliseType { cannam@34: NormaliseNone, cannam@34: NormaliseUnitSum, cannam@34: NormaliseUnitMax cannam@34: }; cannam@34: Chris@123: static void normalise(double *data, int length, Chris@123: NormaliseType n = NormaliseUnitMax); cannam@34: Chris@123: static void normalise(std::vector &data, Chris@123: NormaliseType n = NormaliseUnitMax); cannam@54: Chris@152: /** Chris@152: * Threshold the input/output vector data against a moving-mean Chris@152: * average filter. Chris@152: */ cannam@54: static void adaptiveThreshold(std::vector &data); cannam@55: Chris@152: /** Chris@152: * Return true if x is 2^n for some integer n >= 0. Chris@152: */ cannam@55: static bool isPowerOfTwo(int x); Chris@123: Chris@152: /** Chris@152: * Return the next higher integer power of two from x, e.g. 1300 Chris@152: * -> 2048, 2048 -> 2048. Chris@152: */ Chris@152: static int nextPowerOfTwo(int x); Chris@152: Chris@152: /** Chris@152: * Return the next lower integer power of two from x, e.g. 1300 -> Chris@152: * 1024, 2048 -> 2048. Chris@152: */ Chris@152: static int previousPowerOfTwo(int x); Chris@152: Chris@152: /** Chris@152: * Return the nearest integer power of two to x, e.g. 1300 -> 1024, Chris@152: * 12 -> 16 (not 8; if two are equidistant, the higher is returned). Chris@152: */ Chris@152: static int nearestPowerOfTwo(int x); Chris@152: Chris@152: /** Chris@152: * Return x! Chris@152: */ Chris@135: static double factorial(int x); // returns double in case it is large Chris@125: Chris@152: /** Chris@152: * Return the greatest common divisor of natural numbers a and b. Chris@152: */ Chris@125: static int gcd(int a, int b); cannam@0: }; cannam@0: cannam@0: #endif