diff maths/MathUtilities.cpp @ 54:5bec06ecc88a

* First cut at Matthew's downbeat estimator -- untested so far
author cannam
date Tue, 10 Feb 2009 12:52:43 +0000
parents ad645e404d0c
children 7fe29d8a7eaf
line wrap: on
line diff
--- a/maths/MathUtilities.cpp	Mon Feb 09 16:05:32 2009 +0000
+++ b/maths/MathUtilities.cpp	Tue Feb 10 12:52:43 2009 +0000
@@ -144,6 +144,20 @@
     return retVal;
 }
 
+double MathUtilities::mean(const std::vector<double> &src,
+                           unsigned int start,
+                           unsigned int count)
+{
+    double sum = 0.;
+	
+    for (int i = 0; i < count; ++i)
+    {
+        sum += src[start + i];
+    }
+
+    return sum / count;
+}
+
 void MathUtilities::getFrameMinMax(const double *data, unsigned int len, double *min, double *max)
 {
     unsigned int i;
@@ -189,7 +203,33 @@
 		
    	}
 
-	*pMax = max;
+	if (pMax) *pMax = max;
+
+
+	return index;
+}
+
+int MathUtilities::getMax( const std::vector<double> & data, double* pMax )
+{
+	unsigned int index = 0;
+	unsigned int i;
+	double temp = 0.0;
+	
+	double max = data[0];
+
+	for( i = 0; i < data.size(); i++)
+	{
+		temp = data[ i ];
+
+		if( temp > max )
+		{
+			max =  temp ;
+			index = i;
+		}
+		
+   	}
+
+	if (pMax) *pMax = max;
 
 
 	return index;
@@ -289,5 +329,28 @@
     }
 }
 
+void MathUtilities::adaptiveThreshold(std::vector<double> &data)
+{
+    int sz = int(data.size());
+    if (sz == 0) return;
+
+    std::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);
+
+        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;
+    }
+}