Mercurial > hg > qm-dsp
comparison maths/MathUtilities.cpp @ 477:fa407c1d9923
Untabify + indent
author | Chris Cannam <cannam@all-day-breakfast.com> |
---|---|
date | Thu, 30 May 2019 18:36:48 +0100 |
parents | d583feeeed7a |
children |
comparison
equal
deleted
inserted
replaced
476:2de6184b2ce0 | 477:fa407c1d9923 |
---|---|
42 void MathUtilities::getAlphaNorm(const double *data, int len, int alpha, double* ANorm) | 42 void MathUtilities::getAlphaNorm(const double *data, int len, int alpha, double* ANorm) |
43 { | 43 { |
44 int i; | 44 int i; |
45 double temp = 0.0; | 45 double temp = 0.0; |
46 double a=0.0; | 46 double a=0.0; |
47 | 47 |
48 for( i = 0; i < len; i++) | 48 for( i = 0; i < len; i++) { |
49 { | 49 temp = data[ i ]; |
50 temp = data[ i ]; | 50 a += ::pow( fabs(temp), double(alpha) ); |
51 | |
52 a += ::pow( fabs(temp), double(alpha) ); | |
53 } | 51 } |
54 a /= ( double )len; | 52 a /= ( double )len; |
55 a = ::pow( a, ( 1.0 / (double) alpha ) ); | 53 a = ::pow( a, ( 1.0 / (double) alpha ) ); |
56 | 54 |
57 *ANorm = a; | 55 *ANorm = a; |
61 { | 59 { |
62 int i; | 60 int i; |
63 int len = data.size(); | 61 int len = data.size(); |
64 double temp = 0.0; | 62 double temp = 0.0; |
65 double a=0.0; | 63 double a=0.0; |
66 | 64 |
67 for( i = 0; i < len; i++) | 65 for( i = 0; i < len; i++) { |
68 { | 66 temp = data[ i ]; |
69 temp = data[ i ]; | 67 a += ::pow( fabs(temp), double(alpha) ); |
70 a += ::pow( fabs(temp), double(alpha) ); | |
71 } | 68 } |
72 a /= ( double )len; | 69 a /= ( double )len; |
73 a = ::pow( a, ( 1.0 / (double) alpha ) ); | 70 a = ::pow( a, ( 1.0 / (double) alpha ) ); |
74 | 71 |
75 return a; | 72 return a; |
103 double MathUtilities::sum(const double *src, int len) | 100 double MathUtilities::sum(const double *src, int len) |
104 { | 101 { |
105 int i ; | 102 int i ; |
106 double retVal =0.0; | 103 double retVal =0.0; |
107 | 104 |
108 for( i = 0; i < len; i++) | 105 for( i = 0; i < len; i++) { |
109 { | 106 retVal += src[ i ]; |
110 retVal += src[ i ]; | |
111 } | 107 } |
112 | 108 |
113 return retVal; | 109 return retVal; |
114 } | 110 } |
115 | 111 |
118 double retVal =0.0; | 114 double retVal =0.0; |
119 | 115 |
120 if (len == 0) return 0; | 116 if (len == 0) return 0; |
121 | 117 |
122 double s = sum( src, len ); | 118 double s = sum( src, len ); |
123 | 119 |
124 retVal = s / (double)len; | 120 retVal = s / (double)len; |
125 | 121 |
126 return retVal; | 122 return retVal; |
127 } | 123 } |
128 | 124 |
129 double MathUtilities::mean(const vector<double> &src, | 125 double MathUtilities::mean(const vector<double> &src, |
130 int start, | 126 int start, |
131 int count) | 127 int count) |
132 { | 128 { |
133 double sum = 0.; | 129 double sum = 0.; |
134 | 130 |
135 if (count == 0) return 0; | 131 if (count == 0) return 0; |
136 | 132 |
137 for (int i = 0; i < (int)count; ++i) | 133 for (int i = 0; i < (int)count; ++i) { |
138 { | |
139 sum += src[start + i]; | 134 sum += src[start + i]; |
140 } | 135 } |
141 | 136 |
142 return sum / count; | 137 return sum / count; |
143 } | 138 } |
149 | 144 |
150 if (len == 0) { | 145 if (len == 0) { |
151 *min = *max = 0; | 146 *min = *max = 0; |
152 return; | 147 return; |
153 } | 148 } |
154 | 149 |
155 *min = data[0]; | 150 *min = data[0]; |
156 *max = data[0]; | 151 *max = data[0]; |
157 | 152 |
158 for( i = 0; i < len; i++) | 153 for( i = 0; i < len; i++) { |
159 { | 154 temp = data[ i ]; |
160 temp = data[ i ]; | 155 |
161 | 156 if( temp < *min ) { |
162 if( temp < *min ) | 157 *min = temp ; |
163 { | 158 } |
164 *min = temp ; | 159 if( temp > *max ) { |
165 } | 160 *max = temp ; |
166 if( temp > *max ) | 161 } |
167 { | |
168 *max = temp ; | |
169 } | |
170 | |
171 } | 162 } |
172 } | 163 } |
173 | 164 |
174 int MathUtilities::getMax( double* pData, int Length, double* pMax ) | 165 int MathUtilities::getMax( double* pData, int Length, double* pMax ) |
175 { | 166 { |
176 int index = 0; | 167 int index = 0; |
177 int i; | 168 int i; |
178 double temp = 0.0; | 169 double temp = 0.0; |
179 | 170 |
180 double max = pData[0]; | 171 double max = pData[0]; |
181 | 172 |
182 for( i = 0; i < Length; i++) | 173 for( i = 0; i < Length; i++) { |
183 { | 174 temp = pData[ i ]; |
184 temp = pData[ i ]; | 175 |
185 | 176 if( temp > max ) { |
186 if( temp > max ) | 177 max = temp ; |
187 { | 178 index = i; |
188 max = temp ; | 179 } |
189 index = i; | 180 } |
190 } | 181 |
191 | 182 if (pMax) *pMax = max; |
192 } | 183 |
193 | 184 |
194 if (pMax) *pMax = max; | 185 return index; |
195 | |
196 | |
197 return index; | |
198 } | 186 } |
199 | 187 |
200 int MathUtilities::getMax( const vector<double> & data, double* pMax ) | 188 int MathUtilities::getMax( const vector<double> & data, double* pMax ) |
201 { | 189 { |
202 int index = 0; | 190 int index = 0; |
203 int i; | 191 int i; |
204 double temp = 0.0; | 192 double temp = 0.0; |
205 | 193 |
206 double max = data[0]; | 194 double max = data[0]; |
207 | 195 |
208 for( i = 0; i < int(data.size()); i++) | 196 for( i = 0; i < int(data.size()); i++) { |
209 { | 197 |
210 temp = data[ i ]; | 198 temp = data[ i ]; |
211 | 199 |
212 if( temp > max ) | 200 if( temp > max ) { |
213 { | 201 max = temp ; |
214 max = temp ; | 202 index = i; |
215 index = i; | 203 } |
216 } | 204 } |
217 | 205 |
218 } | 206 if (pMax) *pMax = max; |
219 | 207 |
220 if (pMax) *pMax = max; | 208 |
221 | 209 return index; |
222 | |
223 return index; | |
224 } | 210 } |
225 | 211 |
226 void MathUtilities::circShift( double* pData, int length, int shift) | 212 void MathUtilities::circShift( double* pData, int length, int shift) |
227 { | 213 { |
228 shift = shift % length; | 214 shift = shift % length; |
229 double temp; | 215 double temp; |
230 int i,n; | 216 int i,n; |
231 | 217 |
232 for( i = 0; i < shift; i++) | 218 for( i = 0; i < shift; i++) { |
233 { | 219 |
234 temp=*(pData + length - 1); | 220 temp=*(pData + length - 1); |
235 | 221 |
236 for( n = length-2; n >= 0; n--) | 222 for( n = length-2; n >= 0; n--) { |
237 { | 223 *(pData+n+1)=*(pData+n); |
238 *(pData+n+1)=*(pData+n); | 224 } |
239 } | |
240 | 225 |
241 *pData = temp; | 226 *pData = temp; |
242 } | 227 } |
243 } | 228 } |
244 | 229 |
245 int MathUtilities::compareInt (const void * a, const void * b) | 230 int MathUtilities::compareInt (const void * a, const void * b) |
246 { | 231 { |
247 return ( *(int*)a - *(int*)b ); | 232 return ( *(int*)a - *(int*)b ); |
248 } | 233 } |
249 | 234 |
250 void MathUtilities::normalise(double *data, int length, NormaliseType type) | 235 void MathUtilities::normalise(double *data, int length, NormaliseType type) |
251 { | 236 { |
252 switch (type) { | 237 switch (type) { |
325 } | 310 } |
326 return pow(tot, 1.0 / p); | 311 return pow(tot, 1.0 / p); |
327 } | 312 } |
328 | 313 |
329 vector<double> MathUtilities::normaliseLp(const vector<double> &data, | 314 vector<double> MathUtilities::normaliseLp(const vector<double> &data, |
330 int p, | 315 int p, |
331 double threshold) | 316 double threshold) |
332 { | 317 { |
333 int n = int(data.size()); | 318 int n = int(data.size()); |
334 if (n == 0 || p == 0) return data; | 319 if (n == 0 || p == 0) return data; |
335 double norm = getLpNorm(data, p); | 320 double norm = getLpNorm(data, p); |
336 if (norm < threshold) { | 321 if (norm < threshold) { |
347 { | 332 { |
348 int sz = int(data.size()); | 333 int sz = int(data.size()); |
349 if (sz == 0) return; | 334 if (sz == 0) return; |
350 | 335 |
351 vector<double> smoothed(sz); | 336 vector<double> smoothed(sz); |
352 | 337 |
353 int p_pre = 8; | 338 int p_pre = 8; |
354 int p_post = 7; | 339 int p_post = 7; |
355 | 340 |
356 for (int i = 0; i < sz; ++i) { | 341 for (int i = 0; i < sz; ++i) { |
357 | 342 |
398 | 383 |
399 int | 384 int |
400 MathUtilities::nearestPowerOfTwo(int x) | 385 MathUtilities::nearestPowerOfTwo(int x) |
401 { | 386 { |
402 if (isPowerOfTwo(x)) return x; | 387 if (isPowerOfTwo(x)) return x; |
403 int n0 = previousPowerOfTwo(x), n1 = nextPowerOfTwo(x); | 388 int n0 = previousPowerOfTwo(x); |
389 int n1 = nextPowerOfTwo(x); | |
404 if (x - n0 < n1 - x) return n0; | 390 if (x - n0 < n1 - x) return n0; |
405 else return n1; | 391 else return n1; |
406 } | 392 } |
407 | 393 |
408 double | 394 double |
409 MathUtilities::factorial(int x) | 395 MathUtilities::factorial(int x) |
410 { | 396 { |
411 if (x < 0) return 0; | 397 if (x < 0) return 0; |
412 double f = 1; | 398 double f = 1; |
413 for (int i = 1; i <= x; ++i) { | 399 for (int i = 1; i <= x; ++i) { |
414 f = f * i; | 400 f = f * i; |
415 } | 401 } |
416 return f; | 402 return f; |
417 } | 403 } |
418 | 404 |
419 int | 405 int |