Mercurial > hg > bayesian-drum-tracker
changeset 6:fc095148e2a8
sending out the KL information that cognitive dynamic paper will be using
author | Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk> |
---|---|
date | Tue, 06 Mar 2012 17:04:16 +0000 |
parents | 4288150c257b |
children | 3d30a79c0312 |
files | newOFsrc/BayesDrumTracker.cpp newOFsrc/bayesianArray.cpp newOFsrc/bayesianArray.h newOFsrc/testApp.cpp newOFsrc/testApp.h |
diffstat | 5 files changed, 120 insertions(+), 102 deletions(-) [+] |
line wrap: on
line diff
--- a/newOFsrc/BayesDrumTracker.cpp Tue Mar 06 14:15:35 2012 +0000 +++ b/newOFsrc/BayesDrumTracker.cpp Tue Mar 06 17:04:16 2012 +0000 @@ -304,7 +304,7 @@ m.addFloatArg(maxPhase); beatTimes.tatum = maxTempo; - printf("BEAT INFO %f, %f\n", beatTimes.tatum, maxPhase); +// printf("BEAT INFO %f, %f\n", beatTimes.tatum, maxPhase); sender.sendMessage( m ); @@ -320,7 +320,7 @@ maxTempo += tempoMinimum; beatTimes.tatum = maxTempo; - printf("SEND TATUM %f\n", beatTimes.tatum); +// printf("SEND TATUM %f\n", beatTimes.tatum); m.addFloatArg( maxTempo ); sender.sendMessage( m );
--- a/newOFsrc/bayesianArray.cpp Tue Mar 06 14:15:35 2012 +0000 +++ b/newOFsrc/bayesianArray.cpp Tue Mar 06 17:04:16 2012 +0000 @@ -13,16 +13,16 @@ bayesianArray::bayesianArray(){ likelihoodNoise = 0.5; - likelihoodMean = ARRAY_SIZE/2; - likelihoodStdDev = ARRAY_SIZE / 12; + likelihoodMean = arraySize/2; + likelihoodStdDev = arraySize / 12; initialiseArray(); } void bayesianArray::initialiseArray(){ //maximumIndex = 12;//change this - setGaussianPrior(ARRAY_SIZE/2, ARRAY_SIZE/1); - setGaussianLikelihood(ARRAY_SIZE/2, ARRAY_SIZE/1);//likelihoodMean, likelihoodStdDev); + setGaussianPrior(arraySize/2, arraySize/1); + setGaussianLikelihood(arraySize/2, arraySize/1);//likelihoodMean, likelihoodStdDev); calculatePosterior(); renormalisePosterior(); @@ -32,9 +32,9 @@ earlySixteenthNoteProportion = 0; lateSixteenthNoteProportion = 0; decayNoiseAmount = 0.1; - decayNoiseStdDev = ARRAY_SIZE/24; + decayNoiseStdDev = arraySize/24; standardDeviation = likelihoodStdDev; - setDecayNoiseGaussian(ARRAY_SIZE/2, decayNoiseStdDev); + setDecayNoiseGaussian(arraySize/2, decayNoiseStdDev); setGaussianLikelihood(likelihoodMean, likelihoodStdDev); } @@ -42,7 +42,7 @@ void bayesianArray::setGaussianPrior(float mean, float StdDev){ int i; - for (i=0;i<ARRAY_SIZE;i++){ + for (i=0;i<arraySize;i++){ prior[i] = (1/(StdDev*sqrt(2*PI)))*exp(-1*(i-mean)*(i-mean)/(2*StdDev*StdDev)); //posterior[i] = prior[i]; } @@ -50,7 +50,7 @@ void bayesianArray::setGaussianPosterior(float mean, float StdDev){ int i; - for (i=0;i<ARRAY_SIZE;i++){ + for (i=0;i<arraySize;i++){ posterior[i] = (1/(StdDev*sqrt(2*PI)))*exp(-1*(i-mean)*(i-mean)/(2*StdDev*StdDev)); } } @@ -58,72 +58,72 @@ void bayesianArray::setGaussianLikelihoodForBeats(float mean, float StdDev){ //this has eighth and sixteenth positions included -if (mean >= 0 && mean <= ARRAY_SIZE){ +if (mean >= 0 && mean <= arraySize){ int i; float eighthDifference; //main beat position plus these three: -int eighthPosition = ((int)mean + ARRAY_SIZE/2)%ARRAY_SIZE; -int earlySixteenthPosition = ((int)mean + (3*ARRAY_SIZE/4))%ARRAY_SIZE;; -int lateSixteenthPosition = ((int)mean + (ARRAY_SIZE/4))%ARRAY_SIZE;; +int eighthPosition = ((int)mean + arraySize/2)%arraySize; +int earlySixteenthPosition = ((int)mean + (3*arraySize/4))%arraySize;; +int lateSixteenthPosition = ((int)mean + (arraySize/4))%arraySize;; float mainDifference, sixteenthDifference; float gaussianProportion = 1 - likelihoodNoise; float mainProportion = (1 - eighthNoteProportion - earlySixteenthNoteProportion - lateSixteenthNoteProportion); - for (i=0;i < ARRAY_SIZE;i++){ + for (i=0;i < arraySize;i++){ - mainDifference = min( fabs(i-mean) , (double)(i + ARRAY_SIZE - mean)); + mainDifference = min( fabs(i-mean) , (double)(i + arraySize - mean)); likelihood[i] = gaussianProportion * mainProportion * (1/(StdDev*sqrt(2*PI)))*exp(-1*(mainDifference)*(mainDifference)/(2*StdDev*StdDev)) ; - eighthDifference = min( abs(i - eighthPosition) , i + ARRAY_SIZE - eighthPosition); - eighthDifference = min(eighthDifference , (float)(ARRAY_SIZE + eighthPosition - i )); + eighthDifference = min( abs(i - eighthPosition) , i + arraySize - eighthPosition); + eighthDifference = min(eighthDifference , (float)(arraySize + eighthPosition - i )); //for e.g. +0.43, or -0.47 we require the gaussian around the half note too likelihood[i] += gaussianProportion * eighthNoteProportion * (1/(StdDev*sqrt(2*PI)))*exp(-1*(eighthDifference)*(eighthDifference)/(2*StdDev*StdDev)) ; - sixteenthDifference = min( abs(i - earlySixteenthPosition) , i + ARRAY_SIZE - earlySixteenthPosition); - sixteenthDifference = min(sixteenthDifference , (float)(ARRAY_SIZE + earlySixteenthPosition - i )); + sixteenthDifference = min( abs(i - earlySixteenthPosition) , i + arraySize - earlySixteenthPosition); + sixteenthDifference = min(sixteenthDifference , (float)(arraySize + earlySixteenthPosition - i )); //for e.g. +0.43, or -0.47 we require the gaussian around the half note too likelihood[i] += gaussianProportion * earlySixteenthNoteProportion * (1/(StdDev*sqrt(2*PI)))*exp(-1*(sixteenthDifference)*(sixteenthDifference)/(2*StdDev*StdDev)) ; - sixteenthDifference = min( abs(i - lateSixteenthPosition) , i + ARRAY_SIZE - lateSixteenthPosition); - sixteenthDifference = min(sixteenthDifference , (float)(ARRAY_SIZE + lateSixteenthPosition - i )); + sixteenthDifference = min( abs(i - lateSixteenthPosition) , i + arraySize - lateSixteenthPosition); + sixteenthDifference = min(sixteenthDifference , (float)(arraySize + lateSixteenthPosition - i )); //for e.g. +0.43, or -0.47 we require the gaussian around the half note too likelihood[i] += gaussianProportion * lateSixteenthNoteProportion * (1/(StdDev*sqrt(2*PI)))*exp(-1*(sixteenthDifference)*(sixteenthDifference)/(2*StdDev*StdDev)) ; - likelihood[i] += (likelihoodNoise / ARRAY_SIZE); + likelihood[i] += (likelihoodNoise / arraySize); //likelihood[i] = (float) max(gaussianProportion * (1/(StdDev*sqrt(2*PI)))*exp(-1*(i-mean)*(i-mean)/(2*StdDev*StdDev)) , - //(double) (likelihoodNoise / ARRAY_SIZE) ); + //(double) (likelihoodNoise / arraySize) ); } -// renormaliseArray(&likelihood[0], ARRAY_SIZE); +// renormaliseArray(&likelihood[0], arraySize); }//end if mean within limits } void bayesianArray::setGaussianLikelihood(float mean, float StdDev){ -if (mean >= 0 && mean <= ARRAY_SIZE){ +if (mean >= 0 && mean <= arraySize){ int i; float eighthDifference; -int eighthPosition = ((int)mean + ARRAY_SIZE/2)%ARRAY_SIZE; +int eighthPosition = ((int)mean + arraySize/2)%arraySize; float mainDifference; float gaussianProportion = 1 - likelihoodNoise; - for (i=0;i < ARRAY_SIZE;i++){ + for (i=0;i < arraySize;i++){ - mainDifference = min( fabs(i-mean) , (double)(i + ARRAY_SIZE - mean)); + mainDifference = min( fabs(i-mean) , (double)(i + arraySize - mean)); //without * (1 - eighthNoteProportion) likelihood[i] = gaussianProportion * (1/(StdDev*sqrt(2*PI)))*exp(-1*(mainDifference)*(mainDifference)/(2*StdDev*StdDev)) ; - likelihood[i] += (likelihoodNoise / ARRAY_SIZE); + likelihood[i] += (likelihoodNoise / arraySize); //likelihood[i] = (float) max(gaussianProportion * (1/(StdDev*sqrt(2*PI)))*exp(-1*(i-mean)*(i-mean)/(2*StdDev*StdDev)) , - //(double) (likelihoodNoise / ARRAY_SIZE) ); + //(double) (likelihoodNoise / arraySize) ); } -// renormaliseArray(&likelihood[0], ARRAY_SIZE); +// renormaliseArray(&likelihood[0], arraySize); }//end if mean within limits } void bayesianArray::calculatePosterior(){ int i; - for (i=0;i < ARRAY_SIZE;i++){ + for (i=0;i < arraySize;i++){ posterior[i] = likelihood[i] * prior[i]; } //renormalisePosterior(); @@ -165,7 +165,7 @@ float integratedQuantity = 0; float integratedTotal = 0; double integratedIndex = 0; - for (i=0;i < ARRAY_SIZE;i++){ + for (i=0;i < arraySize;i++){ integratedQuantity += posterior[i];//the values of the probability distribution integratedTotal += i*posterior[i]; } @@ -182,7 +182,7 @@ double total = 0; double pdfSum; double variance = 0; - for (int i=0;i < ARRAY_SIZE;i++){ + for (int i=0;i < arraySize;i++){ //*posterior[i] * total += posterior[i] * (i - integratedEstimate) * (i - integratedEstimate);//the values of the probability distribution pdfSum += posterior[i]; @@ -191,7 +191,7 @@ if (pdfSum > 0) variance = total / pdfSum; else - variance = ARRAY_SIZE; + variance = arraySize; standardDeviation = sqrt(variance); return standardDeviation; @@ -214,7 +214,7 @@ void bayesianArray::resetPrior(){ int i; - for (i=0;i<ARRAY_SIZE;i++){ + for (i=0;i<arraySize;i++){ prior[i] = posterior[i]; } } @@ -222,11 +222,11 @@ void bayesianArray::renormalisePrior(){ int i; float totalArea = 0; - for (i=0;i < ARRAY_SIZE;i++){ + for (i=0;i < arraySize;i++){ totalArea += prior[i]; } - for (i=0;i < ARRAY_SIZE;i++){ + for (i=0;i < arraySize;i++){ prior[i] /= totalArea; } } @@ -234,22 +234,22 @@ void bayesianArray::renormalisePosterior(){ int i; float totalArea = 0; - for (i=0;i < ARRAY_SIZE;i++){ + for (i=0;i < arraySize;i++){ totalArea += posterior[i]; } - for (i=0;i < ARRAY_SIZE;i++){ + for (i=0;i < arraySize;i++){ posterior[i] /= totalArea; } } void bayesianArray::decayPosterior(){ float *pointer; - pointer = getMaximumEstimate(&posterior[0], ARRAY_SIZE); + pointer = getMaximumEstimate(&posterior[0], arraySize); float maximum; maximum = *pointer; int i; - for (i=0;i<ARRAY_SIZE;i++){ + for (i=0;i<arraySize;i++){ posterior[i] += (maximum - posterior[i]) * posteriorDecayRate * 0.01;;//usded to be * maximum not minus value } maximumIndex = *(pointer+1); @@ -257,7 +257,7 @@ void bayesianArray::setDecayNoiseGaussian(float mean, float StdDev){ int i; - for (i=0;i<ARRAY_SIZE;i++){ + for (i=0;i<arraySize;i++){ decayNoiseArray[i] = (1/(StdDev*sqrt(2*PI)))*exp(-1*(i-mean)*(i-mean)/(2*StdDev*StdDev)); } } @@ -265,9 +265,9 @@ void bayesianArray::decayPosteriorWithGaussianNoise(){ int i; - float currentMaximum = getMaximum(&posterior[0], ARRAY_SIZE); - for (i=0;i<ARRAY_SIZE;i++){ - posterior[i] += decayNoiseArray[(i - (int)maximumIndex + ((3*ARRAY_SIZE)/2)) % ARRAY_SIZE] * currentMaximum * decayNoiseAmount; + float currentMaximum = getMaximum(&posterior[0], arraySize); + for (i=0;i<arraySize;i++){ + posterior[i] += decayNoiseArray[(i - (int)maximumIndex + ((3*arraySize)/2)) % arraySize] * currentMaximum * decayNoiseAmount; //posteriorDecayRate * 0.01;;//usded to be * maximum not minus value } @@ -276,7 +276,7 @@ void bayesianArray::resetMaximumPosterior(){ int i; float max = 0; - for (i=0;i < ARRAY_SIZE;i++){ + for (i=0;i < arraySize;i++){ if (posterior[i]>max){ maximumIndex = i; max = posterior[i]; @@ -288,12 +288,12 @@ int tmpIndex; //copy array int i; - for (i=0;i < ARRAY_SIZE;i++){ + for (i=0;i < arraySize;i++){ tempPosteriorArray[i] = posterior[i] ; } //translate values - for (i=0;i < ARRAY_SIZE;i++){ - tmpIndex = (i + translationIndex + ARRAY_SIZE)%ARRAY_SIZE; + for (i=0;i < arraySize;i++){ + tmpIndex = (i + translationIndex + arraySize)%arraySize; posterior[tmpIndex] = tempPosteriorArray[i]; } //now delete tmp array @@ -304,7 +304,7 @@ //take no chances - renormalise both prior and posterior renormalisePosterior(); renormalisePrior(); - for (int i = 0;i < ARRAY_SIZE;i++){ + for (int i = 0;i < arraySize;i++){ if (posterior[i] > 0 && prior[i] > 0){ KLsum += (posterior[i]*log(posterior[i]/prior[i])); } @@ -315,7 +315,7 @@ double bayesianArray::getEntropyOfPosterior(){ double entropy = 0; //make sure normalised? (it is) - for (int i = 0;i < ARRAY_SIZE;i++){ + for (int i = 0;i < arraySize;i++){ if (posterior[i] > 0){ entropy += posterior[i]*log(posterior[i]); }
--- a/newOFsrc/bayesianArray.h Tue Mar 06 14:15:35 2012 +0000 +++ b/newOFsrc/bayesianArray.h Tue Mar 06 17:04:16 2012 +0000 @@ -10,7 +10,7 @@ #ifndef _BAYESIAN_ARRAY #define _BAYESIAN_ARRAY -#define ARRAY_SIZE 240 +//#define arraySize 240 class bayesianArray{ @@ -42,12 +42,14 @@ float* getMaximumEstimate(float *ptr, int length); double getIntegratedEstimateIndex(); - float prior [ARRAY_SIZE]; - float posterior [ARRAY_SIZE]; - float likelihood [ARRAY_SIZE]; - float tempPosteriorArray[ARRAY_SIZE]; + static int const arraySize = 240; + + float prior [arraySize]; + float posterior [arraySize]; + float likelihood [arraySize]; + float tempPosteriorArray[arraySize]; - float decayNoiseArray[ARRAY_SIZE]; + float decayNoiseArray[arraySize]; float decayNoiseStdDev, decayNoiseAmount; float likelihoodMean, likelihoodStdDev, likelihoodNoise;
--- a/newOFsrc/testApp.cpp Tue Mar 06 14:15:35 2012 +0000 +++ b/newOFsrc/testApp.cpp Tue Mar 06 17:04:16 2012 +0000 @@ -55,29 +55,7 @@ KLdiv = 0; entropy = 0; -/* - beatDistribution.initialiseArray(); - tempoDistribution.initialiseArray(); - beatTimes.lastBeatTime = 0; - correctionFactor = 0.5; - - - tempoDistribution.likelihoodStdDev = ARRAY_SIZE / 32; -// tempoDistribution.likelihoodNoise = 0.96; - tempoDistribution.likelihoodNoise = 0.7; - tempoDistribution.setGaussianPrior(ARRAY_SIZE/2, ARRAY_SIZE/1);//wide - - beatDistribution.likelihoodStdDev = ARRAY_SIZE / 32; - beatDistribution.likelihoodNoise = 0.56; - beatDistribution.setGaussianPrior(ARRAY_SIZE/2, ARRAY_SIZE/1); - - - tempoMinimum = 180; - tempoMaximum = 400; - posteriorMaximum = 0.1; - - */ hidePriorMode = false; printInterval = true; @@ -106,27 +84,7 @@ void testApp::resetParameters(){ - /* - beatTimes.startIndex = 0; - beatTimes.lastBeatTime = 0; - maxPhase = 0; - posteriorMaximum = 0.1; - - accompanimentStarted = false; - tempoDistribution.likelihoodNoise = 0.8; - tempoDistribution.setGaussianPrior(ARRAY_SIZE/2, ARRAY_SIZE/2);//wide - - beatDistribution.initialiseArray(); - tempoDistribution.initialiseArray(); - - tempoDistribution.calculateStandardDeviation(); - beatDistribution.calculateStandardDeviation(); - - tempoStdDev = tempoDistribution.standardDeviation; - - beatTimes.resetBeatTimeArray(); - */ } //-------------------------------------------------------------- @@ -168,11 +126,22 @@ if ( m.getAddress() == "/beatError" ){ double timeNow = ofGetElapsedTimeMillis(); if (timeNow - drumTracker.setBeatToNowTime > 1000) + recentError = m.getArgAsFloat(0); + int size = drumTracker.beatDistribution.arraySize; + int newIndex = round((size/2)+(recentError*size)); + double priorValue = drumTracker.beatDistribution.posterior[newIndex]; drumTracker.newKickError(m.getArgAsFloat(0), m.getArgAsFloat(2), m.getArgAsString(1)); + KLdiv = drumTracker.beatDistribution.getKLdivergence(); entropy = drumTracker.beatDistribution.getEntropyOfPosterior(); - recentError = m.getArgAsFloat(0); + drumType = m.getArgAsString(1); + beatPosition = m.getArgAsFloat(3); + printf("NEW BEAT: time %f error %f, position %f, drum %s, ", m.getArgAsFloat(2), recentError, beatPosition, drumType.c_str()); + + printf("KL div %f, entropy %f prior value %f, ", KLdiv, entropy, priorValue); + + printf("tatum is %f\n", drumTracker.beatTimes.tatum ); }//end if new error @@ -1549,3 +1518,48 @@ } */ +/* + beatDistribution.initialiseArray(); + tempoDistribution.initialiseArray(); + beatTimes.lastBeatTime = 0; + correctionFactor = 0.5; + + + + tempoDistribution.likelihoodStdDev = ARRAY_SIZE / 32; + // tempoDistribution.likelihoodNoise = 0.96; + tempoDistribution.likelihoodNoise = 0.7; + tempoDistribution.setGaussianPrior(ARRAY_SIZE/2, ARRAY_SIZE/1);//wide + + beatDistribution.likelihoodStdDev = ARRAY_SIZE / 32; + beatDistribution.likelihoodNoise = 0.56; + beatDistribution.setGaussianPrior(ARRAY_SIZE/2, ARRAY_SIZE/1); + + + tempoMinimum = 180; + tempoMaximum = 400; + posteriorMaximum = 0.1; + + */ + +/* + beatTimes.startIndex = 0; + beatTimes.lastBeatTime = 0; + maxPhase = 0; + posteriorMaximum = 0.1; + + accompanimentStarted = false; + + tempoDistribution.likelihoodNoise = 0.8; + tempoDistribution.setGaussianPrior(ARRAY_SIZE/2, ARRAY_SIZE/2);//wide + + beatDistribution.initialiseArray(); + tempoDistribution.initialiseArray(); + + tempoDistribution.calculateStandardDeviation(); + beatDistribution.calculateStandardDeviation(); + + tempoStdDev = tempoDistribution.standardDeviation; + + beatTimes.resetBeatTimeArray(); + */ \ No newline at end of file