annotate maths/MathUtilities.h @ 515:08bcc06c38ec tip master

Remove fast-math
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 28 Jan 2020 15:27:37 +0000
parents fa407c1d9923
children
rev   line source
c@241 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@241 2
c@241 3 /*
c@241 4 QM DSP Library
c@241 5
c@241 6 Centre for Digital Music, Queen Mary, University of London.
c@309 7 This file 2005-2006 Christian Landone.
c@309 8
c@309 9 This program is free software; you can redistribute it and/or
c@309 10 modify it under the terms of the GNU General Public License as
c@309 11 published by the Free Software Foundation; either version 2 of the
c@309 12 License, or (at your option) any later version. See the file
c@309 13 COPYING included with this distribution for more information.
c@241 14 */
c@241 15
c@241 16 #ifndef MATHUTILITIES_H
c@241 17 #define MATHUTILITIES_H
c@241 18
c@241 19 #include <vector>
c@241 20
c@304 21 #include "nan-inf.h"
c@304 22
c@377 23 /**
c@377 24 * Static helper functions for simple mathematical calculations.
c@377 25 */
c@241 26 class MathUtilities
c@241 27 {
cannam@477 28 public:
c@377 29 /**
c@377 30 * Round x to the nearest integer.
c@377 31 */
c@241 32 static double round( double x );
c@259 33
c@377 34 /**
c@377 35 * Return through min and max pointers the highest and lowest
c@377 36 * values in the given array of the given length.
c@377 37 */
cannam@477 38 static void getFrameMinMax( const double* data, int len,
cannam@477 39 double* min, double* max );
c@259 40
c@377 41 /**
c@377 42 * Return the mean of the given array of the given length.
c@377 43 */
c@414 44 static double mean( const double* src, int len );
c@377 45
c@377 46 /**
c@377 47 * Return the mean of the subset of the given vector identified by
c@377 48 * start and count.
c@377 49 */
c@279 50 static double mean( const std::vector<double> &data,
c@414 51 int start, int count );
c@377 52
c@377 53 /**
c@377 54 * Return the sum of the values in the given array of the given
c@377 55 * length.
c@377 56 */
c@414 57 static double sum( const double* src, int len );
c@377 58
c@377 59 /**
c@377 60 * Return the median of the values in the given array of the given
c@377 61 * length. If the array is even in length, the returned value will
c@377 62 * be half-way between the two values adjacent to median.
c@377 63 */
c@414 64 static double median( const double* src, int len );
c@259 65
c@377 66 /**
c@377 67 * The principle argument function. Map the phase angle ang into
c@377 68 * the range [-pi,pi).
c@377 69 */
c@241 70 static double princarg( double ang );
c@377 71
c@377 72 /**
c@377 73 * Floating-point division modulus: return x % y.
c@377 74 */
c@241 75 static double mod( double x, double y);
c@259 76
c@418 77 /**
c@418 78 * The alpha norm is the alpha'th root of the mean alpha'th power
c@418 79 * magnitude. For example if alpha = 2 this corresponds to the RMS
c@418 80 * of the input data, and when alpha = 1 this is the mean
c@418 81 * magnitude.
c@418 82 */
cannam@477 83 static void getAlphaNorm(const double *data, int len, int alpha, double* ANorm);
c@418 84
c@418 85 /**
c@418 86 * The alpha norm is the alpha'th root of the mean alpha'th power
c@418 87 * magnitude. For example if alpha = 2 this corresponds to the RMS
c@418 88 * of the input data, and when alpha = 1 this is the mean
c@418 89 * magnitude.
c@418 90 */
c@414 91 static double getAlphaNorm(const std::vector <double> &data, int alpha );
c@259 92
c@259 93 enum NormaliseType {
c@259 94 NormaliseNone,
c@259 95 NormaliseUnitSum,
c@259 96 NormaliseUnitMax
c@259 97 };
c@259 98
c@348 99 static void normalise(double *data, int length,
c@348 100 NormaliseType n = NormaliseUnitMax);
c@259 101
c@348 102 static void normalise(std::vector<double> &data,
c@348 103 NormaliseType n = NormaliseUnitMax);
c@279 104
c@377 105 /**
c@418 106 * Calculate the L^p norm of a vector. Equivalent to MATLAB's
c@418 107 * norm(data, p).
c@418 108 */
c@418 109 static double getLpNorm(const std::vector<double> &data,
c@418 110 int p);
c@418 111
c@418 112 /**
c@418 113 * Normalise a vector by dividing through by its L^p norm. If the
c@418 114 * norm is below the given threshold, the unit vector for that
c@418 115 * norm is returned. p may be 0, in which case no normalisation
c@418 116 * happens and the data is returned unchanged.
c@418 117 */
c@418 118 static std::vector<double> normaliseLp(const std::vector<double> &data,
c@418 119 int p,
c@418 120 double threshold = 1e-6);
c@418 121
c@418 122 /**
c@377 123 * Threshold the input/output vector data against a moving-mean
c@377 124 * average filter.
c@377 125 */
c@279 126 static void adaptiveThreshold(std::vector<double> &data);
c@280 127
cannam@477 128 static void circShift( double* data, int length, int shift);
c@418 129
cannam@477 130 static int getMax( double* data, int length, double* max = 0 );
cannam@477 131 static int getMax( const std::vector<double> &data, double* max = 0 );
cannam@477 132 static int compareInt(const void * a, const void * b);
c@418 133
c@377 134 /**
c@377 135 * Return true if x is 2^n for some integer n >= 0.
c@377 136 */
c@280 137 static bool isPowerOfTwo(int x);
c@348 138
c@377 139 /**
c@377 140 * Return the next higher integer power of two from x, e.g. 1300
c@377 141 * -> 2048, 2048 -> 2048.
c@377 142 */
c@377 143 static int nextPowerOfTwo(int x);
c@377 144
c@377 145 /**
c@377 146 * Return the next lower integer power of two from x, e.g. 1300 ->
c@377 147 * 1024, 2048 -> 2048.
c@377 148 */
c@377 149 static int previousPowerOfTwo(int x);
c@377 150
c@377 151 /**
c@377 152 * Return the nearest integer power of two to x, e.g. 1300 -> 1024,
c@377 153 * 12 -> 16 (not 8; if two are equidistant, the higher is returned).
c@377 154 */
c@377 155 static int nearestPowerOfTwo(int x);
c@377 156
c@377 157 /**
c@377 158 * Return x!
c@377 159 */
c@360 160 static double factorial(int x); // returns double in case it is large
c@350 161
c@377 162 /**
c@377 163 * Return the greatest common divisor of natural numbers a and b.
c@377 164 */
c@350 165 static int gcd(int a, int b);
c@241 166 };
c@241 167
c@241 168 #endif