comparison src/dsp/MathUtilities.h @ 119:a38d6940f8fb

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