annotate constant-q-cpp/src/dsp/MathUtilities.h @ 372:af71cbdab621 tip

Update bqvec code
author Chris Cannam
date Tue, 19 Nov 2019 10:13:32 +0000
parents 5d0a2ebb4d17
children
rev   line source
Chris@366 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@366 2 /*
Chris@366 3 Constant-Q library
Chris@366 4 Copyright (c) 2013-2014 Queen Mary, University of London
Chris@366 5
Chris@366 6 Permission is hereby granted, free of charge, to any person
Chris@366 7 obtaining a copy of this software and associated documentation
Chris@366 8 files (the "Software"), to deal in the Software without
Chris@366 9 restriction, including without limitation the rights to use, copy,
Chris@366 10 modify, merge, publish, distribute, sublicense, and/or sell copies
Chris@366 11 of the Software, and to permit persons to whom the Software is
Chris@366 12 furnished to do so, subject to the following conditions:
Chris@366 13
Chris@366 14 The above copyright notice and this permission notice shall be
Chris@366 15 included in all copies or substantial portions of the Software.
Chris@366 16
Chris@366 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@366 18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@366 19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Chris@366 20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
Chris@366 21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@366 22 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@366 23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@366 24
Chris@366 25 Except as contained in this notice, the names of the Centre for
Chris@366 26 Digital Music; Queen Mary, University of London; and Chris Cannam
Chris@366 27 shall not be used in advertising or otherwise to promote the sale,
Chris@366 28 use or other dealings in this Software without prior written
Chris@366 29 authorization.
Chris@366 30 */
Chris@366 31
Chris@366 32 #ifndef MATHUTILITIES_H
Chris@366 33 #define MATHUTILITIES_H
Chris@366 34
Chris@366 35 #include <vector>
Chris@366 36
Chris@366 37 #include "nan-inf.h"
Chris@366 38 #include "pi.h"
Chris@366 39
Chris@366 40 /**
Chris@366 41 * Static helper functions for simple mathematical calculations.
Chris@366 42 */
Chris@366 43 class MathUtilities
Chris@366 44 {
Chris@366 45 public:
Chris@366 46 /**
Chris@366 47 * Round x to the nearest integer.
Chris@366 48 */
Chris@366 49 static double round( double x );
Chris@366 50
Chris@366 51 /**
Chris@366 52 * Return through min and max pointers the highest and lowest
Chris@366 53 * values in the given array of the given length.
Chris@366 54 */
Chris@366 55 static void getFrameMinMax( const double* data, unsigned int len, double* min, double* max );
Chris@366 56
Chris@366 57 /**
Chris@366 58 * Return the mean of the given array of the given length.
Chris@366 59 */
Chris@366 60 static double mean( const double* src, unsigned int len );
Chris@366 61
Chris@366 62 /**
Chris@366 63 * Return the mean of the subset of the given vector identified by
Chris@366 64 * start and count.
Chris@366 65 */
Chris@366 66 static double mean( const std::vector<double> &data,
Chris@366 67 unsigned int start, unsigned int count );
Chris@366 68
Chris@366 69 /**
Chris@366 70 * Return the sum of the values in the given array of the given
Chris@366 71 * length.
Chris@366 72 */
Chris@366 73 static double sum( const double* src, unsigned int len );
Chris@366 74
Chris@366 75 /**
Chris@366 76 * Return the median of the values in the given array of the given
Chris@366 77 * length. If the array is even in length, the returned value will
Chris@366 78 * be half-way between the two values adjacent to median.
Chris@366 79 */
Chris@366 80 static double median( const double* src, unsigned int len );
Chris@366 81
Chris@366 82 /**
Chris@366 83 * The principle argument function. Map the phase angle ang into
Chris@366 84 * the range [-pi,pi).
Chris@366 85 */
Chris@366 86 static double princarg( double ang );
Chris@366 87
Chris@366 88 /**
Chris@366 89 * Floating-point division modulus: return x % y.
Chris@366 90 */
Chris@366 91 static double mod( double x, double y);
Chris@366 92
Chris@366 93 static void getAlphaNorm(const double *data, unsigned int len, unsigned int alpha, double* ANorm);
Chris@366 94 static double getAlphaNorm(const std::vector <double> &data, unsigned int alpha );
Chris@366 95
Chris@366 96 static void circShift( double* data, int length, int shift);
Chris@366 97
Chris@366 98 static int getMax( double* data, unsigned int length, double* max = 0 );
Chris@366 99 static int getMax( const std::vector<double> &data, double* max = 0 );
Chris@366 100 static int compareInt(const void * a, const void * b);
Chris@366 101
Chris@366 102 enum NormaliseType {
Chris@366 103 NormaliseNone,
Chris@366 104 NormaliseUnitSum,
Chris@366 105 NormaliseUnitMax
Chris@366 106 };
Chris@366 107
Chris@366 108 static void normalise(double *data, int length,
Chris@366 109 NormaliseType n = NormaliseUnitMax);
Chris@366 110
Chris@366 111 static void normalise(std::vector<double> &data,
Chris@366 112 NormaliseType n = NormaliseUnitMax);
Chris@366 113
Chris@366 114 /**
Chris@366 115 * Threshold the input/output vector data against a moving-mean
Chris@366 116 * average filter.
Chris@366 117 */
Chris@366 118 static void adaptiveThreshold(std::vector<double> &data);
Chris@366 119
Chris@366 120 /**
Chris@366 121 * Return true if x is 2^n for some integer n >= 0.
Chris@366 122 */
Chris@366 123 static bool isPowerOfTwo(int x);
Chris@366 124
Chris@366 125 /**
Chris@366 126 * Return the next higher integer power of two from x, e.g. 1300
Chris@366 127 * -> 2048, 2048 -> 2048.
Chris@366 128 */
Chris@366 129 static int nextPowerOfTwo(int x);
Chris@366 130
Chris@366 131 /**
Chris@366 132 * Return the next lower integer power of two from x, e.g. 1300 ->
Chris@366 133 * 1024, 2048 -> 2048.
Chris@366 134 */
Chris@366 135 static int previousPowerOfTwo(int x);
Chris@366 136
Chris@366 137 /**
Chris@366 138 * Return the nearest integer power of two to x, e.g. 1300 -> 1024,
Chris@366 139 * 12 -> 16 (not 8; if two are equidistant, the higher is returned).
Chris@366 140 */
Chris@366 141 static int nearestPowerOfTwo(int x);
Chris@366 142
Chris@366 143 /**
Chris@366 144 * Return x!
Chris@366 145 */
Chris@366 146 static double factorial(int x); // returns double in case it is large
Chris@366 147
Chris@366 148 /**
Chris@366 149 * Return the greatest common divisor of natural numbers a and b.
Chris@366 150 */
Chris@366 151 static int gcd(int a, int b);
Chris@366 152 };
Chris@366 153
Chris@366 154 #endif