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: */ Chris@189: static void getFrameMinMax( const double* data, 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: */ Chris@189: static double mean( const double* src, 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, Chris@189: int start, 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: */ Chris@189: static double sum( const double* src, 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: */ Chris@189: static double median( const double* src, 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: Chris@194: /** Chris@194: * The alpha norm is the alpha'th root of the mean alpha'th power Chris@194: * magnitude. For example if alpha = 2 this corresponds to the RMS Chris@194: * of the input data, and when alpha = 1 this is the mean Chris@194: * magnitude. Chris@194: */ Chris@189: static void getAlphaNorm(const double *data, int len, int alpha, double* ANorm); Chris@194: Chris@194: /** Chris@194: * The alpha norm is the alpha'th root of the mean alpha'th power Chris@194: * magnitude. For example if alpha = 2 this corresponds to the RMS Chris@194: * of the input data, and when alpha = 1 this is the mean Chris@194: * magnitude. Chris@194: */ Chris@189: static double getAlphaNorm(const std::vector &data, int alpha ); 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@194: * Calculate the L^p norm of a vector. Equivalent to MATLAB's Chris@194: * norm(data, p). Chris@194: */ Chris@194: static double getLpNorm(const std::vector &data, Chris@194: int p); Chris@194: Chris@194: /** Chris@194: * Normalise a vector by dividing through by its L^p norm. If the Chris@194: * norm is below the given threshold, the unit vector for that Chris@194: * norm is returned. p may be 0, in which case no normalisation Chris@194: * happens and the data is returned unchanged. Chris@194: */ Chris@194: static std::vector normaliseLp(const std::vector &data, Chris@194: int p, Chris@194: double threshold = 1e-6); Chris@194: Chris@194: /** 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@194: static void circShift( double* data, int length, int shift); Chris@194: Chris@194: static int getMax( double* data, int length, double* max = 0 ); Chris@194: static int getMax( const std::vector &data, double* max = 0 ); Chris@194: static int compareInt(const void * a, const void * b); Chris@194: 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