c@241: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@241: c@241: /* c@241: QM DSP Library c@241: c@241: Centre for Digital Music, Queen Mary, University of London. c@309: This file 2005-2006 Christian Landone. c@309: c@309: This program is free software; you can redistribute it and/or c@309: modify it under the terms of the GNU General Public License as c@309: published by the Free Software Foundation; either version 2 of the c@309: License, or (at your option) any later version. See the file c@309: COPYING included with this distribution for more information. c@241: */ c@241: c@241: #include "MathUtilities.h" c@241: c@241: #include c@241: #include c@241: c@241: c@241: double MathUtilities::mod(double x, double y) c@241: { c@241: double a = floor( x / y ); c@241: c@241: double b = x - ( y * a ); c@241: return b; c@241: } c@241: c@241: double MathUtilities::princarg(double ang) c@241: { c@241: double ValOut; c@241: c@241: ValOut = mod( ang + M_PI, -2 * M_PI ) + M_PI; c@241: c@241: return ValOut; c@241: } c@241: c@241: void MathUtilities::getAlphaNorm(const double *data, unsigned int len, unsigned int alpha, double* ANorm) c@241: { c@241: unsigned int i; c@241: double temp = 0.0; c@241: double a=0.0; c@241: c@241: for( i = 0; i < len; i++) c@241: { c@241: temp = data[ i ]; c@241: c@304: a += ::pow( fabs(temp), double(alpha) ); c@241: } c@241: a /= ( double )len; c@241: a = ::pow( a, ( 1.0 / (double) alpha ) ); c@241: c@241: *ANorm = a; c@241: } c@241: c@241: double MathUtilities::getAlphaNorm( const std::vector &data, unsigned int alpha ) c@241: { c@241: unsigned int i; c@241: unsigned int len = data.size(); c@241: double temp = 0.0; c@241: double a=0.0; c@241: c@241: for( i = 0; i < len; i++) c@241: { c@241: temp = data[ i ]; c@241: c@304: a += ::pow( fabs(temp), double(alpha) ); c@241: } c@241: a /= ( double )len; c@241: a = ::pow( a, ( 1.0 / (double) alpha ) ); c@241: c@241: return a; c@241: } c@241: c@241: double MathUtilities::round(double x) c@241: { c@241: double val = (double)floor(x + 0.5); c@241: c@241: return val; c@241: } c@241: c@241: double MathUtilities::median(const double *src, unsigned int len) c@241: { c@241: unsigned int i, j; c@241: double tmp = 0.0; c@241: double tempMedian; c@241: double medianVal; c@241: c@241: double* scratch = new double[ len ];//Vector < double > sortedX = Vector < double > ( size ); c@241: c@241: for ( i = 0; i < len; i++ ) c@241: { c@241: scratch[i] = src[i]; c@241: } c@241: c@241: for ( i = 0; i < len - 1; i++ ) c@241: { c@241: for ( j = 0; j < len - 1 - i; j++ ) c@241: { c@241: if ( scratch[j + 1] < scratch[j] ) c@241: { c@241: // compare the two neighbors c@241: tmp = scratch[j]; // swap a[j] and a[j+1] c@241: scratch[j] = scratch[j + 1]; c@241: scratch[j + 1] = tmp; c@241: } c@241: } c@241: } c@241: int middle; c@241: if ( len % 2 == 0 ) c@241: { c@241: middle = len / 2; c@241: tempMedian = ( scratch[middle] + scratch[middle - 1] ) / 2; c@241: } c@241: else c@241: { c@241: middle = ( int )floor( len / 2.0 ); c@241: tempMedian = scratch[middle]; c@241: } c@241: c@241: medianVal = tempMedian; c@241: c@241: delete [] scratch; c@241: return medianVal; c@241: } c@241: c@241: double MathUtilities::sum(const double *src, unsigned int len) c@241: { c@241: unsigned int i ; c@241: double retVal =0.0; c@241: c@241: for( i = 0; i < len; i++) c@241: { c@241: retVal += src[ i ]; c@241: } c@241: c@241: return retVal; c@241: } c@241: c@241: double MathUtilities::mean(const double *src, unsigned int len) c@241: { c@241: double retVal =0.0; c@241: c@241: double s = sum( src, len ); c@241: c@241: retVal = s / (double)len; c@241: c@241: return retVal; c@241: } c@241: c@279: double MathUtilities::mean(const std::vector &src, c@279: unsigned int start, c@279: unsigned int count) c@279: { c@279: double sum = 0.; c@279: c@279: for (int i = 0; i < count; ++i) c@279: { c@279: sum += src[start + i]; c@279: } c@279: c@279: return sum / count; c@279: } c@279: c@241: void MathUtilities::getFrameMinMax(const double *data, unsigned int len, double *min, double *max) c@241: { c@241: unsigned int i; c@241: double temp = 0.0; c@241: double a=0.0; c@283: c@283: if (len == 0) { c@283: *min = *max = 0; c@283: return; c@283: } c@241: c@241: *min = data[0]; c@241: *max = data[0]; c@241: c@241: for( i = 0; i < len; i++) c@241: { c@241: temp = data[ i ]; c@241: c@241: if( temp < *min ) c@241: { c@241: *min = temp ; c@241: } c@241: if( temp > *max ) c@241: { c@241: *max = temp ; c@241: } c@241: c@241: } c@241: } c@241: c@241: int MathUtilities::getMax( double* pData, unsigned int Length, double* pMax ) c@241: { c@241: unsigned int index = 0; c@241: unsigned int i; c@241: double temp = 0.0; c@241: c@241: double max = pData[0]; c@241: c@241: for( i = 0; i < Length; i++) c@241: { c@241: temp = pData[ i ]; c@241: c@241: if( temp > max ) c@241: { c@241: max = temp ; c@241: index = i; c@241: } c@241: c@241: } c@241: c@279: if (pMax) *pMax = max; c@279: c@279: c@279: return index; c@279: } c@279: c@279: int MathUtilities::getMax( const std::vector & data, double* pMax ) c@279: { c@279: unsigned int index = 0; c@279: unsigned int i; c@279: double temp = 0.0; c@279: c@279: double max = data[0]; c@279: c@279: for( i = 0; i < data.size(); i++) c@279: { c@279: temp = data[ i ]; c@279: c@279: if( temp > max ) c@279: { c@279: max = temp ; c@279: index = i; c@279: } c@279: c@279: } c@279: c@279: if (pMax) *pMax = max; c@241: c@241: c@241: return index; c@241: } c@241: c@241: void MathUtilities::circShift( double* pData, int length, int shift) c@241: { c@241: shift = shift % length; c@241: double temp; c@241: int i,n; c@241: c@241: for( i = 0; i < shift; i++) c@241: { c@241: temp=*(pData + length - 1); c@241: c@241: for( n = length-2; n >= 0; n--) c@241: { c@241: *(pData+n+1)=*(pData+n); c@241: } c@241: c@241: *pData = temp; c@241: } c@241: } c@241: c@241: int MathUtilities::compareInt (const void * a, const void * b) c@241: { c@241: return ( *(int*)a - *(int*)b ); c@241: } c@241: c@259: void MathUtilities::normalise(double *data, int length, NormaliseType type) c@259: { c@259: switch (type) { c@259: c@259: case NormaliseNone: return; c@259: c@259: case NormaliseUnitSum: c@259: { c@259: double sum = 0.0; c@259: for (int i = 0; i < length; ++i) { c@259: sum += data[i]; c@259: } c@259: if (sum != 0.0) { c@259: for (int i = 0; i < length; ++i) { c@259: data[i] /= sum; c@259: } c@259: } c@259: } c@259: break; c@259: c@259: case NormaliseUnitMax: c@259: { c@259: double max = 0.0; c@259: for (int i = 0; i < length; ++i) { c@259: if (fabs(data[i]) > max) { c@259: max = fabs(data[i]); c@259: } c@259: } c@259: if (max != 0.0) { c@259: for (int i = 0; i < length; ++i) { c@259: data[i] /= max; c@259: } c@259: } c@259: } c@259: break; c@259: c@259: } c@259: } c@259: c@259: void MathUtilities::normalise(std::vector &data, NormaliseType type) c@259: { c@259: switch (type) { c@259: c@259: case NormaliseNone: return; c@259: c@259: case NormaliseUnitSum: c@259: { c@259: double sum = 0.0; c@259: for (int i = 0; i < data.size(); ++i) sum += data[i]; c@259: if (sum != 0.0) { c@259: for (int i = 0; i < data.size(); ++i) data[i] /= sum; c@259: } c@259: } c@259: break; c@259: c@259: case NormaliseUnitMax: c@259: { c@259: double max = 0.0; c@259: for (int i = 0; i < data.size(); ++i) { c@259: if (fabs(data[i]) > max) max = fabs(data[i]); c@259: } c@259: if (max != 0.0) { c@259: for (int i = 0; i < data.size(); ++i) data[i] /= max; c@259: } c@259: } c@259: break; c@259: c@259: } c@259: } c@259: c@279: void MathUtilities::adaptiveThreshold(std::vector &data) c@279: { c@279: int sz = int(data.size()); c@279: if (sz == 0) return; c@279: c@279: std::vector smoothed(sz); c@279: c@279: int p_pre = 8; c@279: int p_post = 7; c@279: c@279: for (int i = 0; i < sz; ++i) { c@279: c@279: int first = std::max(0, i - p_pre); c@279: int last = std::min(sz - 1, i + p_post); c@279: c@279: smoothed[i] = mean(data, first, last - first + 1); c@279: } c@279: c@279: for (int i = 0; i < sz; i++) { c@279: data[i] -= smoothed[i]; c@279: if (data[i] < 0.0) data[i] = 0.0; c@279: } c@279: } c@259: c@280: bool c@280: MathUtilities::isPowerOfTwo(int x) c@280: { c@280: if (x < 2) return false; c@280: if (x & (x-1)) return false; c@280: return true; c@280: } c@280: c@280: int c@280: MathUtilities::nextPowerOfTwo(int x) c@280: { c@280: if (isPowerOfTwo(x)) return x; c@280: int n = 1; c@280: while (x) { x >>= 1; n <<= 1; } c@280: return n; c@280: } c@280: c@280: int c@280: MathUtilities::previousPowerOfTwo(int x) c@280: { c@280: if (isPowerOfTwo(x)) return x; c@280: int n = 1; c@280: x >>= 1; c@280: while (x) { x >>= 1; n <<= 1; } c@280: return n; c@280: } c@280: c@280: int c@280: MathUtilities::nearestPowerOfTwo(int x) c@280: { c@280: if (isPowerOfTwo(x)) return x; c@280: int n0 = previousPowerOfTwo(x), n1 = nearestPowerOfTwo(x); c@280: if (x - n0 < n1 - x) return n0; c@280: else return n1; c@280: } c@280: