comparison constant-q-cpp/src/dsp/MathUtilities.h @ 366:5d0a2ebb4d17

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