changeset 4:45b5cf9be377

checking through Bayesian update procedure - working without cross update method.
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Thu, 02 Feb 2012 12:13:44 +0000
parents 5e188c0035b6
children 5ef00d1dfe68
files README.txt bayesianArraySrc/BayesianArrayStructure.cpp bayesianArraySrc/BayesianArrayStructure.h bayesianArraySrc/DynamicVector.cpp bayesianArraySrc/DynamicVector.h bayesianArraySrc/midiEventHolder.cpp src/AudioEventMatcher.cpp src/AudioEventMatcher.h
diffstat 8 files changed, 127 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/README.txt	Wed Feb 01 16:05:26 2012 +0000
+++ b/README.txt	Thu Feb 02 12:13:44 2012 +0000
@@ -19,3 +19,8 @@
 
 Examine yin and audio so that pitch algorithm is CORRECT!
 
+
+DEBUG and Problems to check
+Looking at offset correction - appears offset changes after bestUpdate but our MAP and dustbin might be for the last offset
+
+changing crossupdate threshold UP for tmp bug
--- a/bayesianArraySrc/BayesianArrayStructure.cpp	Wed Feb 01 16:05:26 2012 +0000
+++ b/bayesianArraySrc/BayesianArrayStructure.cpp	Thu Feb 02 12:13:44 2012 +0000
@@ -15,7 +15,7 @@
 BayesianArrayStructure::BayesianArrayStructure(){
 	printf("Bayesian structure: DeFault constructor called");
 
-	usingIntegratedTempoEstimate = true;// use max index
+	usingIntegratedTempoEstimate = false;// use max index
 	
 	relativeSpeedLikelihoodStdDev = 5.0;
 	
@@ -29,7 +29,7 @@
 	lastEventTime = 0;//ofGetElapsedTimeMillis();
 
 	tmpBestEstimate = 0;
-	crossUpdateTimeThreshold = 60;
+	crossUpdateTimeThreshold = 60000;
 	priorWidth = 20;
 	
 }
@@ -103,6 +103,9 @@
 	int index = relativeSpeedPosterior.getRealTermsAsIndex(speedPriorValue);
 	relativeSpeedPosterior.zero();
 	relativeSpeedPosterior.addGaussianShape(index, priorWidth, 0.8);
+	printf("speed adding to index for 1 = %f\n", relativeSpeedPosterior.getRealTermsAsIndex(1));
+	relativeSpeedPosterior.addToIndex(relativeSpeedPosterior.getRealTermsAsIndex(1), 1);
+	
 	relativeSpeedPosterior.renormalise();
 	relativeSpeedPosterior.getMaximum();
 	relativeSpeedPrior.copyFromDynamicVector(relativeSpeedPosterior);
@@ -188,7 +191,7 @@
 
 }
 
-
+/*
 void BayesianArrayStructure::updateTmpBestEstimate(const double& timeDifference){
 	//input is the time since the start of playing
 //	double timeDiff = ofGetElapsedTimeMillis() - lastEventTime;//lastBestEstimateUpdateTime;
@@ -202,11 +205,12 @@
 	//printf("tmp best %f and best %f time diff %f posterior MAP %f at speed %f\n", 0Estimate, bestEstimate, timeDifference, posterior.getIndexInRealTerms(posterior.MAPestimate), relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate));
 	//lastBestEstimateUpdateTime = ofGetElapsedTimeMillis();
 }
-	
+*/
+
 void BayesianArrayStructure::updateBestEstimate(const double& timeDifference){
 //	double timeDiff = ofGetElapsedTimeMillis() - lastEventTime;//
 	double tmp = bestEstimate;
-	printf("post offest %i\n", posterior.offset);
+	printf("best est routine: posterior offset %f\n", posterior.offset);
 	
 	double timeDiff = timeDifference;
 	
@@ -216,21 +220,24 @@
 		timeDiff = ofGetElapsedTimeMillis() - lastBestEstimateUpdateTime;
 	
 	//lastbest is time we started playing
-	
+	/*
 	if (usingIntegratedTempoEstimate)
 		speedEstimateIndex = relativeSpeedPosterior.getIntegratedEstimate();
 	else
-		speedEstimateIndex = relativeSpeedPosterior.MAPestimate;
+		speedEstimateIndex = relativeSpeedPosterior.getMAPestimate();
+	*/
+	speedEstimateIndex = getSpeedEstimateIndex();
 	
 	speedEstimate = relativeSpeedPosterior.getIndexInRealTerms(speedEstimateIndex);
 	bestEstimate = posterior.getIndexInRealTerms(posterior.MAPestimate) + timeDiff*speedEstimate;
 	
-	printf("best estimate update from %f to %f; time diff %f MAP %i = %f ms speed %f\n", tmp, bestEstimate, timeDiff,
-		   posterior.MAPestimate, posterior.getIndexInRealTerms(posterior.MAPestimate), speedEstimate);
+	printf("best estimate update from %f to %f; time diff %f MAP %i = %f ms speed index %f est %f SpeedxTime %f\n", tmp, bestEstimate, timeDiff,
+		   posterior.MAPestimate, posterior.getIndexInRealTerms(posterior.MAPestimate), speedEstimateIndex, speedEstimate, timeDiff*speedEstimate);
 }
 
 void BayesianArrayStructure::calculatePosterior(){
 	//posterior.doProduct(prior, likelihood);
+	assert(posterior.offset == prior.offset);
 	
 	int i;
 	for (i = 0;i < posterior.length;i++){
@@ -242,11 +249,21 @@
 }
 
 
+double  BayesianArrayStructure::getSpeedEstimateIndex(){
+	if (usingIntegratedTempoEstimate)
+		return relativeSpeedPosterior.getIntegratedEstimate();
+	else
+		return relativeSpeedPosterior.getMAPestimate();
+}
 
 
 void BayesianArrayStructure::setNewDistributionOffsets(const double& newOffset){
+	
+	printf("prior offset was %f now %f\n", prior.offset, newOffset);
+	
 	prior.offset = newOffset;
 	likelihood.offset = newOffset;
+	
 	//posterior.offset = newOffset;
 }
 
@@ -258,9 +275,12 @@
 	
 	double timeDifferenceInPositionVectorUnits = timeDifference / prior.scalar;
 	
-
+	printf("CROSS UPDATE time diff %f ms is %f units; ", timeDifference, timeDifferenceInPositionVectorUnits);
 	prior.zero();//kill prior
-	calculateNewPriorOffset(timeDifference);//set new prior offset here
+	
+//	calculateNewPriorOffset(timeDifference);//dioesnt do anything
+	
+	printf("new prior offset %f and post offset %f\n", prior.offset, posterior.offset);
 	
 	if (timeDifferenceInPositionVectorUnits > crossUpdateTimeThreshold)
 		complexCrossUpdate(timeDifferenceInPositionVectorUnits);
@@ -274,6 +294,7 @@
 }
 
 void BayesianArrayStructure::complexCrossUpdate(const double& timeDifferenceInPositionVectorUnits){
+
 	int distanceMoved, newPriorIndex;
 	
 	double speedValue = relativeSpeedPosterior.offset;
@@ -286,21 +307,31 @@
 		distanceMoved = round(timeDifferenceInPositionVectorUnits * speedValue);//round the value
 	
 		if (relativeSpeedPosterior.array[i] != 0){
+			
 			double speedContribution = relativeSpeedPosterior.array[i];
-			//	printf("speed [%i] gives %f moved %i in %f units \n", i, speedValue, distanceMoved, timeDifferenceInPositionVectorUnits);
 			
-			newPriorIndex = posterior.offset - prior.offset + distanceMoved;
+		//	printf("speed [%i](val[%f]) gives %f moved %i in %f units \n", i, relativeSpeedPosterior.array[i], speedValue, distanceMoved, timeDifferenceInPositionVectorUnits);
 			
-			for (int postIndex = 0;postIndex < posterior.arraySize;postIndex++){
+			//1/2/12 deleted line
+			newPriorIndex = posterior.offset - prior.offset + distanceMoved;//i.e. where post[0] goes to in terms of prior at this speed
+			int postIndex = 0;
+			while (postIndex < posterior.arraySize && newPriorIndex < prior.arraySize){
+
+			//did use a for loop
+			//	for (postIndex = 0;postIndex < posterior.arraySize;postIndex++){
 				//old posterior contributing to new prior
-			//	newPriorIndex = postIndex + posterior.offset - prior.offset + distanceMoved;
+			
+				//would use this method
+				//newPriorIndex = postIndex + posterior.offset - prior.offset + distanceMoved;
 				
-				if (newPriorIndex >= 0 && newPriorIndex < prior.arraySize){
+				if (newPriorIndex >= 0){
 					prior.addToIndex(newPriorIndex, posterior.array[postIndex]*speedContribution);
+			//		printf("speed index %i new prior index %i post val %f speed contrib %f dist %i\n", i, newPriorIndex, posterior.array[postIndex], speedContribution, distanceMoved);
 				}
-				
-				newPriorIndex++;//optimised code - the commented line above explains how this works
-			}//end for
+				//but we actually do this for simplicity
+				newPriorIndex++;
+				postIndex++;
+			}//end for. now while
 			
 			
 		}//if not zero
@@ -363,7 +394,9 @@
 
 void BayesianArrayStructure::calculateNewPriorOffset(const double& timeDifference){
 	
-	double maxSpeed = relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate);
+//	double maxSpeed = relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate);
+	
+	double maxSpeed = relativeSpeedPosterior.getIndexInRealTerms(getSpeedEstimateIndex());//either integrated or MAP
 	//	printf("Maxspeed is %f\n", maxSpeed);
 	
 	double priorMax = posterior.getMaximum();
--- a/bayesianArraySrc/BayesianArrayStructure.h	Wed Feb 01 16:05:26 2012 +0000
+++ b/bayesianArraySrc/BayesianArrayStructure.h	Thu Feb 02 12:13:44 2012 +0000
@@ -54,7 +54,8 @@
 	DynamicVector acceleration;
 	
 	double tmpBestEstimate;
-	void updateTmpBestEstimate(const double& timeDifference);
+
+//	void updateTmpBestEstimate(const double& timeDifference);
 	
 	int updateCounter;
 	
@@ -68,6 +69,7 @@
 	void updateBestEstimate(const double& timeDifference);
 	double lastBestEstimateUpdateTime;
 	double speedEstimate, speedEstimateIndex;
+	double getSpeedEstimateIndex();
 	
 	double speedDecayWidth, speedDecayAmount;
 	void decaySpeedDistribution(double timeDifference);
--- a/bayesianArraySrc/DynamicVector.cpp	Wed Feb 01 16:05:26 2012 +0000
+++ b/bayesianArraySrc/DynamicVector.cpp	Thu Feb 02 12:13:44 2012 +0000
@@ -64,6 +64,19 @@
 	return max;
 }
 
+int DynamicVector::getMAPestimate(){
+	int i;
+	double max = 0;
+	for (i=0;i < length;i++){	
+		if (array[i] > max){
+			max = array[i];
+			MAPestimate = i;
+		}
+	}
+	maximumValue = max;
+	return MAPestimate;
+}
+
 double DynamicVector::getIntegratedEstimate(){
 	//returns the index of the integrated average - where the probability distribution is centred
 	integratedEstimate = 0;
--- a/bayesianArraySrc/DynamicVector.h	Wed Feb 01 16:05:26 2012 +0000
+++ b/bayesianArraySrc/DynamicVector.h	Thu Feb 02 12:13:44 2012 +0000
@@ -27,6 +27,7 @@
 	typedef std::vector<double> DoubleVector;
 	DoubleVector array;
 	double getMaximum();
+	int getMAPestimate();
 	double getIntegratedEstimate();
 	double getLookupIndex(const int& i, const double& mean, const double& StdDev);
 	void addGaussianShapeByLookupTable(double& mean, double& StdDev, double factor);
--- a/bayesianArraySrc/midiEventHolder.cpp	Wed Feb 01 16:05:26 2012 +0000
+++ b/bayesianArraySrc/midiEventHolder.cpp	Thu Feb 02 12:13:44 2012 +0000
@@ -191,10 +191,10 @@
 //	tempoSpeedString = "";
 	
 	//MOVE INTO BAYESSTRUCT?? XXX
-	//bayesStruct.copyPriorToPosterior();
+
 	//why was this here??
 	bayesStruct.prior.copyFromDynamicVector(bayesStruct.posterior);//try the otehr way
-	//bayesStruct.copyPriorToPosterior();
+
 	//need to get new MAP position and set the offset of the arrays
 	//currently bestEstimate is the approx for the new MAP position
 	
--- a/src/AudioEventMatcher.cpp	Wed Feb 01 16:05:26 2012 +0000
+++ b/src/AudioEventMatcher.cpp	Thu Feb 02 12:13:44 2012 +0000
@@ -52,12 +52,16 @@
 	
 	double screenWidthMillis = recordedTracks.loadedAudioFiles[0].fileLoader.onsetDetect.framesToMillis(recordedTracks.loadedAudioFiles[0].fileLoader.onsetDetect.amplitudeNumber);
 	
+	bayesianStruct.posterior.drawVector(0, bayesianStruct.posterior.getRealTermsAsIndex(screenWidthMillis), bayesPositionWindow);
+	
+//	bayesianStruct.posterior.drawVector(bayesianStruct.posterior.getRealTermsAsIndex(0), bayesianStruct.posterior.getRealTermsAsIndex(screenWidthMillis), bayesPositionWindow);
+
+	
 	bayesianStruct.likelihood.drawVector(bayesianStruct.likelihood.getRealTermsAsIndex(0), bayesianStruct.likelihood.getRealTermsAsIndex(screenWidthMillis), bayesLikelihoodWindow);
 	
 	bayesianStruct.relativeSpeedPosterior.drawVector(0, bayesianStruct.relativeSpeedPosterior.getRealTermsAsIndex(2), bayesTempoWindow);
 	
-	bayesianStruct.posterior.drawVector(bayesianStruct.posterior.getRealTermsAsIndex(0), bayesianStruct.posterior.getRealTermsAsIndex(screenWidthMillis), bayesPositionWindow);
-	
+		
 	string tmpStr = "zero is "+ofToString(bayesianStruct.posterior.getRealTermsAsIndex(0));
 	tmpStr += " offsetis "+ofToString(bayesianStruct.posterior.offset);
 	tmpStr += " screenWidth = "+ofToString(bayesianStruct.posterior.getRealTermsAsIndex(screenWidthMillis));
@@ -73,6 +77,12 @@
 
 void AudioEventMatcher::newPitchEvent(const double& pitchIn, const double& timeIn){
 	liveInput.addPitchEvent(pitchIn, timeIn);
+	
+	//tmp print stuff
+	printf("New pitch MAP post estimate now %i, ", bayesianStruct.posterior.MAPestimate);
+	double tmp  = bayesianStruct.posterior.getMAPestimate();
+	printf(" getting it %f and offset %f == %f ms\n", tmp,  bayesianStruct.posterior.offset, bayesianStruct.posterior.getIndexInRealTerms(tmp));
+	
 	matchNewPitchEvent(0, pitchIn, timeIn);
 }
 
@@ -159,6 +169,18 @@
 	bayesianStruct.likelihood.addConstant(numberOfMatches*(1-pitchLikelihoodToNoise)/(pitchLikelihoodToNoise*bayesianStruct.likelihood.length));
 	
 	recordedTracks.recentPitch = pitchIn;
+	
+
+	//tmp set likelihood constant and calculate using that
+	bayesianStruct.likelihood.zero();
+	bayesianStruct.likelihood.addConstant(1);
+	
+	bayesianStruct.calculatePosterior();
+	
+	//tmp print stuff
+	printf("After CALC");
+	printPostOffset();	
+	
 }
 
 double AudioEventMatcher::getPitchDistance(const double& pitchOne, const double& pitchTwo, const double& scale){
@@ -195,34 +217,49 @@
 void  AudioEventMatcher::updateBayesianDistributions(const double& newEventTime){
 	//MOVE INTO bayesianStruct?? XX
 	
-
-	
 	//NEED TO CHECK HERE THAT THEY HAVE THE SAME OFFSETS
 	bayesianStruct.prior.copyFromDynamicVector(bayesianStruct.posterior);//try the otehr way
 
 	//bayesianStruct.copyPriorToPosterior();
 	//need to get new MAP position and set the offset of the arrays
 	//currently bestEstimate is the approx for the new MAP position
-		
+	int tmpMap = bayesianStruct.posterior.getMAPestimate();
+	
 	double timeDifference = newEventTime - bayesianStruct.lastEventTime; 
-	printf("updating distributions at time %f diff %f\n", newEventTime, timeDifference);
+	printf("updating distributions at time %f diff %f offset %f tmpmap est %i\n", newEventTime, timeDifference, bayesianStruct.posterior.offset, tmpMap);
 	
 	//addnoise to the tempo distribution
 	//bayesianStruct.decaySpeedDistribution(timeDifference);
-	
+
 	if (timeDifference > 50){
 		bayesianStruct.addGaussianNoiseToSpeedPosterior(timeDifference * 10.0 / 100.);
 	}
 	
 	bayesianStruct.updateBestEstimate(timeDifference);
-	
 	bayesianStruct.lastBestEstimateUpdateTime = newEventTime;//getTimeNow(timePlayed);
 		
+//	printf("set new distrb max 0 or %f\n", bayesianStruct.bestEstimate - (bayesianStruct.prior.scalar*bayesianStruct.prior.arraySize/2));
+	
+	//tmp print stuff
+	
+	printf("HALFWAY BEST ");
+	printPostOffset();
+	
 	bayesianStruct.setNewDistributionOffsets(max(0., bayesianStruct.bestEstimate - (bayesianStruct.prior.scalar*bayesianStruct.prior.arraySize/2)));
-	
 	bayesianStruct.crossUpdateArrays(bayesianStruct.posterior, bayesianStruct.relativeSpeedPosterior, timeDifference);
 	
-	bayesianStruct.posterior.offset = max(0., bayesianStruct.bestEstimate - (bayesianStruct.prior.scalar*bayesianStruct.prior.arraySize/2));//	bayesianStruct.prior.offset = max(0., bayesianStruct.bestEstimate - (bayesianStruct.prior.scalar*bayesianStruct.prior.arraySize/2));
-		
+	//i.e. using the same offset as prior
+	bayesianStruct.posterior.offset = bayesianStruct.prior.offset;// 
+
+//	float tmpPrior = max(0., bayesianStruct.bestEstimate - (bayesianStruct.prior.scalar*bayesianStruct.prior.arraySize/2));//	bayesianStruct.prior.offset = max(0., bayesianStruct.bestEstimate - (bayesianStruct.prior.scalar*bayesianStruct.prior.arraySize/2));
+//	printf("Using prior offset of %f not %f\n", tmpPrior, bayesianStruct.prior.offset);
+	
 	bayesianStruct.lastEventTime = newEventTime;//bayesianStruct.lastEventTime = ofGetElapsedTimeMillis();
+
+
 }
+
+void AudioEventMatcher::printPostOffset(){
+	double tmp = bayesianStruct.posterior.getMAPestimate();
+	printf(" MAP index %i post offset %f == %f ms\n", bayesianStruct.posterior.MAPestimate, bayesianStruct.posterior.offset, bayesianStruct.posterior.getIndexInRealTerms(bayesianStruct.posterior.MAPestimate));
+}
\ No newline at end of file
--- a/src/AudioEventMatcher.h	Wed Feb 01 16:05:26 2012 +0000
+++ b/src/AudioEventMatcher.h	Thu Feb 02 12:13:44 2012 +0000
@@ -55,5 +55,7 @@
 	
 	bool usingRealTime;
 	
+	//tmp debug fn
+	void printPostOffset();
 };
 #endif