c@225
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@225
|
2
|
c@225
|
3 /*
|
c@225
|
4 QM DSP Library
|
c@225
|
5
|
c@225
|
6 Centre for Digital Music, Queen Mary, University of London.
|
c@225
|
7 This file copyright 2005-2006 Christian Landone.
|
c@225
|
8 All rights reserved.
|
c@225
|
9 */
|
c@225
|
10
|
c@225
|
11 #include "MathUtilities.h"
|
c@225
|
12
|
c@225
|
13 #include <iostream>
|
c@225
|
14 #include <cmath>
|
c@225
|
15
|
c@225
|
16
|
c@225
|
17 double MathUtilities::mod(double x, double y)
|
c@225
|
18 {
|
c@225
|
19 double a = floor( x / y );
|
c@225
|
20
|
c@225
|
21 double b = x - ( y * a );
|
c@225
|
22 return b;
|
c@225
|
23 }
|
c@225
|
24
|
c@225
|
25 double MathUtilities::princarg(double ang)
|
c@225
|
26 {
|
c@225
|
27 double ValOut;
|
c@225
|
28
|
c@225
|
29 ValOut = mod( ang + M_PI, -2 * M_PI ) + M_PI;
|
c@225
|
30
|
c@225
|
31 return ValOut;
|
c@225
|
32 }
|
c@225
|
33
|
c@225
|
34 void MathUtilities::getAlphaNorm(const double *data, unsigned int len, unsigned int alpha, double* ANorm)
|
c@225
|
35 {
|
c@225
|
36 unsigned int i;
|
c@225
|
37 double temp = 0.0;
|
c@225
|
38 double a=0.0;
|
c@225
|
39
|
c@225
|
40 for( i = 0; i < len; i++)
|
c@225
|
41 {
|
c@225
|
42 temp = data[ i ];
|
c@225
|
43
|
c@225
|
44 a += ::pow( fabs(temp), alpha );
|
c@225
|
45 }
|
c@225
|
46 a /= ( double )len;
|
c@225
|
47 a = ::pow( a, ( 1.0 / (double) alpha ) );
|
c@225
|
48
|
c@225
|
49 *ANorm = a;
|
c@225
|
50 }
|
c@225
|
51
|
c@225
|
52 double MathUtilities::getAlphaNorm( const std::vector <double> &data, unsigned int alpha )
|
c@225
|
53 {
|
c@225
|
54 unsigned int i;
|
c@225
|
55 unsigned int len = data.size();
|
c@225
|
56 double temp = 0.0;
|
c@225
|
57 double a=0.0;
|
c@225
|
58
|
c@225
|
59 for( i = 0; i < len; i++)
|
c@225
|
60 {
|
c@225
|
61 temp = data[ i ];
|
c@225
|
62
|
c@225
|
63 a += ::pow( fabs(temp), alpha );
|
c@225
|
64 }
|
c@225
|
65 a /= ( double )len;
|
c@225
|
66 a = ::pow( a, ( 1.0 / (double) alpha ) );
|
c@225
|
67
|
c@225
|
68 return a;
|
c@225
|
69 }
|
c@225
|
70
|
c@225
|
71 double MathUtilities::round(double x)
|
c@225
|
72 {
|
c@225
|
73 double val = (double)floor(x + 0.5);
|
c@225
|
74
|
c@225
|
75 return val;
|
c@225
|
76 }
|
c@225
|
77
|
c@225
|
78 double MathUtilities::median(const double *src, unsigned int len)
|
c@225
|
79 {
|
c@225
|
80 unsigned int i, j;
|
c@225
|
81 double tmp = 0.0;
|
c@225
|
82 double tempMedian;
|
c@225
|
83 double medianVal;
|
c@225
|
84
|
c@225
|
85 double* scratch = new double[ len ];//Vector < double > sortedX = Vector < double > ( size );
|
c@225
|
86
|
c@225
|
87 for ( i = 0; i < len; i++ )
|
c@225
|
88 {
|
c@225
|
89 scratch[i] = src[i];
|
c@225
|
90 }
|
c@225
|
91
|
c@225
|
92 for ( i = 0; i < len - 1; i++ )
|
c@225
|
93 {
|
c@225
|
94 for ( j = 0; j < len - 1 - i; j++ )
|
c@225
|
95 {
|
c@225
|
96 if ( scratch[j + 1] < scratch[j] )
|
c@225
|
97 {
|
c@225
|
98 // compare the two neighbors
|
c@225
|
99 tmp = scratch[j]; // swap a[j] and a[j+1]
|
c@225
|
100 scratch[j] = scratch[j + 1];
|
c@225
|
101 scratch[j + 1] = tmp;
|
c@225
|
102 }
|
c@225
|
103 }
|
c@225
|
104 }
|
c@225
|
105 int middle;
|
c@225
|
106 if ( len % 2 == 0 )
|
c@225
|
107 {
|
c@225
|
108 middle = len / 2;
|
c@225
|
109 tempMedian = ( scratch[middle] + scratch[middle - 1] ) / 2;
|
c@225
|
110 }
|
c@225
|
111 else
|
c@225
|
112 {
|
c@225
|
113 middle = ( int )floor( len / 2.0 );
|
c@225
|
114 tempMedian = scratch[middle];
|
c@225
|
115 }
|
c@225
|
116
|
c@225
|
117 medianVal = tempMedian;
|
c@225
|
118
|
c@225
|
119 delete [] scratch;
|
c@225
|
120 return medianVal;
|
c@225
|
121 }
|
c@225
|
122
|
c@225
|
123 double MathUtilities::sum(const double *src, unsigned int len)
|
c@225
|
124 {
|
c@225
|
125 unsigned int i ;
|
c@225
|
126 double retVal =0.0;
|
c@225
|
127
|
c@225
|
128 for( i = 0; i < len; i++)
|
c@225
|
129 {
|
c@225
|
130 retVal += src[ i ];
|
c@225
|
131 }
|
c@225
|
132
|
c@225
|
133 return retVal;
|
c@225
|
134 }
|
c@225
|
135
|
c@225
|
136 double MathUtilities::mean(const double *src, unsigned int len)
|
c@225
|
137 {
|
c@225
|
138 double retVal =0.0;
|
c@225
|
139
|
c@225
|
140 double s = sum( src, len );
|
c@225
|
141
|
c@225
|
142 retVal = s / (double)len;
|
c@225
|
143
|
c@225
|
144 return retVal;
|
c@225
|
145 }
|
c@225
|
146
|
c@225
|
147 void MathUtilities::getFrameMinMax(const double *data, unsigned int len, double *min, double *max)
|
c@225
|
148 {
|
c@225
|
149 unsigned int i;
|
c@225
|
150 double temp = 0.0;
|
c@225
|
151 double a=0.0;
|
c@225
|
152
|
c@225
|
153 *min = data[0];
|
c@225
|
154 *max = data[0];
|
c@225
|
155
|
c@225
|
156 for( i = 0; i < len; i++)
|
c@225
|
157 {
|
c@225
|
158 temp = data[ i ];
|
c@225
|
159
|
c@225
|
160 if( temp < *min )
|
c@225
|
161 {
|
c@225
|
162 *min = temp ;
|
c@225
|
163 }
|
c@225
|
164 if( temp > *max )
|
c@225
|
165 {
|
c@225
|
166 *max = temp ;
|
c@225
|
167 }
|
c@225
|
168
|
c@225
|
169 }
|
c@225
|
170 }
|