2011-03-02 07:37:39 -05:00
|
|
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
|
|
|
|
|
|
|
/*
|
|
|
|
QM DSP Library
|
|
|
|
|
|
|
|
Centre for Digital Music, Queen Mary, University of London.
|
|
|
|
This file 2005-2006 Christian Landone.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
License, or (at your option) any later version. See the file
|
|
|
|
COPYING included with this distribution for more information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "MathUtilities.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
2016-10-05 18:16:44 -04:00
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
2011-03-02 07:37:39 -05:00
|
|
|
#include <cmath>
|
|
|
|
|
2017-04-01 15:13:00 -04:00
|
|
|
using namespace std;
|
2011-03-02 07:37:39 -05:00
|
|
|
|
|
|
|
double MathUtilities::mod(double x, double y)
|
|
|
|
{
|
|
|
|
double a = floor( x / y );
|
|
|
|
|
|
|
|
double b = x - ( y * a );
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
double MathUtilities::princarg(double ang)
|
|
|
|
{
|
|
|
|
double ValOut;
|
|
|
|
|
|
|
|
ValOut = mod( ang + M_PI, -2 * M_PI ) + M_PI;
|
|
|
|
|
|
|
|
return ValOut;
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:13:00 -04:00
|
|
|
void MathUtilities::getAlphaNorm(const double *data, int len, int alpha, double* ANorm)
|
2011-03-02 07:37:39 -05:00
|
|
|
{
|
2017-04-01 15:13:00 -04:00
|
|
|
int i;
|
2011-03-02 07:37:39 -05:00
|
|
|
double temp = 0.0;
|
|
|
|
double a=0.0;
|
2016-10-05 18:16:44 -04:00
|
|
|
|
2011-03-02 07:37:39 -05:00
|
|
|
for( i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
temp = data[ i ];
|
2016-10-05 18:16:44 -04:00
|
|
|
|
2011-03-02 07:37:39 -05:00
|
|
|
a += ::pow( fabs(temp), double(alpha) );
|
|
|
|
}
|
|
|
|
a /= ( double )len;
|
|
|
|
a = ::pow( a, ( 1.0 / (double) alpha ) );
|
|
|
|
|
|
|
|
*ANorm = a;
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:13:00 -04:00
|
|
|
double MathUtilities::getAlphaNorm( const vector <double> &data, int alpha )
|
2011-03-02 07:37:39 -05:00
|
|
|
{
|
2017-04-01 15:13:00 -04:00
|
|
|
int i;
|
|
|
|
int len = data.size();
|
2011-03-02 07:37:39 -05:00
|
|
|
double temp = 0.0;
|
|
|
|
double a=0.0;
|
2016-10-05 18:16:44 -04:00
|
|
|
|
2011-03-02 07:37:39 -05:00
|
|
|
for( i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
temp = data[ i ];
|
|
|
|
a += ::pow( fabs(temp), double(alpha) );
|
|
|
|
}
|
|
|
|
a /= ( double )len;
|
|
|
|
a = ::pow( a, ( 1.0 / (double) alpha ) );
|
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
double MathUtilities::round(double x)
|
|
|
|
{
|
2016-10-05 18:16:44 -04:00
|
|
|
if (x < 0) {
|
|
|
|
return -floor(-x + 0.5);
|
|
|
|
} else {
|
|
|
|
return floor(x + 0.5);
|
|
|
|
}
|
2011-03-02 07:37:39 -05:00
|
|
|
}
|
|
|
|
|
2017-04-01 15:13:00 -04:00
|
|
|
double MathUtilities::median(const double *src, int len)
|
2011-03-02 07:37:39 -05:00
|
|
|
{
|
2016-10-05 18:16:44 -04:00
|
|
|
if (len == 0) return 0;
|
|
|
|
|
2017-04-01 15:13:00 -04:00
|
|
|
vector<double> scratch;
|
2016-10-05 18:16:44 -04:00
|
|
|
for (int i = 0; i < len; ++i) scratch.push_back(src[i]);
|
2017-04-01 15:13:00 -04:00
|
|
|
sort(scratch.begin(), scratch.end());
|
2016-10-05 18:16:44 -04:00
|
|
|
|
|
|
|
int middle = len/2;
|
|
|
|
if (len % 2 == 0) {
|
|
|
|
return (scratch[middle] + scratch[middle - 1]) / 2;
|
|
|
|
} else {
|
|
|
|
return scratch[middle];
|
2011-03-02 07:37:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:13:00 -04:00
|
|
|
double MathUtilities::sum(const double *src, int len)
|
2011-03-02 07:37:39 -05:00
|
|
|
{
|
2017-04-01 15:13:00 -04:00
|
|
|
int i ;
|
2011-03-02 07:37:39 -05:00
|
|
|
double retVal =0.0;
|
|
|
|
|
|
|
|
for( i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
retVal += src[ i ];
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:13:00 -04:00
|
|
|
double MathUtilities::mean(const double *src, int len)
|
2011-03-02 07:37:39 -05:00
|
|
|
{
|
|
|
|
double retVal =0.0;
|
|
|
|
|
2016-10-05 18:16:44 -04:00
|
|
|
if (len == 0) return 0;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2016-10-05 18:16:44 -04:00
|
|
|
double s = sum( src, len );
|
|
|
|
|
2011-03-02 07:37:39 -05:00
|
|
|
retVal = s / (double)len;
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:13:00 -04:00
|
|
|
double MathUtilities::mean(const vector<double> &src,
|
|
|
|
int start,
|
|
|
|
int count)
|
2011-03-02 07:37:39 -05:00
|
|
|
{
|
|
|
|
double sum = 0.;
|
2016-10-05 18:16:44 -04:00
|
|
|
|
|
|
|
if (count == 0) return 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < (int)count; ++i)
|
2011-03-02 07:37:39 -05:00
|
|
|
{
|
|
|
|
sum += src[start + i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return sum / count;
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:13:00 -04:00
|
|
|
void MathUtilities::getFrameMinMax(const double *data, int len, double *min, double *max)
|
2011-03-02 07:37:39 -05:00
|
|
|
{
|
2017-04-01 15:13:00 -04:00
|
|
|
int i;
|
2011-03-02 07:37:39 -05:00
|
|
|
double temp = 0.0;
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
*min = *max = 0;
|
|
|
|
return;
|
|
|
|
}
|
2016-10-05 18:16:44 -04:00
|
|
|
|
2011-03-02 07:37:39 -05:00
|
|
|
*min = data[0];
|
|
|
|
*max = data[0];
|
|
|
|
|
|
|
|
for( i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
temp = data[ i ];
|
|
|
|
|
|
|
|
if( temp < *min )
|
|
|
|
{
|
|
|
|
*min = temp ;
|
|
|
|
}
|
|
|
|
if( temp > *max )
|
|
|
|
{
|
|
|
|
*max = temp ;
|
|
|
|
}
|
2016-10-05 18:16:44 -04:00
|
|
|
|
2011-03-02 07:37:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:13:00 -04:00
|
|
|
int MathUtilities::getMax( double* pData, int Length, double* pMax )
|
2011-03-02 07:37:39 -05:00
|
|
|
{
|
2017-04-01 15:13:00 -04:00
|
|
|
int index = 0;
|
|
|
|
int i;
|
2011-03-02 07:37:39 -05:00
|
|
|
double temp = 0.0;
|
2016-10-05 18:16:44 -04:00
|
|
|
|
2011-03-02 07:37:39 -05:00
|
|
|
double max = pData[0];
|
|
|
|
|
|
|
|
for( i = 0; i < Length; i++)
|
|
|
|
{
|
|
|
|
temp = pData[ i ];
|
|
|
|
|
|
|
|
if( temp > max )
|
|
|
|
{
|
|
|
|
max = temp ;
|
|
|
|
index = i;
|
|
|
|
}
|
2016-10-05 18:16:44 -04:00
|
|
|
|
2011-03-02 07:37:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pMax) *pMax = max;
|
|
|
|
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:13:00 -04:00
|
|
|
int MathUtilities::getMax( const vector<double> & data, double* pMax )
|
2011-03-02 07:37:39 -05:00
|
|
|
{
|
2017-04-01 15:13:00 -04:00
|
|
|
int index = 0;
|
|
|
|
int i;
|
2011-03-02 07:37:39 -05:00
|
|
|
double temp = 0.0;
|
2016-10-05 18:16:44 -04:00
|
|
|
|
2011-03-02 07:37:39 -05:00
|
|
|
double max = data[0];
|
|
|
|
|
2017-04-01 15:13:00 -04:00
|
|
|
for( i = 0; i < int(data.size()); i++)
|
2011-03-02 07:37:39 -05:00
|
|
|
{
|
|
|
|
temp = data[ i ];
|
|
|
|
|
|
|
|
if( temp > max )
|
|
|
|
{
|
|
|
|
max = temp ;
|
|
|
|
index = i;
|
|
|
|
}
|
2016-10-05 18:16:44 -04:00
|
|
|
|
2011-03-02 07:37:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pMax) *pMax = max;
|
|
|
|
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MathUtilities::circShift( double* pData, int length, int shift)
|
|
|
|
{
|
|
|
|
shift = shift % length;
|
|
|
|
double temp;
|
|
|
|
int i,n;
|
|
|
|
|
|
|
|
for( i = 0; i < shift; i++)
|
|
|
|
{
|
|
|
|
temp=*(pData + length - 1);
|
|
|
|
|
|
|
|
for( n = length-2; n >= 0; n--)
|
|
|
|
{
|
|
|
|
*(pData+n+1)=*(pData+n);
|
|
|
|
}
|
|
|
|
|
|
|
|
*pData = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int MathUtilities::compareInt (const void * a, const void * b)
|
|
|
|
{
|
2016-10-05 18:16:44 -04:00
|
|
|
return ( *(int*)a - *(int*)b );
|
2011-03-02 07:37:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void MathUtilities::normalise(double *data, int length, NormaliseType type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
|
|
|
|
case NormaliseNone: return;
|
|
|
|
|
|
|
|
case NormaliseUnitSum:
|
|
|
|
{
|
|
|
|
double sum = 0.0;
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
sum += data[i];
|
|
|
|
}
|
|
|
|
if (sum != 0.0) {
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
data[i] /= sum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NormaliseUnitMax:
|
|
|
|
{
|
|
|
|
double max = 0.0;
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
if (fabs(data[i]) > max) {
|
|
|
|
max = fabs(data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (max != 0.0) {
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
data[i] /= max;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:13:00 -04:00
|
|
|
void MathUtilities::normalise(vector<double> &data, NormaliseType type)
|
2011-03-02 07:37:39 -05:00
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
|
|
|
|
case NormaliseNone: return;
|
|
|
|
|
|
|
|
case NormaliseUnitSum:
|
|
|
|
{
|
|
|
|
double sum = 0.0;
|
2016-10-05 18:16:44 -04:00
|
|
|
for (int i = 0; i < (int)data.size(); ++i) sum += data[i];
|
2011-03-02 07:37:39 -05:00
|
|
|
if (sum != 0.0) {
|
2016-10-05 18:16:44 -04:00
|
|
|
for (int i = 0; i < (int)data.size(); ++i) data[i] /= sum;
|
2011-03-02 07:37:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NormaliseUnitMax:
|
|
|
|
{
|
|
|
|
double max = 0.0;
|
2016-10-05 18:16:44 -04:00
|
|
|
for (int i = 0; i < (int)data.size(); ++i) {
|
2011-03-02 07:37:39 -05:00
|
|
|
if (fabs(data[i]) > max) max = fabs(data[i]);
|
|
|
|
}
|
|
|
|
if (max != 0.0) {
|
2016-10-05 18:16:44 -04:00
|
|
|
for (int i = 0; i < (int)data.size(); ++i) data[i] /= max;
|
2011-03-02 07:37:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 15:13:00 -04:00
|
|
|
double MathUtilities::getLpNorm(const vector<double> &data, int p)
|
|
|
|
{
|
|
|
|
double tot = 0.0;
|
|
|
|
for (int i = 0; i < int(data.size()); ++i) {
|
|
|
|
tot += abs(pow(data[i], p));
|
|
|
|
}
|
|
|
|
return pow(tot, 1.0 / p);
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<double> MathUtilities::normaliseLp(const vector<double> &data,
|
|
|
|
int p,
|
|
|
|
double threshold)
|
|
|
|
{
|
|
|
|
int n = int(data.size());
|
|
|
|
if (n == 0 || p == 0) return data;
|
|
|
|
double norm = getLpNorm(data, p);
|
|
|
|
if (norm < threshold) {
|
|
|
|
return vector<double>(n, 1.0 / pow(n, 1.0 / p)); // unit vector
|
|
|
|
}
|
|
|
|
vector<double> out(n);
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
out[i] = data[i] / norm;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MathUtilities::adaptiveThreshold(vector<double> &data)
|
2011-03-02 07:37:39 -05:00
|
|
|
{
|
|
|
|
int sz = int(data.size());
|
|
|
|
if (sz == 0) return;
|
|
|
|
|
2017-04-01 15:13:00 -04:00
|
|
|
vector<double> smoothed(sz);
|
2016-10-05 18:16:44 -04:00
|
|
|
|
2011-03-02 07:37:39 -05:00
|
|
|
int p_pre = 8;
|
|
|
|
int p_post = 7;
|
|
|
|
|
|
|
|
for (int i = 0; i < sz; ++i) {
|
|
|
|
|
2017-04-01 15:13:00 -04:00
|
|
|
int first = max(0, i - p_pre);
|
|
|
|
int last = min(sz - 1, i + p_post);
|
2011-03-02 07:37:39 -05:00
|
|
|
|
|
|
|
smoothed[i] = mean(data, first, last - first + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < sz; i++) {
|
|
|
|
data[i] -= smoothed[i];
|
|
|
|
if (data[i] < 0.0) data[i] = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MathUtilities::isPowerOfTwo(int x)
|
|
|
|
{
|
2016-10-05 18:16:44 -04:00
|
|
|
if (x < 1) return false;
|
2011-03-02 07:37:39 -05:00
|
|
|
if (x & (x-1)) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
MathUtilities::nextPowerOfTwo(int x)
|
|
|
|
{
|
|
|
|
if (isPowerOfTwo(x)) return x;
|
2016-10-05 18:16:44 -04:00
|
|
|
if (x < 1) return 1;
|
2011-03-02 07:37:39 -05:00
|
|
|
int n = 1;
|
|
|
|
while (x) { x >>= 1; n <<= 1; }
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
MathUtilities::previousPowerOfTwo(int x)
|
|
|
|
{
|
|
|
|
if (isPowerOfTwo(x)) return x;
|
2016-10-05 18:16:44 -04:00
|
|
|
if (x < 1) return 1;
|
2011-03-02 07:37:39 -05:00
|
|
|
int n = 1;
|
|
|
|
x >>= 1;
|
|
|
|
while (x) { x >>= 1; n <<= 1; }
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
MathUtilities::nearestPowerOfTwo(int x)
|
|
|
|
{
|
|
|
|
if (isPowerOfTwo(x)) return x;
|
2016-10-05 18:16:44 -04:00
|
|
|
int n0 = previousPowerOfTwo(x), n1 = nextPowerOfTwo(x);
|
2011-03-02 07:37:39 -05:00
|
|
|
if (x - n0 < n1 - x) return n0;
|
|
|
|
else return n1;
|
|
|
|
}
|
|
|
|
|
2016-10-05 18:16:44 -04:00
|
|
|
double
|
|
|
|
MathUtilities::factorial(int x)
|
|
|
|
{
|
|
|
|
if (x < 0) return 0;
|
|
|
|
double f = 1;
|
|
|
|
for (int i = 1; i <= x; ++i) {
|
|
|
|
f = f * i;
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
MathUtilities::gcd(int a, int b)
|
|
|
|
{
|
|
|
|
int c = a % b;
|
|
|
|
if (c == 0) {
|
|
|
|
return b;
|
|
|
|
} else {
|
|
|
|
return gcd(b, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|