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@414
|
76 static void getAlphaNorm(const double *data, int len, int alpha, double* ANorm);
|
c@414
|
77 static double getAlphaNorm(const std::vector <double> &data, int alpha );
|
c@259
|
78
|
c@241
|
79 static void circShift( double* data, int length, int shift);
|
c@259
|
80
|
c@414
|
81 static int getMax( double* data, int length, double* max = 0 );
|
c@279
|
82 static int getMax( const std::vector<double> &data, double* max = 0 );
|
c@241
|
83 static int compareInt(const void * a, const void * b);
|
c@259
|
84
|
c@259
|
85 enum NormaliseType {
|
c@259
|
86 NormaliseNone,
|
c@259
|
87 NormaliseUnitSum,
|
c@259
|
88 NormaliseUnitMax
|
c@259
|
89 };
|
c@259
|
90
|
c@348
|
91 static void normalise(double *data, int length,
|
c@348
|
92 NormaliseType n = NormaliseUnitMax);
|
c@259
|
93
|
c@348
|
94 static void normalise(std::vector<double> &data,
|
c@348
|
95 NormaliseType n = NormaliseUnitMax);
|
c@279
|
96
|
c@377
|
97 /**
|
c@377
|
98 * Threshold the input/output vector data against a moving-mean
|
c@377
|
99 * average filter.
|
c@377
|
100 */
|
c@279
|
101 static void adaptiveThreshold(std::vector<double> &data);
|
c@280
|
102
|
c@377
|
103 /**
|
c@377
|
104 * Return true if x is 2^n for some integer n >= 0.
|
c@377
|
105 */
|
c@280
|
106 static bool isPowerOfTwo(int x);
|
c@348
|
107
|
c@377
|
108 /**
|
c@377
|
109 * Return the next higher integer power of two from x, e.g. 1300
|
c@377
|
110 * -> 2048, 2048 -> 2048.
|
c@377
|
111 */
|
c@377
|
112 static int nextPowerOfTwo(int x);
|
c@377
|
113
|
c@377
|
114 /**
|
c@377
|
115 * Return the next lower integer power of two from x, e.g. 1300 ->
|
c@377
|
116 * 1024, 2048 -> 2048.
|
c@377
|
117 */
|
c@377
|
118 static int previousPowerOfTwo(int x);
|
c@377
|
119
|
c@377
|
120 /**
|
c@377
|
121 * Return the nearest integer power of two to x, e.g. 1300 -> 1024,
|
c@377
|
122 * 12 -> 16 (not 8; if two are equidistant, the higher is returned).
|
c@377
|
123 */
|
c@377
|
124 static int nearestPowerOfTwo(int x);
|
c@377
|
125
|
c@377
|
126 /**
|
c@377
|
127 * Return x!
|
c@377
|
128 */
|
c@360
|
129 static double factorial(int x); // returns double in case it is large
|
c@350
|
130
|
c@377
|
131 /**
|
c@377
|
132 * Return the greatest common divisor of natural numbers a and b.
|
c@377
|
133 */
|
c@350
|
134 static int gcd(int a, int b);
|
c@241
|
135 };
|
c@241
|
136
|
c@241
|
137 #endif
|