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