c@225: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@225: c@225: /* c@225: QM DSP Library c@225: c@225: Centre for Digital Music, Queen Mary, University of London. c@225: This file copyright 2005-2006 Christian Landone. c@225: All rights reserved. c@225: */ c@225: c@225: #include "MathUtilities.h" c@225: c@225: #include c@225: #include c@225: c@225: c@225: double MathUtilities::mod(double x, double y) c@225: { c@225: double a = floor( x / y ); c@225: c@225: double b = x - ( y * a ); c@225: return b; c@225: } c@225: c@225: double MathUtilities::princarg(double ang) c@225: { c@225: double ValOut; c@225: c@225: ValOut = mod( ang + M_PI, -2 * M_PI ) + M_PI; c@225: c@225: return ValOut; c@225: } c@225: c@225: void MathUtilities::getAlphaNorm(const double *data, unsigned int len, unsigned int alpha, double* ANorm) c@225: { c@225: unsigned int i; c@225: double temp = 0.0; c@225: double a=0.0; c@225: c@225: for( i = 0; i < len; i++) c@225: { c@225: temp = data[ i ]; c@225: c@225: a += ::pow( fabs(temp), alpha ); c@225: } c@225: a /= ( double )len; c@225: a = ::pow( a, ( 1.0 / (double) alpha ) ); c@225: c@225: *ANorm = a; c@225: } c@225: c@225: double MathUtilities::getAlphaNorm( const std::vector &data, unsigned int alpha ) c@225: { c@225: unsigned int i; c@225: unsigned int len = data.size(); c@225: double temp = 0.0; c@225: double a=0.0; c@225: c@225: for( i = 0; i < len; i++) c@225: { c@225: temp = data[ i ]; c@225: c@225: a += ::pow( fabs(temp), alpha ); c@225: } c@225: a /= ( double )len; c@225: a = ::pow( a, ( 1.0 / (double) alpha ) ); c@225: c@225: return a; c@225: } c@225: c@225: double MathUtilities::round(double x) c@225: { c@225: double val = (double)floor(x + 0.5); c@225: c@225: return val; c@225: } c@225: c@225: double MathUtilities::median(const double *src, unsigned int len) c@225: { c@225: unsigned int i, j; c@225: double tmp = 0.0; c@225: double tempMedian; c@225: double medianVal; c@225: c@225: double* scratch = new double[ len ];//Vector < double > sortedX = Vector < double > ( size ); c@225: c@225: for ( i = 0; i < len; i++ ) c@225: { c@225: scratch[i] = src[i]; c@225: } c@225: c@225: for ( i = 0; i < len - 1; i++ ) c@225: { c@225: for ( j = 0; j < len - 1 - i; j++ ) c@225: { c@225: if ( scratch[j + 1] < scratch[j] ) c@225: { c@225: // compare the two neighbors c@225: tmp = scratch[j]; // swap a[j] and a[j+1] c@225: scratch[j] = scratch[j + 1]; c@225: scratch[j + 1] = tmp; c@225: } c@225: } c@225: } c@225: int middle; c@225: if ( len % 2 == 0 ) c@225: { c@225: middle = len / 2; c@225: tempMedian = ( scratch[middle] + scratch[middle - 1] ) / 2; c@225: } c@225: else c@225: { c@225: middle = ( int )floor( len / 2.0 ); c@225: tempMedian = scratch[middle]; c@225: } c@225: c@225: medianVal = tempMedian; c@225: c@225: delete [] scratch; c@225: return medianVal; c@225: } c@225: c@225: double MathUtilities::sum(const double *src, unsigned int len) c@225: { c@225: unsigned int i ; c@225: double retVal =0.0; c@225: c@225: for( i = 0; i < len; i++) c@225: { c@225: retVal += src[ i ]; c@225: } c@225: c@225: return retVal; c@225: } c@225: c@225: double MathUtilities::mean(const double *src, unsigned int len) c@225: { c@225: double retVal =0.0; c@225: c@225: double s = sum( src, len ); c@225: c@225: retVal = s / (double)len; c@225: c@225: return retVal; c@225: } c@225: c@225: void MathUtilities::getFrameMinMax(const double *data, unsigned int len, double *min, double *max) c@225: { c@225: unsigned int i; c@225: double temp = 0.0; c@225: double a=0.0; c@225: c@225: *min = data[0]; c@225: *max = data[0]; c@225: c@225: for( i = 0; i < len; i++) c@225: { c@225: temp = data[ i ]; c@225: c@225: if( temp < *min ) c@225: { c@225: *min = temp ; c@225: } c@225: if( temp > *max ) c@225: { c@225: *max = temp ; c@225: } c@225: c@225: } c@225: } c@232: c@232: int MathUtilities::getMax( double* pData, unsigned int Length, double* pMax ) c@232: { c@232: unsigned int index = 0; c@232: unsigned int i; c@232: double temp = 0.0; c@232: c@232: double max = pData[0]; c@232: c@232: for( i = 0; i < Length; i++) c@232: { c@232: temp = pData[ i ]; c@232: c@232: if( temp > max ) c@232: { c@232: max = temp ; c@232: index = i; c@232: } c@232: c@232: } c@232: c@232: *pMax = max; c@232: c@232: c@232: return index; c@232: } c@232: c@232: void MathUtilities::circShift( double* pData, int length, int shift) c@232: { c@232: shift = shift % length; c@232: double temp; c@232: int i,n; c@232: c@232: for( i = 0; i < shift; i++) c@232: { c@232: temp=*(pData + length - 1); c@232: c@232: for( n = length-2; n >= 0; n--) c@232: { c@232: *(pData+n+1)=*(pData+n); c@232: } c@232: c@232: *pData = temp; c@232: } c@232: } c@232: c@232: int MathUtilities::compareInt (const void * a, const void * b) c@232: { c@232: return ( *(int*)a - *(int*)b ); c@232: } c@232: