changeset 3:6565c7cb9c71

Added KL divergence and entropy
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Wed, 22 Feb 2012 22:29:43 +0000
parents c49a8f33afab
children 1ea18717ba7c
files newOFsrc/bayesianArray.cpp newOFsrc/bayesianArray.h newOFsrc/testApp.cpp newOFsrc/testApp.h
diffstat 4 files changed, 29 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/newOFsrc/bayesianArray.cpp	Wed Feb 22 22:16:48 2012 +0000
+++ b/newOFsrc/bayesianArray.cpp	Wed Feb 22 22:29:43 2012 +0000
@@ -311,3 +311,14 @@
 	return KLsum;
 }
 
+double bayesianArray::getEntropyOfPosterior(){
+	double entropy = 0;
+	for (int i = 0;i < ARRAY_SIZE;i++){
+		if (posterior[i] > 0){
+			entropy += posterior[i]*log(posterior[i]);
+		}
+	}
+	return -1*entropy;
+}
+
+
--- a/newOFsrc/bayesianArray.h	Wed Feb 22 22:16:48 2012 +0000
+++ b/newOFsrc/bayesianArray.h	Wed Feb 22 22:29:43 2012 +0000
@@ -57,6 +57,7 @@
 		double standardDeviation;
 				
 		double getKLdivergence();
+		double getEntropyOfPosterior();
 
 private:
 };
--- a/newOFsrc/testApp.cpp	Wed Feb 22 22:16:48 2012 +0000
+++ b/newOFsrc/testApp.cpp	Wed Feb 22 22:29:43 2012 +0000
@@ -54,6 +54,7 @@
 	ofSetFrameRate(60); // if vertical sync is off, we can go a bit fast... this caps the framerate at 60fps.
 
 	KLdiv = 0;
+	entropy = 0;
 /*
 	beatDistribution.initialiseArray();
 	tempoDistribution.initialiseArray();
@@ -169,6 +170,7 @@
 			if (timeNow - drumTracker.setBeatToNowTime > 1000)
 			drumTracker.newKickError(m.getArgAsFloat(0), m.getArgAsFloat(2), m.getArgAsString(1));
 			KLdiv = drumTracker.beatDistribution.getKLdivergence();
+			entropy = drumTracker.beatDistribution.getEntropyOfPosterior();
 			}//end if new error
 		
 
@@ -367,13 +369,23 @@
 		bSnapshot = false;
 	}
 	
+
+	
+	drawKLdivAndEntropy();
+	
+}//end draw
+
+
+void testApp::drawKLdivAndEntropy(){
 	ofDrawBitmapString("KLdiv :"+ofToString(KLdiv, 3), 20, 40);
 	ofSetColor(200,0,0,160);
 	ofRect(0, ofGetHeight()*(1-KLdiv), 40, ofGetHeight()*KLdiv);
 	
-	
-	
-}//end draw
+	ofSetColor(0,0,		200, 160);
+	ofDrawBitmapString("Entropy :"+ofToString(entropy, 3), 20, 60);
+	double entropyDrawFactor = 0.1;
+	ofRect(60, ofGetHeight()*(1-entropy*entropyDrawFactor), 40, ofGetHeight()*entropy*entropyDrawFactor);
+}
 
 
 void testApp::drawTempoData(){
--- a/newOFsrc/testApp.h	Wed Feb 22 22:16:48 2012 +0000
+++ b/newOFsrc/testApp.h	Wed Feb 22 22:29:43 2012 +0000
@@ -122,7 +122,8 @@
 //		string tempoUpdateStrings[16];
 //		string tempoDataString;
 
-	double KLdiv;
+	double KLdiv, entropy;
+	void drawKLdivAndEntropy();
 	private:
 
 	//	ofxOscSender sender;