cannam@0
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
cannam@0
|
2
|
cannam@0
|
3 /*
|
cannam@0
|
4 QM DSP Library
|
cannam@0
|
5
|
cannam@0
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@84
|
7 This file 2005-2006 Christian Landone.
|
Chris@84
|
8
|
Chris@84
|
9 This program is free software; you can redistribute it and/or
|
Chris@84
|
10 modify it under the terms of the GNU General Public License as
|
Chris@84
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@84
|
12 License, or (at your option) any later version. See the file
|
Chris@84
|
13 COPYING included with this distribution for more information.
|
cannam@0
|
14 */
|
cannam@0
|
15
|
cannam@0
|
16 #ifndef MATHUTILITIES_H
|
cannam@0
|
17 #define MATHUTILITIES_H
|
cannam@0
|
18
|
cannam@0
|
19 #include <vector>
|
cannam@0
|
20
|
cannam@79
|
21 #include "nan-inf.h"
|
cannam@79
|
22
|
Chris@152
|
23 /**
|
Chris@152
|
24 * Static helper functions for simple mathematical calculations.
|
Chris@152
|
25 */
|
cannam@0
|
26 class MathUtilities
|
cannam@0
|
27 {
|
cannam@0
|
28 public:
|
Chris@152
|
29 /**
|
Chris@152
|
30 * Round x to the nearest integer.
|
Chris@152
|
31 */
|
cannam@0
|
32 static double round( double x );
|
cannam@34
|
33
|
Chris@152
|
34 /**
|
Chris@152
|
35 * Return through min and max pointers the highest and lowest
|
Chris@152
|
36 * values in the given array of the given length.
|
Chris@152
|
37 */
|
cannam@0
|
38 static void getFrameMinMax( const double* data, unsigned int len, double* min, double* max );
|
cannam@34
|
39
|
Chris@152
|
40 /**
|
Chris@152
|
41 * Return the mean of the given array of the given length.
|
Chris@152
|
42 */
|
cannam@0
|
43 static double mean( const double* src, unsigned int len );
|
Chris@152
|
44
|
Chris@152
|
45 /**
|
Chris@152
|
46 * Return the mean of the subset of the given vector identified by
|
Chris@152
|
47 * start and count.
|
Chris@152
|
48 */
|
cannam@54
|
49 static double mean( const std::vector<double> &data,
|
cannam@54
|
50 unsigned int start, unsigned int count );
|
Chris@152
|
51
|
Chris@152
|
52 /**
|
Chris@152
|
53 * Return the sum of the values in the given array of the given
|
Chris@152
|
54 * length.
|
Chris@152
|
55 */
|
cannam@0
|
56 static double sum( const double* src, unsigned int len );
|
Chris@152
|
57
|
Chris@152
|
58 /**
|
Chris@152
|
59 * Return the median of the values in the given array of the given
|
Chris@152
|
60 * length. If the array is even in length, the returned value will
|
Chris@152
|
61 * be half-way between the two values adjacent to median.
|
Chris@152
|
62 */
|
cannam@34
|
63 static double median( const double* src, unsigned int len );
|
cannam@34
|
64
|
Chris@152
|
65 /**
|
Chris@152
|
66 * The principle argument function. Map the phase angle ang into
|
Chris@152
|
67 * the range [-pi,pi).
|
Chris@152
|
68 */
|
cannam@0
|
69 static double princarg( double ang );
|
Chris@152
|
70
|
Chris@152
|
71 /**
|
Chris@152
|
72 * Floating-point division modulus: return x % y.
|
Chris@152
|
73 */
|
cannam@0
|
74 static double mod( double x, double y);
|
cannam@34
|
75
|
cannam@0
|
76 static void getAlphaNorm(const double *data, unsigned int len, unsigned int alpha, double* ANorm);
|
cannam@0
|
77 static double getAlphaNorm(const std::vector <double> &data, unsigned int alpha );
|
cannam@34
|
78
|
cannam@7
|
79 static void circShift( double* data, int length, int shift);
|
cannam@34
|
80
|
cannam@54
|
81 static int getMax( double* data, unsigned int length, double* max = 0 );
|
cannam@54
|
82 static int getMax( const std::vector<double> &data, double* max = 0 );
|
cannam@7
|
83 static int compareInt(const void * a, const void * b);
|
cannam@34
|
84
|
cannam@34
|
85 enum NormaliseType {
|
cannam@34
|
86 NormaliseNone,
|
cannam@34
|
87 NormaliseUnitSum,
|
cannam@34
|
88 NormaliseUnitMax
|
cannam@34
|
89 };
|
cannam@34
|
90
|
Chris@123
|
91 static void normalise(double *data, int length,
|
Chris@123
|
92 NormaliseType n = NormaliseUnitMax);
|
cannam@34
|
93
|
Chris@123
|
94 static void normalise(std::vector<double> &data,
|
Chris@123
|
95 NormaliseType n = NormaliseUnitMax);
|
cannam@54
|
96
|
Chris@152
|
97 /**
|
Chris@152
|
98 * Threshold the input/output vector data against a moving-mean
|
Chris@152
|
99 * average filter.
|
Chris@152
|
100 */
|
cannam@54
|
101 static void adaptiveThreshold(std::vector<double> &data);
|
cannam@55
|
102
|
Chris@152
|
103 /**
|
Chris@152
|
104 * Return true if x is 2^n for some integer n >= 0.
|
Chris@152
|
105 */
|
cannam@55
|
106 static bool isPowerOfTwo(int x);
|
Chris@123
|
107
|
Chris@152
|
108 /**
|
Chris@152
|
109 * Return the next higher integer power of two from x, e.g. 1300
|
Chris@152
|
110 * -> 2048, 2048 -> 2048.
|
Chris@152
|
111 */
|
Chris@152
|
112 static int nextPowerOfTwo(int x);
|
Chris@152
|
113
|
Chris@152
|
114 /**
|
Chris@152
|
115 * Return the next lower integer power of two from x, e.g. 1300 ->
|
Chris@152
|
116 * 1024, 2048 -> 2048.
|
Chris@152
|
117 */
|
Chris@152
|
118 static int previousPowerOfTwo(int x);
|
Chris@152
|
119
|
Chris@152
|
120 /**
|
Chris@152
|
121 * Return the nearest integer power of two to x, e.g. 1300 -> 1024,
|
Chris@152
|
122 * 12 -> 16 (not 8; if two are equidistant, the higher is returned).
|
Chris@152
|
123 */
|
Chris@152
|
124 static int nearestPowerOfTwo(int x);
|
Chris@152
|
125
|
Chris@152
|
126 /**
|
Chris@152
|
127 * Return x!
|
Chris@152
|
128 */
|
Chris@135
|
129 static double factorial(int x); // returns double in case it is large
|
Chris@125
|
130
|
Chris@152
|
131 /**
|
Chris@152
|
132 * Return the greatest common divisor of natural numbers a and b.
|
Chris@152
|
133 */
|
Chris@125
|
134 static int gcd(int a, int b);
|
cannam@0
|
135 };
|
cannam@0
|
136
|
cannam@0
|
137 #endif
|