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@348: #include c@348: #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@414: void MathUtilities::getAlphaNorm(const double *data, int len, int alpha, double* ANorm) c@241: { c@414: 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@414: double MathUtilities::getAlphaNorm( const std::vector &data, int alpha ) c@241: { c@414: int i; c@414: 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@348: if (x < 0) { c@348: return -floor(-x + 0.5); c@348: } else { c@348: return floor(x + 0.5); c@348: } c@241: } c@241: c@414: double MathUtilities::median(const double *src, int len) c@241: { c@348: if (len == 0) return 0; c@348: c@348: std::vector scratch; c@348: for (int i = 0; i < len; ++i) scratch.push_back(src[i]); c@348: std::sort(scratch.begin(), scratch.end()); c@241: c@348: int middle = len/2; c@348: if (len % 2 == 0) { c@348: return (scratch[middle] + scratch[middle - 1]) / 2; c@348: } else { c@348: return scratch[middle]; c@241: } c@241: } c@241: c@414: double MathUtilities::sum(const double *src, int len) c@241: { c@414: 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@414: double MathUtilities::mean(const double *src, int len) c@241: { c@241: double retVal =0.0; c@241: c@348: if (len == 0) return 0; c@348: 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@414: int start, c@414: int count) c@279: { c@279: double sum = 0.; c@279: c@348: if (count == 0) return 0; c@348: c@325: for (int i = 0; i < (int)count; ++i) c@279: { c@279: sum += src[start + i]; c@279: } c@279: c@279: return sum / count; c@279: } c@279: c@414: void MathUtilities::getFrameMinMax(const double *data, int len, double *min, double *max) c@241: { c@414: int i; c@241: double temp = 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@414: int MathUtilities::getMax( double* pData, int Length, double* pMax ) c@241: { c@414: int index = 0; c@414: 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@414: int index = 0; c@414: int i; c@279: double temp = 0.0; c@279: c@279: double max = data[0]; c@279: c@414: for( i = 0; i < int(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@325: for (int i = 0; i < (int)data.size(); ++i) sum += data[i]; c@259: if (sum != 0.0) { c@325: for (int i = 0; i < (int)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@325: for (int i = 0; i < (int)data.size(); ++i) { c@259: if (fabs(data[i]) > max) max = fabs(data[i]); c@259: } c@259: if (max != 0.0) { c@325: for (int i = 0; i < (int)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@348: if (x < 1) 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@348: if (x < 1) return 1; 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@348: if (x < 1) return 1; 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@348: int n0 = previousPowerOfTwo(x), n1 = nextPowerOfTwo(x); c@280: if (x - n0 < n1 - x) return n0; c@280: else return n1; c@280: } c@280: c@360: double c@348: MathUtilities::factorial(int x) c@348: { c@348: if (x < 0) return 0; c@360: double f = 1; c@348: for (int i = 1; i <= x; ++i) { c@348: f = f * i; c@348: } c@348: return f; c@348: } c@348: c@350: int c@350: MathUtilities::gcd(int a, int b) c@350: { c@350: int c = a % b; c@350: if (c == 0) { c@350: return b; c@350: } else { c@350: return gcd(b, c); c@350: } c@350: } c@350: