Mercurial > hg > qm-dsp
diff maths/MathUtilities.cpp @ 418:d583feeeed7a
Add L^p norms, doc, tests
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Wed, 07 Oct 2015 11:14:16 +0100 |
parents | 7e8d1f26b098 |
children | fa407c1d9923 |
line wrap: on
line diff
--- a/maths/MathUtilities.cpp Wed Oct 07 10:36:09 2015 +0100 +++ b/maths/MathUtilities.cpp Wed Oct 07 11:14:16 2015 +0100 @@ -20,6 +20,7 @@ #include <vector> #include <cmath> +using namespace std; double MathUtilities::mod(double x, double y) { @@ -56,7 +57,7 @@ *ANorm = a; } -double MathUtilities::getAlphaNorm( const std::vector <double> &data, int alpha ) +double MathUtilities::getAlphaNorm( const vector <double> &data, int alpha ) { int i; int len = data.size(); @@ -66,7 +67,6 @@ for( i = 0; i < len; i++) { temp = data[ i ]; - a += ::pow( fabs(temp), double(alpha) ); } a /= ( double )len; @@ -88,9 +88,9 @@ { if (len == 0) return 0; - std::vector<double> scratch; + vector<double> scratch; for (int i = 0; i < len; ++i) scratch.push_back(src[i]); - std::sort(scratch.begin(), scratch.end()); + sort(scratch.begin(), scratch.end()); int middle = len/2; if (len % 2 == 0) { @@ -126,7 +126,7 @@ return retVal; } -double MathUtilities::mean(const std::vector<double> &src, +double MathUtilities::mean(const vector<double> &src, int start, int count) { @@ -197,7 +197,7 @@ return index; } -int MathUtilities::getMax( const std::vector<double> & data, double* pMax ) +int MathUtilities::getMax( const vector<double> & data, double* pMax ) { int index = 0; int i; @@ -286,7 +286,7 @@ } } -void MathUtilities::normalise(std::vector<double> &data, NormaliseType type) +void MathUtilities::normalise(vector<double> &data, NormaliseType type) { switch (type) { @@ -317,20 +317,46 @@ } } -void MathUtilities::adaptiveThreshold(std::vector<double> &data) +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) { int sz = int(data.size()); if (sz == 0) return; - std::vector<double> smoothed(sz); + vector<double> smoothed(sz); int p_pre = 8; int p_post = 7; for (int i = 0; i < sz; ++i) { - int first = std::max(0, i - p_pre); - int last = std::min(sz - 1, i + p_post); + int first = max(0, i - p_pre); + int last = min(sz - 1, i + p_post); smoothed[i] = mean(data, first, last - first + 1); }