changeset 8:572564b7cb85

added calculation posterior into both onset and pitch processes
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Fri, 03 Feb 2012 13:28:59 +0000
parents 33dedfe32893
children bc62266af280
files bayesianArraySrc/BayesianArrayStructure.cpp bayesianArraySrc/DynamicVector.cpp src/AudioEventMatcher.cpp src/AudioEventMatcher.h src/testApp.cpp
diffstat 5 files changed, 65 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/bayesianArraySrc/BayesianArrayStructure.cpp	Thu Feb 02 21:55:51 2012 +0000
+++ b/bayesianArraySrc/BayesianArrayStructure.cpp	Fri Feb 03 13:28:59 2012 +0000
@@ -165,10 +165,9 @@
 	int zeroIndex = posterior.getRealTermsAsIndex(0);
 	printf("ZERO INDEX %i\n", zeroIndex);
 	
-	posterior.addGaussianShapeFromRealTime(0, 60, 1);//one way to add at x msec
+	posterior.addGaussianShapeFromRealTime(0, 300, 1);//one way to add at x msec
 //	posterior.addGaussianShape(posterior.getRealTermsAsIndex(10), 50, 1);//alternative way
 	
-	//posterior.addToIndex(0, 1);
 	likelihood.addConstant(1);
 	
 	updateCounter = 0;
@@ -299,8 +298,9 @@
 	updateBestEstimate(timeDifference);
 	lastBestEstimateUpdateTime = newEventTime;//getTimeNow(timePlayed);
 
+	//set TARGETS - commented tenmporarily
+	setNewDistributionOffsets(max(0., bestEstimate - (prior.scalar*prior.arraySize/2)));
 	
-	setNewDistributionOffsets(max(0., bestEstimate - (prior.scalar*prior.arraySize/2)));
 	crossUpdateArrays(posterior, relativeSpeedPosterior, timeDifference);
 	
 	//i.e. using the same offset as prior
@@ -315,10 +315,9 @@
 
 
 void BayesianArrayStructure::crossUpdateArrays(DynamicVector& position, DynamicVector& speed, double timeDifference){
+
 	//set the cutoff for offset of position first! XXX
 	
-//	printf("time difference %f, ", timeDifference);
-	
 	double timeDifferenceInPositionVectorUnits = timeDifference / prior.scalar;
 	
 	printf("CROSS UPDATE time diff %f ms is %f units; ", timeDifference, timeDifferenceInPositionVectorUnits);
@@ -335,7 +334,7 @@
 			
 
 	updateCounter++;
-	prior.renormalise();
+	prior.renormalise();//not strictly necessary??
 
 }
 
@@ -401,14 +400,19 @@
 
 void BayesianArrayStructure::translateByMaximumSpeed(const double& timeDifferenceInPositionVectorUnits){
 
+	
 	int distanceMoved, newPriorIndex;
+	double speedIndex = getSpeedEstimateIndex();
+	double speedValue = relativeSpeedPosterior.getIndexInRealTerms(speedIndex);
 	
-	double speedValue = relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate);
+//	double speedValue = relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate);
+	
 	//so for scalar 0.01, 50 -> speed value of 0.5
-	double speedContribution = relativeSpeedPosterior.array[relativeSpeedPosterior.integratedEstimate];
+	double speedContribution = relativeSpeedPosterior.array[(int)round(speedIndex)];
 		//so we have moved 
 		distanceMoved = round(timeDifferenceInPositionVectorUnits * speedValue);//round the value
-					//	printf("speed [%i] gives %f moved %i in %f units \n", i, speedValue, distanceMoved, timeDifferenceInPositionVectorUnits);
+				
+	//	printf("speed [%i] gives %f moved %i in %f units \n", i, speedValue, distanceMoved, timeDifferenceInPositionVectorUnits);
 			
 			for (int postIndex = 0;postIndex < posterior.arraySize;postIndex++){
 				//old posterior contributing to new prior
--- a/bayesianArraySrc/DynamicVector.cpp	Thu Feb 02 21:55:51 2012 +0000
+++ b/bayesianArraySrc/DynamicVector.cpp	Fri Feb 03 13:28:59 2012 +0000
@@ -375,6 +375,9 @@
 	//some lines where the bounaries are
 	ofLine(window.x + minScreenIndex, window.y + screenHeight, window.x + minScreenIndex, window.y + screenHeight/2);
 	ofLine(window.x + maxScreenIndex, window.y + screenHeight, window.x + maxScreenIndex, window.y + screenHeight/2);
+	string infoString = "max "+ofToString(maxVal);
+	infoString += "\n offset "+ofToString(offset);
+	ofDrawBitmapString(infoString, window.x + window.width/2, window.y + 15);
 	
 //	ofDrawBitmapString(ofToString(stepSize, 2)+"  "+ofToString(maxScreenIndex - minScreenIndex, 0), 20, 600);
 
--- a/src/AudioEventMatcher.cpp	Thu Feb 02 21:55:51 2012 +0000
+++ b/src/AudioEventMatcher.cpp	Fri Feb 03 13:28:59 2012 +0000
@@ -19,6 +19,7 @@
 	usingRealTime = false;
 	bayesianStruct.realTimeMode = &usingRealTime;
 	recentPitch = 0;
+	currentAlignmentPosition = 0;
 }
 
 void AudioEventMatcher::setWindowDimensions(){
@@ -46,9 +47,16 @@
 
 void  AudioEventMatcher::startPlaying(){
 	bayesianStruct.setStartPlaying();
+	currentAlignmentPosition = 0;
+	startTime = ofGetElapsedTimeMillis();
 	//bayesianStruct.posterior.printArray();
 }
 
+void AudioEventMatcher::updateBestAlignmentPosition(){
+	currentAlignmentPosition = bayesianStruct.posterior.offset + bayesianStruct.posterior.getIndexInRealTerms(bayesianStruct.posterior.MAPestimate);
+	currentAlignmentPosition += (ofGetElapsedTimeMillis() - lastAlignmentTime) * bayesianStruct.relativeSpeedPosterior.getIndexInRealTerms(bayesianStruct.relativeSpeedPosterior.MAPestimate);
+}
+
 void AudioEventMatcher::draw(){
 	//draw some outlines in blue
 	ofSetColor(20,200,200);
@@ -64,6 +72,7 @@
 //	bayesianStruct.relativeSpeedPrior.drawVector(0, 200, bayesTempoWindow);
 	
 	drawBayesianDistributions();
+	
 	//	bayesianStruct.posterior.drawVector(0, bayesianStruct.posterior.getRealTermsAsIndex(screenWidthMillis), bayesPositionWindow);
 	
 	//bayesianStruct.posterior.drawVector(bayesianStruct.posterior.getRealTermsAsIndex(0), bayesianStruct.posterior.getRealTermsAsIndex(screenWidthMillis), bayesPositionWindow);
@@ -88,14 +97,9 @@
 	
 	string tmpString = "start "+ofToString(screenStartTimeMillis)+" (index "+ofToString(startIndex)+"), end "+ofToString(screenEndTimeMillis);
 	ofDrawBitmapString(tmpString, bayesPositionWindow.x+20, bayesPositionWindow.y+20);
-
 	
+//	bayesianStruct.likelihood.drawConstrainedVector(startIndex, endIndex, 0, ofGetWidth(), bayesLikelihoodWindow);
 	
-	bayesianStruct.likelihood.drawConstrainedVector(startIndex, endIndex, 0, ofGetWidth(), bayesLikelihoodWindow);
-	
-	
-	//bayesianStruct.likelihood.drawVector(bayesianStruct.likelihood.getRealTermsAsIndex(0), bayesianStruct.likelihood.getRealTermsAsIndex(screenWidthMillis), bayesLikelihoodWindow);
-
 	bayesianStruct.relativeSpeedPosterior.drawConstrainedVector(0, bayesianStruct.relativeSpeedPosterior.arraySize, 0, ofGetWidth(), bayesTempoWindow);
 	
 	string tmpStr = "zero is "+ofToString(bayesianStruct.posterior.getRealTermsAsIndex(0));
@@ -105,15 +109,28 @@
 	tmpStr = "best est "+ofToString(bayesianStruct.bestEstimate);
 	ofDrawBitmapString(tmpStr, 20, 180);
 	
-	ofDrawBitmapString("screenamp "+ofToString(screenWidthMillis), 20, 100);
+	ofDrawBitmapString("screenwidth "+ofToString(screenWidthMillis), 20, 800);
 	
-	
+	ofSetColor(0,255,0);
+	double currentEstimateIndex = (currentAlignmentPosition - screenStartTimeMillis)*ofGetWidth()/screenWidthMillis;
+	ofLine(currentEstimateIndex, bayesPositionWindow.y, currentEstimateIndex, bayesPositionWindow.y + bayesPositionWindow.height);
 	
 	//draw track by track likelihoods
 	for (int i = 0; i <recordedTracks.numberOfAudioTracks;i++){
 		ofSetColor(200,255,50);
-		likelihoodVisualisation[i].drawConstrainedVector(startIndex, endIndex, 0, ofGetWidth(), recordedTracks.loadedAudioFiles[i].fileLoader.onsetDetect.window);		
+		likelihoodVisualisation[i].drawConstrainedVector(likelihoodVisualisation[i].getRealTermsAsIndex(screenStartTimeMillis), likelihoodVisualisation[i].getRealTermsAsIndex(screenEndTimeMillis), 0, ofGetWidth(), recordedTracks.loadedAudioFiles[i].fileLoader.onsetDetect.window);		
+		ofSetColor(255);
+		ofDrawBitmapString("recent event "+ofToString(recentEventTime[i]), recordedTracks.loadedAudioFiles[i].fileLoader.onsetDetect.window.x + 20, recordedTracks.loadedAudioFiles[i].fileLoader.onsetDetect.window.y + recordedTracks.loadedAudioFiles[i].fileLoader.onsetDetect.window.height - 10);
 	}
+
+	int priorStartIndex = recentPrior.getRealTermsAsIndex(screenStartTimeMillis);
+	int priorEndIndex = recentPrior.getRealTermsAsIndex(screenEndTimeMillis);
+	ofSetColor(0,200,200);
+	recentPrior.drawConstrainedVector(priorStartIndex, priorEndIndex, 0, ofGetWidth(), bayesPositionWindow);
+
+//	bayesianStruct.prior.addTriangularShape(100, 20, 0.4);
+	ofSetColor(255,0,100);
+	bayesianStruct.prior.drawConstrainedVector(bayesianStruct.prior.getRealTermsAsIndex(screenStartTimeMillis), bayesianStruct.prior.getRealTermsAsIndex(screenEndTimeMillis), 0, ofGetWidth(), bayesLikelihoodWindow);
 	
 }
 
@@ -133,6 +150,8 @@
 	recentPitch = pitchIn;//for drawing
 	recentTime = timeIn;
 	}
+	
+	
 }
 
 void AudioEventMatcher::newKickEvent(const double& timeIn){	
@@ -166,7 +185,7 @@
 	bayesianStruct.updateBayesianDistributions(timeIn);//moves the posterior up into prior given the time interval and calculates new offsets
 	
 	//start at beginning but OPTIMISE later
-	double onsetLikelihoodToNoise = 0.3;
+	double onsetLikelihoodToNoise = 0.5;
 	
 	double likelihoodWidth = 40;
 	
@@ -194,10 +213,12 @@
 	
 //	bayesianStruct.likelihood.addConstant((1-likelihoodToNoiseRatio)/bayesianStruct.likelihood.length);
 	bayesianStruct.likelihood.addConstant(numberOfMatchesFound*(1-onsetLikelihoodToNoise)/(onsetLikelihoodToNoise*bayesianStruct.likelihood.length));
-
 	bayesianStruct.likelihood.renormalise();
 	
-		
+	bayesianStruct.calculatePosterior();
+	
+	lastAlignmentTime = ofGetElapsedTimeMillis();
+	recentEventTime[channel] = ofGetElapsedTimeMillis() - startTime;
 	
 }
 
@@ -208,10 +229,10 @@
 	
 	
 	bayesianStruct.updateBayesianDistributions(timeIn);//moves the posterior up into prior given the time interval and calculates new offsets
-	
+
 	//set the lielihoods by matching the pitched note
 	
-	double pitchLikelihoodToNoise = 0.6;//more noise
+	double pitchLikelihoodToNoise = 0.5;//more noise
 	int numberOfMatches = 0;
 	bayesianStruct.likelihood.zero();//set to zero
 	
@@ -232,6 +253,9 @@
 		}
 	}
 
+	recentPrior = bayesianStruct.prior;
+	
+	
 	if (numberOfMatches > 0){//no point updating unless there is a match
 	
 		bayesianStruct.likelihood.addConstant(numberOfMatches*(1-pitchLikelihoodToNoise)/(pitchLikelihoodToNoise*bayesianStruct.likelihood.length));
@@ -243,6 +267,8 @@
 		bayesianStruct.calculatePosterior();
 	}
 	
+	lastAlignmentTime = ofGetElapsedTimeMillis();
+	recentEventTime[channel] = ofGetElapsedTimeMillis() - startTime;
 }
 
 double AudioEventMatcher::getPitchDistance(const double& pitchOne, const double& pitchTwo, const double& scale){
--- a/src/AudioEventMatcher.h	Thu Feb 02 21:55:51 2012 +0000
+++ b/src/AudioEventMatcher.h	Fri Feb 03 13:28:59 2012 +0000
@@ -28,6 +28,8 @@
 	
 	void setArraySizes();
 	
+	void updateBestAlignmentPosition();
+	
 	void draw();
 	void drawBayesianDistributions();;
 	void setWindowDimensions();
@@ -62,6 +64,12 @@
 	
 	DynamicVector likelihoodVisualisation[3];
 	DynamicVector recentPrior;
+	
+	double currentAlignmentPosition;
+	double lastAlignmentTime;
+	
+	double recentEventTime[3];
+	double startTime;
 
 };
 #endif
--- a/src/testApp.cpp	Thu Feb 02 21:55:51 2012 +0000
+++ b/src/testApp.cpp	Fri Feb 03 13:28:59 2012 +0000
@@ -40,7 +40,7 @@
 void testApp::update(){
 
 	eventMatcher.recordedTracks.updatePosition();
-	
+	eventMatcher.updateBestAlignmentPosition();
 	checkForOSCmessages();
 	
 }