comparison src/BayesianArrayStructure.cpp @ 18:c7107e5c8f03

added in a mode to use either integrated estimate or MAP estimate in tempo process. Decreased nooise param from 0.02 to 0.03 so observations given more weight
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Fri, 18 Nov 2011 11:56:32 +0000
parents 3f103cf78148
children 2e17f0fdeaef
comparison
equal deleted inserted replaced
17:d75d16c57eac 18:c7107e5c8f03
22 speedPriorValue = 1.0; 22 speedPriorValue = 1.0;
23 speedEstimate = speedPriorValue; 23 speedEstimate = speedPriorValue;
24 24
25 lastEventTime = 0;//ofGetElapsedTimeMillis(); 25 lastEventTime = 0;//ofGetElapsedTimeMillis();
26 26
27 /*
28 tmpPrior.createVector(240);
29 tmpPrior.addGaussianShape(100, 40, 1);
30 tmpPrior.addGaussianShape(200, 10, 0.2);
31 tmpPrior.translateDistribution(20);
32 */
33 tmpBestEstimate = 0; 27 tmpBestEstimate = 0;
34 crossUpdateTimeThreshold = 100; 28 crossUpdateTimeThreshold = 100;
35 priorWidth = 30; 29 priorWidth = 50;
36 } 30 }
37 31
38 BayesianArrayStructure::BayesianArrayStructure(int length){ 32 BayesianArrayStructure::BayesianArrayStructure(int length){
39 printf("BAYESIAN STURTUCRE CREATED LENGTH: %i\n", length); 33 printf("BAYESIAN STURTUCRE CREATED LENGTH: %i\n", length);
40 //this constructor isnt called it seems 34 //this constructor isnt called it seems
41 prior.createVector(length); 35 prior.createVector(length);
42 likelihood.createVector(length); 36 likelihood.createVector(length);
43 posterior.createVector(length); 37 posterior.createVector(length);
44 38
45 lastEventTime = 0; 39 lastEventTime = 0;
40 usingIntegratedTempoEstimate = false;//use max index
46 41
47 } 42 }
48 43
49 44
50 45
65 void BayesianArrayStructure::resetSpeedToOne(){ 60 void BayesianArrayStructure::resetSpeedToOne(){
66 relativeSpeedPrior.zero(); 61 relativeSpeedPrior.zero();
67 relativeSpeedPosterior.zero(); 62 relativeSpeedPosterior.zero();
68 relativeSpeedLikelihood.zero(); 63 relativeSpeedLikelihood.zero();
69 64
70 //relativeSpeedPosterior.addGaussianShape(40, 5, 0.6);
71 65
72 relativeSpeedPosterior.addGaussianShape(100, 20, 0.8); 66 relativeSpeedPosterior.addGaussianShape(100, 20, 0.8);
73 // relativeSpeedPosterior.addGaussianShape(50, 1, 0.8);
74
75 // relativeSpeedPosterior.addToIndex(100, 1);
76 //relativeSpeedPosterior.addToIndex(50, 1);
77 relativeSpeedPosterior.renormalise(); 67 relativeSpeedPosterior.renormalise();
78 relativeSpeedPosterior.getMaximum(); 68 relativeSpeedPosterior.getMaximum();
79 69
80 setSpeedPrior(speedPriorValue); 70 setSpeedPrior(speedPriorValue);
81 speedEstimate = speedPriorValue; 71 speedEstimate = speedPriorValue;
82 72
83 prior.zero(); 73 prior.zero();
84 posterior.zero(); 74 posterior.zero();
85 // posterior.offset = - 200; 75
86 // posterior.addGaussianShape(200, 40, 1);
87 posterior.addToIndex(0, 1); 76 posterior.addToIndex(0, 1);
88 posterior.renormalise(); 77 posterior.renormalise();
89 78
90 //acceleration.addGaussianShape(2000, 20, 0.8); 79 //acceleration.addGaussianShape(2000, 20, 0.8);
91 80
128 likelihood.scalar = f; 117 likelihood.scalar = f;
129 } 118 }
130 } 119 }
131 120
132 void BayesianArrayStructure::simpleExample(){ 121 void BayesianArrayStructure::simpleExample(){
133 //simple example
134
135 // prior.addGaussianShape(50, 10, 1);
136 // prior.addGaussianShape(150, 30, 0.3);
137 // prior.addGaussianShape(250, 30, 0.2);
138
139 // likelihood.addGaussianShape(90, 20, 0.6);
140 // likelihood.addConstant(0.02);
141 // posterior.doProduct(prior, likelihood);
142
143 // relativeSpeedPosterior.addToIndex(100, 1);
144 // relativeSpeedPosterior.addToIndex(40, 0.7);
145
146 // relativeSpeedPosterior.addGaussianShape(100, 2, 1);
147 // relativeSpeedPosterior.addGaussianShape(40, 2, 0.5);
148 relativeSpeedPosterior.getMaximum(); 122 relativeSpeedPosterior.getMaximum();
149 relativeSpeedPrior.copyFromDynamicVector(relativeSpeedPosterior); 123 relativeSpeedPrior.copyFromDynamicVector(relativeSpeedPosterior);
150 } 124 }
151 125
152 void BayesianArrayStructure::copyPriorToPosterior(){ 126 void BayesianArrayStructure::copyPriorToPosterior(){
226 //Using timedifferencfe here will make it go wrong. Is time since beginning of playing 200 //Using timedifferencfe here will make it go wrong. Is time since beginning of playing
227 201
228 //if (*realTimeMode) 202 //if (*realTimeMode)
229 timeDiff = ofGetElapsedTimeMillis() - lastBestEstimateUpdateTime; 203 timeDiff = ofGetElapsedTimeMillis() - lastBestEstimateUpdateTime;
230 204
231 double speedEstimate = relativeSpeedPosterior.getIntegratedEstimate(); 205 double speedEstimate;
206 if (usingIntegratedTempoEstimate)
207 speedEstimate = relativeSpeedPosterior.getIntegratedEstimate();
208 else
209 speedEstimate = relativeSpeedPosterior.MAPestimate;
210
211
232 speedEstimate = relativeSpeedPosterior.getIndexInRealTerms(speedEstimate); 212 speedEstimate = relativeSpeedPosterior.getIndexInRealTerms(speedEstimate);
233 //relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.MAPestimate) 213 //relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.MAPestimate)
234 bestEstimate = posterior.getIndexInRealTerms(posterior.MAPestimate) + timeDiff*speedEstimate; 214 bestEstimate = posterior.getIndexInRealTerms(posterior.MAPestimate) + timeDiff*speedEstimate;
235 215
236 // bestEstimate = tmpBestEstimate; 216 // bestEstimate = tmpBestEstimate;
503 ofLine(screenWidth/2, 0, screenWidth/2, ofGetHeight());//middle of screen 483 ofLine(screenWidth/2, 0, screenWidth/2, ofGetHeight());//middle of screen
504 484
505 ofSetColor(155,255, 0); 485 ofSetColor(155,255, 0);
506 double fractionOfScreen = ((double)relativeSpeedPosterior.integratedEstimate / relativeSpeedPosterior.length); 486 double fractionOfScreen = ((double)relativeSpeedPosterior.integratedEstimate / relativeSpeedPosterior.length);
507 ofLine(screenWidth * fractionOfScreen, 0, screenWidth * fractionOfScreen, ofGetHeight()); 487 ofLine(screenWidth * fractionOfScreen, 0, screenWidth * fractionOfScreen, ofGetHeight());
488
489 ofSetColor(0,255, 255);
490 fractionOfScreen = ((double)relativeSpeedPosterior.MAPestimate / relativeSpeedPosterior.length);
491 ofLine(screenWidth * fractionOfScreen, 0, screenWidth * fractionOfScreen, ofGetHeight());
492
508 } 493 }
509 494
510 495
511 void BayesianArrayStructure::drawArraysRelativeToTimeframe(const double& startTimeMillis, const double& endTimeMillis){ 496 void BayesianArrayStructure::drawArraysRelativeToTimeframe(const double& startTimeMillis, const double& endTimeMillis){
512 497