1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
6 Centre for Digital Music, Queen Mary, University of London.
7 This file 2005-2006 Christian Landone.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
16 #include "MathUtilities.h"
22 double MathUtilities::mod(double x, double y)
24 double a = floor( x / y );
26 double b = x - ( y * a );
30 double MathUtilities::princarg(double ang)
34 ValOut = mod( ang + M_PI, -2 * M_PI ) + M_PI;
39 void MathUtilities::getAlphaNorm(const double *data, unsigned int len, unsigned int alpha, double* ANorm)
45 for( i = 0; i < len; i++)
49 a += ::pow( fabs(temp), double(alpha) );
52 a = ::pow( a, ( 1.0 / (double) alpha ) );
57 double MathUtilities::getAlphaNorm( const std::vector <double> &data, unsigned int alpha )
60 unsigned int len = data.size();
64 for( i = 0; i < len; i++)
68 a += ::pow( fabs(temp), double(alpha) );
71 a = ::pow( a, ( 1.0 / (double) alpha ) );
76 double MathUtilities::round(double x)
78 double val = (double)floor(x + 0.5);
83 double MathUtilities::median(const double *src, unsigned int len)
90 double* scratch = new double[ len ];//Vector < double > sortedX = Vector < double > ( size );
92 for ( i = 0; i < len; i++ )
97 for ( i = 0; i < len - 1; i++ )
99 for ( j = 0; j < len - 1 - i; j++ )
101 if ( scratch[j + 1] < scratch[j] )
103 // compare the two neighbors
104 tmp = scratch[j]; // swap a[j] and a[j+1]
105 scratch[j] = scratch[j + 1];
106 scratch[j + 1] = tmp;
114 tempMedian = ( scratch[middle] + scratch[middle - 1] ) / 2;
118 middle = ( int )floor( len / 2.0 );
119 tempMedian = scratch[middle];
122 medianVal = tempMedian;
128 double MathUtilities::sum(const double *src, unsigned int len)
133 for( i = 0; i < len; i++)
141 double MathUtilities::mean(const double *src, unsigned int len)
145 double s = sum( src, len );
147 retVal = s / (double)len;
152 double MathUtilities::mean(const std::vector<double> &src,
158 for (int i = 0; i < count; ++i)
160 sum += src[start + i];
166 void MathUtilities::getFrameMinMax(const double *data, unsigned int len, double *min, double *max)
180 for( i = 0; i < len; i++)
196 int MathUtilities::getMax( double* pData, unsigned int Length, double* pMax )
198 unsigned int index = 0;
202 double max = pData[0];
204 for( i = 0; i < Length; i++)
216 if (pMax) *pMax = max;
222 int MathUtilities::getMax( const std::vector<double> & data, double* pMax )
224 unsigned int index = 0;
228 double max = data[0];
230 for( i = 0; i < data.size(); i++)
242 if (pMax) *pMax = max;
248 void MathUtilities::circShift( double* pData, int length, int shift)
250 shift = shift % length;
254 for( i = 0; i < shift; i++)
256 temp=*(pData + length - 1);
258 for( n = length-2; n >= 0; n--)
260 *(pData+n+1)=*(pData+n);
267 int MathUtilities::compareInt (const void * a, const void * b)
269 return ( *(int*)a - *(int*)b );
272 void MathUtilities::normalise(double *data, int length, NormaliseType type)
276 case NormaliseNone: return;
278 case NormaliseUnitSum:
281 for (int i = 0; i < length; ++i) {
285 for (int i = 0; i < length; ++i) {
292 case NormaliseUnitMax:
295 for (int i = 0; i < length; ++i) {
296 if (fabs(data[i]) > max) {
301 for (int i = 0; i < length; ++i) {
311 void MathUtilities::normalise(std::vector<double> &data, NormaliseType type)
315 case NormaliseNone: return;
317 case NormaliseUnitSum:
320 for (int i = 0; i < data.size(); ++i) sum += data[i];
322 for (int i = 0; i < data.size(); ++i) data[i] /= sum;
327 case NormaliseUnitMax:
330 for (int i = 0; i < data.size(); ++i) {
331 if (fabs(data[i]) > max) max = fabs(data[i]);
334 for (int i = 0; i < data.size(); ++i) data[i] /= max;
342 void MathUtilities::adaptiveThreshold(std::vector<double> &data)
344 int sz = int(data.size());
347 std::vector<double> smoothed(sz);
352 for (int i = 0; i < sz; ++i) {
354 int first = std::max(0, i - p_pre);
355 int last = std::min(sz - 1, i + p_post);
357 smoothed[i] = mean(data, first, last - first + 1);
360 for (int i = 0; i < sz; i++) {
361 data[i] -= smoothed[i];
362 if (data[i] < 0.0) data[i] = 0.0;
367 MathUtilities::isPowerOfTwo(int x)
369 if (x < 2) return false;
370 if (x & (x-1)) return false;
375 MathUtilities::nextPowerOfTwo(int x)
377 if (isPowerOfTwo(x)) return x;
379 while (x) { x >>= 1; n <<= 1; }
384 MathUtilities::previousPowerOfTwo(int x)
386 if (isPowerOfTwo(x)) return x;
389 while (x) { x >>= 1; n <<= 1; }
394 MathUtilities::nearestPowerOfTwo(int x)
396 if (isPowerOfTwo(x)) return x;
397 int n0 = previousPowerOfTwo(x), n1 = nearestPowerOfTwo(x);
398 if (x - n0 < n1 - x) return n0;