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@179
|
38 #include "pi.h"
|
c@119
|
39
|
c@119
|
40 /**
|
c@119
|
41 * Static helper functions for simple mathematical calculations.
|
c@119
|
42 */
|
c@119
|
43 class MathUtilities
|
c@119
|
44 {
|
c@119
|
45 public:
|
c@119
|
46 /**
|
c@119
|
47 * Round x to the nearest integer.
|
c@119
|
48 */
|
c@119
|
49 static double round( double x );
|
c@119
|
50
|
c@119
|
51 /**
|
c@119
|
52 * Return through min and max pointers the highest and lowest
|
c@119
|
53 * values in the given array of the given length.
|
c@119
|
54 */
|
c@119
|
55 static void getFrameMinMax( const double* data, unsigned int len, double* min, double* max );
|
c@119
|
56
|
c@119
|
57 /**
|
c@119
|
58 * Return the mean of the given array of the given length.
|
c@119
|
59 */
|
c@119
|
60 static double mean( const double* src, unsigned int len );
|
c@119
|
61
|
c@119
|
62 /**
|
c@119
|
63 * Return the mean of the subset of the given vector identified by
|
c@119
|
64 * start and count.
|
c@119
|
65 */
|
c@119
|
66 static double mean( const std::vector<double> &data,
|
c@119
|
67 unsigned int start, unsigned int count );
|
c@119
|
68
|
c@119
|
69 /**
|
c@119
|
70 * Return the sum of the values in the given array of the given
|
c@119
|
71 * length.
|
c@119
|
72 */
|
c@119
|
73 static double sum( const double* src, unsigned int len );
|
c@119
|
74
|
c@119
|
75 /**
|
c@119
|
76 * Return the median of the values in the given array of the given
|
c@119
|
77 * length. If the array is even in length, the returned value will
|
c@119
|
78 * be half-way between the two values adjacent to median.
|
c@119
|
79 */
|
c@119
|
80 static double median( const double* src, unsigned int len );
|
c@119
|
81
|
c@119
|
82 /**
|
c@119
|
83 * The principle argument function. Map the phase angle ang into
|
c@119
|
84 * the range [-pi,pi).
|
c@119
|
85 */
|
c@119
|
86 static double princarg( double ang );
|
c@119
|
87
|
c@119
|
88 /**
|
c@119
|
89 * Floating-point division modulus: return x % y.
|
c@119
|
90 */
|
c@119
|
91 static double mod( double x, double y);
|
c@119
|
92
|
c@119
|
93 static void getAlphaNorm(const double *data, unsigned int len, unsigned int alpha, double* ANorm);
|
c@119
|
94 static double getAlphaNorm(const std::vector <double> &data, unsigned int alpha );
|
c@119
|
95
|
c@119
|
96 static void circShift( double* data, int length, int shift);
|
c@119
|
97
|
c@119
|
98 static int getMax( double* data, unsigned int length, double* max = 0 );
|
c@119
|
99 static int getMax( const std::vector<double> &data, double* max = 0 );
|
c@119
|
100 static int compareInt(const void * a, const void * b);
|
c@119
|
101
|
c@119
|
102 enum NormaliseType {
|
c@119
|
103 NormaliseNone,
|
c@119
|
104 NormaliseUnitSum,
|
c@119
|
105 NormaliseUnitMax
|
c@119
|
106 };
|
c@119
|
107
|
c@119
|
108 static void normalise(double *data, int length,
|
c@119
|
109 NormaliseType n = NormaliseUnitMax);
|
c@119
|
110
|
c@119
|
111 static void normalise(std::vector<double> &data,
|
c@119
|
112 NormaliseType n = NormaliseUnitMax);
|
c@119
|
113
|
c@119
|
114 /**
|
c@119
|
115 * Threshold the input/output vector data against a moving-mean
|
c@119
|
116 * average filter.
|
c@119
|
117 */
|
c@119
|
118 static void adaptiveThreshold(std::vector<double> &data);
|
c@119
|
119
|
c@119
|
120 /**
|
c@119
|
121 * Return true if x is 2^n for some integer n >= 0.
|
c@119
|
122 */
|
c@119
|
123 static bool isPowerOfTwo(int x);
|
c@119
|
124
|
c@119
|
125 /**
|
c@119
|
126 * Return the next higher integer power of two from x, e.g. 1300
|
c@119
|
127 * -> 2048, 2048 -> 2048.
|
c@119
|
128 */
|
c@119
|
129 static int nextPowerOfTwo(int x);
|
c@119
|
130
|
c@119
|
131 /**
|
c@119
|
132 * Return the next lower integer power of two from x, e.g. 1300 ->
|
c@119
|
133 * 1024, 2048 -> 2048.
|
c@119
|
134 */
|
c@119
|
135 static int previousPowerOfTwo(int x);
|
c@119
|
136
|
c@119
|
137 /**
|
c@119
|
138 * Return the nearest integer power of two to x, e.g. 1300 -> 1024,
|
c@119
|
139 * 12 -> 16 (not 8; if two are equidistant, the higher is returned).
|
c@119
|
140 */
|
c@119
|
141 static int nearestPowerOfTwo(int x);
|
c@119
|
142
|
c@119
|
143 /**
|
c@119
|
144 * Return x!
|
c@119
|
145 */
|
c@119
|
146 static double factorial(int x); // returns double in case it is large
|
c@119
|
147
|
c@119
|
148 /**
|
c@119
|
149 * Return the greatest common divisor of natural numbers a and b.
|
c@119
|
150 */
|
c@119
|
151 static int gcd(int a, int b);
|
c@119
|
152 };
|
c@119
|
153
|
c@119
|
154 #endif
|