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