Mercurial > hg > midi-score-follower
comparison src/BayesianArrayStructure.cpp @ 2:5581023e0de4
Added separate CannamMidiFileLoader class to handle the loading in.
author | Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk> |
---|---|
date | Fri, 19 Aug 2011 01:26:40 +0100 |
parents | 1a32ce016bb9 |
children | de86d77f2612 |
comparison
equal
deleted
inserted
replaced
1:1a32ce016bb9 | 2:5581023e0de4 |
---|---|
14 | 14 |
15 prior.createVector(1); | 15 prior.createVector(1); |
16 likelihood.createVector(1); | 16 likelihood.createVector(1); |
17 posterior.createVector(1); | 17 posterior.createVector(1); |
18 | 18 |
19 | 19 |
20 tmpPrior.createVector(240); | |
21 tmpPrior.addGaussianShape(100, 40, 1); | |
22 tmpPrior.addGaussianShape(200, 10, 0.2); | |
23 tmpPrior.translateDistribution(20); | |
24 | |
25 lastEventTime = ofGetElapsedTimeMillis(); | 20 lastEventTime = ofGetElapsedTimeMillis(); |
26 | 21 |
27 speedDecayWidth = 20; | 22 /* |
28 speedDecayAmount = 10; | 23 tmpPrior.createVector(240); |
24 tmpPrior.addGaussianShape(100, 40, 1); | |
25 tmpPrior.addGaussianShape(200, 10, 0.2); | |
26 tmpPrior.translateDistribution(20); | |
27 */ | |
28 tmpBestEstimate = 0; | |
29 } | 29 } |
30 | 30 |
31 BayesianArrayStructure::BayesianArrayStructure(int length){ | 31 BayesianArrayStructure::BayesianArrayStructure(int length){ |
32 printf("BAYESIAN STURTUCRE CREATED LENGTH: %i\n", length); | 32 printf("BAYESIAN STURTUCRE CREATED LENGTH: %i\n", length); |
33 //this constructor isnt called it seems | 33 //this constructor isnt called it seems |
34 prior.createVector(length); | 34 prior.createVector(length); |
35 likelihood.createVector(length); | 35 likelihood.createVector(length); |
36 posterior.createVector(length); | 36 posterior.createVector(length); |
37 | |
38 lastEventTime = 0; | |
37 | 39 |
38 } | 40 } |
39 | 41 |
40 | 42 |
41 | 43 |
43 printf("BAYESIAN STRUCTURE size is : %i\n", length); | 45 printf("BAYESIAN STRUCTURE size is : %i\n", length); |
44 | 46 |
45 prior.createVector(length); | 47 prior.createVector(length); |
46 likelihood.createVector(length); | 48 likelihood.createVector(length); |
47 posterior.createVector(length); | 49 posterior.createVector(length); |
50 | |
48 | 51 |
49 acceleration.createVector(length); | 52 acceleration.createVector(length); |
50 | 53 |
51 } | 54 } |
52 | 55 |
55 void BayesianArrayStructure::resetSpeedToOne(){ | 58 void BayesianArrayStructure::resetSpeedToOne(){ |
56 relativeSpeedPrior.zero(); | 59 relativeSpeedPrior.zero(); |
57 relativeSpeedPosterior.zero(); | 60 relativeSpeedPosterior.zero(); |
58 relativeSpeedLikelihood.zero(); | 61 relativeSpeedLikelihood.zero(); |
59 | 62 |
60 relativeSpeedPosterior.addGaussianShape(40, 5, 0.6); | 63 //relativeSpeedPosterior.addGaussianShape(40, 5, 0.6); |
61 | 64 |
62 relativeSpeedPosterior.addGaussianShape(100, 5, 0.8); | 65 relativeSpeedPosterior.addGaussianShape(100, 20, 0.8); |
66 // relativeSpeedPosterior.addGaussianShape(50, 1, 0.8); | |
67 | |
68 // relativeSpeedPosterior.addToIndex(100, 1); | |
69 //relativeSpeedPosterior.addToIndex(50, 1); | |
63 relativeSpeedPosterior.renormalise(); | 70 relativeSpeedPosterior.renormalise(); |
64 relativeSpeedPosterior.getMaximum(); | 71 relativeSpeedPosterior.getMaximum(); |
65 | 72 |
66 acceleration.addGaussianShape(2000, 20, 0.8); | 73 prior.zero(); |
74 posterior.zero(); | |
75 // posterior.offset = - 200; | |
76 // posterior.addGaussianShape(200, 40, 1); | |
77 posterior.addToIndex(0, 1); | |
78 posterior.renormalise(); | |
79 | |
80 //acceleration.addGaussianShape(2000, 20, 0.8); | |
67 | 81 |
68 } | 82 } |
69 | 83 |
70 void BayesianArrayStructure::resetSpeedSize(int length){ | 84 void BayesianArrayStructure::resetSpeedSize(int length){ |
71 printf("BAYESIAN SPEED size is : %i\n", length); | 85 printf("BAYESIAN SPEED size is : %i\n", length); |
72 | 86 |
73 relativeSpeedPrior.createVector(length); | 87 relativeSpeedPrior.createVector(length); |
74 relativeSpeedLikelihood.createVector(length); | 88 relativeSpeedLikelihood.createVector(length); |
75 relativeSpeedPosterior.createVector(length); | 89 relativeSpeedPosterior.createVector(length); |
76 | 90 tmpPosteriorForStorage.createVector(length); |
77 | 91 |
78 | 92 |
79 } | 93 } |
94 | |
80 void BayesianArrayStructure::setRelativeSpeedScalar(double f){ | 95 void BayesianArrayStructure::setRelativeSpeedScalar(double f){ |
81 relativeSpeedPrior.scalar = f; | 96 relativeSpeedPrior.scalar = f; |
82 relativeSpeedPosterior.scalar = f; | 97 relativeSpeedPosterior.scalar = f; |
83 relativeSpeedLikelihood.scalar = f; | 98 relativeSpeedLikelihood.scalar = f; |
84 } | 99 } |
85 | 100 |
101 | |
102 void BayesianArrayStructure::setPositionDistributionScalar(double f){ | |
103 if (f > 0){ | |
104 prior.scalar = f; | |
105 posterior.scalar = f; | |
106 likelihood.scalar = f; | |
107 } | |
108 } | |
109 | |
86 void BayesianArrayStructure::simpleExample(){ | 110 void BayesianArrayStructure::simpleExample(){ |
87 //simple example | 111 //simple example |
88 prior.addGaussianShape(50, 10, 1); | 112 |
89 prior.addGaussianShape(150, 30, 0.3); | 113 // prior.addGaussianShape(50, 10, 1); |
90 prior.addGaussianShape(250, 30, 0.2); | 114 // prior.addGaussianShape(150, 30, 0.3); |
91 | 115 // prior.addGaussianShape(250, 30, 0.2); |
92 likelihood.addGaussianShape(90, 20, 0.6); | 116 |
93 likelihood.addConstant(0.02); | 117 // likelihood.addGaussianShape(90, 20, 0.6); |
94 posterior.doProduct(prior, likelihood); | 118 // likelihood.addConstant(0.02); |
119 // posterior.doProduct(prior, likelihood); | |
95 | 120 |
96 // relativeSpeedPosterior.addToIndex(100, 1); | 121 // relativeSpeedPosterior.addToIndex(100, 1); |
97 // relativeSpeedPosterior.addToIndex(40, 0.7); | 122 // relativeSpeedPosterior.addToIndex(40, 0.7); |
98 relativeSpeedPosterior.addGaussianShape(100, 20, 1); | 123 |
99 // relativeSpeedPosterior.addGaussianShape(10, 2, 0.5); | 124 // relativeSpeedPosterior.addGaussianShape(100, 2, 1); |
125 // relativeSpeedPosterior.addGaussianShape(40, 2, 0.5); | |
100 relativeSpeedPosterior.getMaximum(); | 126 relativeSpeedPosterior.getMaximum(); |
101 | 127 relativeSpeedPrior.copyFromDynamicVector(relativeSpeedPosterior); |
102 } | 128 } |
103 | 129 |
104 void BayesianArrayStructure::copyPriorToPosterior(){ | 130 void BayesianArrayStructure::copyPriorToPosterior(){ |
105 | 131 |
106 for (int i = 0;i < prior.arraySize;i++){ | 132 for (int i = 0;i < prior.arraySize;i++){ |
107 posterior.array[i] = prior.array[i]; | 133 posterior.array[i] = prior.array[i]; |
108 } | 134 } |
109 } | 135 } |
110 | 136 |
137 void BayesianArrayStructure::setStartPlaying(){ | |
138 lastEventTime = 0;//ofGetElapsedTimeMillis(); | |
139 bestEstimate = 0; | |
140 lastBestEstimateUpdateTime = ofGetElapsedTimeMillis(); | |
141 | |
142 resetArrays(); | |
143 } | |
144 | |
111 void BayesianArrayStructure::resetArrays(){ | 145 void BayesianArrayStructure::resetArrays(){ |
146 //called when we start playing | |
147 | |
112 prior.zero(); | 148 prior.zero(); |
113 likelihood.zero(); | 149 likelihood.zero(); |
114 prior.addGaussianShape(0, 80, 1); | 150 posterior.zero(); |
151 | |
152 updateCounter = 0; | |
153 | |
154 posterior.offset = 0; | |
155 setNewDistributionOffsets(0); | |
156 | |
157 int zeroIndex = posterior.getRealTermsAsIndex(0); | |
158 | |
159 posterior.addGaussianShape(zeroIndex, 500, 1); | |
160 // posterior.addToIndex(0, 1); | |
115 likelihood.addConstant(1); | 161 likelihood.addConstant(1); |
116 posterior.zero(); | 162 |
117 posterior.addGaussianShape(0, 60, 1); | 163 updateCounter = 0; |
118 setNewDistributionOffsets(0); | 164 |
119 bestEstimate = 0; | 165 bestEstimate = 0; |
120 // lastBestEstimateUpdateTime = ofGetElapsedTimeMillis(); | 166 // lastBestEstimateUpdateTime = ofGetElapsedTimeMillis(); |
121 | 167 |
122 } | 168 } |
123 | 169 |
170 void BayesianArrayStructure::zeroArrays(){ | |
171 prior.zero(); | |
172 likelihood.zero(); | |
173 posterior.zero(); | |
174 | |
175 relativeSpeedPrior.zero(); | |
176 relativeSpeedPosterior.zero(); | |
177 relativeSpeedLikelihood.zero(); | |
178 | |
179 } | |
180 | |
181 | |
182 void BayesianArrayStructure::updateTmpBestEstimate(const double& timeDifference){ | |
183 //input is the time since the start of playing | |
184 | |
185 // double timeDiff = ofGetElapsedTimeMillis() - lastEventTime;//lastBestEstimateUpdateTime; | |
186 | |
187 tmpBestEstimate = posterior.getIndexInRealTerms(posterior.MAPestimate) + timeDifference*relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.MAPestimate); | |
188 // | |
189 printf("tmp best %f and best %f time diff %f posterior MAP %f\n", tmpBestEstimate, bestEstimate, timeDifference, posterior.getIndexInRealTerms(posterior.MAPestimate)); | |
190 //lastBestEstimateUpdateTime = ofGetElapsedTimeMillis(); | |
191 } | |
192 | |
124 void BayesianArrayStructure::updateBestEstimate(){ | 193 void BayesianArrayStructure::updateBestEstimate(){ |
125 double timeDiff = ofGetElapsedTimeMillis() - lastEventTime;//lastBestEstimateUpdateTime; | 194 // double timeDiff = ofGetElapsedTimeMillis() - lastEventTime;// |
126 | 195 double timeDiff = ofGetElapsedTimeMillis() - lastBestEstimateUpdateTime; |
127 bestEstimate = posterior.getIndexInRealTerms(posterior.MAPestimate) + timeDiff*relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.MAPestimate); | 196 bestEstimate = posterior.getIndexInRealTerms(posterior.MAPestimate) + timeDiff*relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.MAPestimate); |
128 // | 197 |
129 //lastBestEstimateUpdateTime = ofGetElapsedTimeMillis(); | 198 // bestEstimate = tmpBestEstimate; |
130 } | 199 } |
131 | 200 |
132 void BayesianArrayStructure::calculatePosterior(){ | 201 void BayesianArrayStructure::calculatePosterior(){ |
133 posterior.doProduct(prior, likelihood); | 202 //posterior.doProduct(prior, likelihood); |
203 | |
204 int i; | |
205 for (i = 0;i < posterior.length;i++){ | |
206 posterior.array[i] = likelihood.array[i] * prior.array[i]; | |
207 } | |
208 | |
209 | |
134 posterior.renormalise(); | 210 posterior.renormalise(); |
135 | 211 |
136 /* | 212 |
137 int i; | |
138 for (i = 0;i < prior.length;i++){ | |
139 // printf("priori [%i] is %f\n", i, prior[i]); | |
140 *(posterior+i) = *(prior+i); | |
141 // posterior[i] = likelihood[i] * prior[i]; | |
142 } | |
143 */ | |
144 | 213 |
145 | 214 |
146 } | 215 } |
147 | 216 |
148 | 217 |
149 | 218 |
150 | 219 |
151 void BayesianArrayStructure::setNewDistributionOffsets(const double& newOffset){ | 220 void BayesianArrayStructure::setNewDistributionOffsets(const double& newOffset){ |
152 prior.offset = newOffset; | 221 prior.offset = newOffset; |
153 likelihood.offset = newOffset; | 222 likelihood.offset = newOffset; |
154 // posterior.offset = newOffset; | 223 //posterior.offset = newOffset; |
155 } | 224 } |
156 | 225 |
157 | 226 |
158 void BayesianArrayStructure::crossUpdateArrays(DynamicVector& position, DynamicVector& speed, double timeDifference){ | 227 void BayesianArrayStructure::crossUpdateArrays(DynamicVector& position, DynamicVector& speed, double timeDifference){ |
159 //set the cutoff for offset of position first! XXX | 228 //set the cutoff for offset of position first! XXX |
173 //so we have moved | 242 //so we have moved |
174 int distanceMoved = round(timeDifferenceInPositionVectorUnits * speedValue);//round the value | 243 int distanceMoved = round(timeDifferenceInPositionVectorUnits * speedValue);//round the value |
175 | 244 |
176 if (speed.array[i] != 0){ | 245 if (speed.array[i] != 0){ |
177 | 246 |
178 // printf("speed [%i] gives %f moved %i\n", i, speedValue, distanceMoved); | 247 // printf("speed [%i] gives %f moved %i in %f units \n", i, speedValue, distanceMoved, timeDifferenceInPositionVectorUnits); |
179 | 248 |
180 for (int postIndex = 0;postIndex < position.arraySize;postIndex++){ | 249 for (int postIndex = 0;postIndex < position.arraySize;postIndex++){ |
181 //old posterior contributing to new prior | 250 //old posterior contributing to new prior |
182 int newPriorIndex = postIndex + position.offset - prior.offset + distanceMoved; | 251 int newPriorIndex = postIndex + position.offset - prior.offset + distanceMoved; |
183 if (newPriorIndex >= 0 && newPriorIndex < prior.arraySize){ | 252 if (newPriorIndex >= 0 && newPriorIndex < prior.arraySize){ |
184 prior.addToIndex(newPriorIndex, position.array[postIndex]*speed.array[i]); | 253 prior.addToIndex(newPriorIndex, position.array[postIndex]*speed.array[i]); |
185 // printf("adding [%i] : %f\n", newPriorIndex, posterior.array[postIndex]*speed.array[i]); | 254 // printf("adding [%i] : %f\n", newPriorIndex, posterior.array[postIndex]*speed.array[i]); |
186 } | 255 } |
187 | 256 |
188 } | 257 } |
189 | 258 |
190 }//if not zero | 259 }//if not zero |
191 }//end speed | 260 }//end speed |
192 | 261 updateCounter++; |
193 prior.renormalise(); | 262 prior.renormalise(); |
194 | 263 |
264 } | |
265 | |
266 | |
267 void BayesianArrayStructure::addGaussianNoiseToSpeedPosterior(const double& std_dev){ | |
268 tmpPosteriorForStorage.copyFromDynamicVector(relativeSpeedPosterior); | |
269 | |
270 for (int i = 0;i < relativeSpeedPosterior.length;i++){ | |
271 tmpPosteriorForStorage.addGaussianShape(i, std_dev, relativeSpeedPosterior.array[i]); | |
272 } | |
273 | |
274 tmpPosteriorForStorage.renormalise(); | |
275 | |
276 relativeSpeedPosterior.copyFromDynamicVector(tmpPosteriorForStorage); | |
277 } | |
278 | |
279 | |
280 void BayesianArrayStructure::addTriangularNoiseToSpeedPosterior(const double& std_dev){ | |
281 tmpPosteriorForStorage.copyFromDynamicVector(relativeSpeedPosterior); | |
282 | |
283 for (int i = 0;i < relativeSpeedPosterior.length;i++){ | |
284 //adding a linear amount depending on distance | |
285 tmpPosteriorForStorage.addTriangularShape(i, std_dev*2.0, relativeSpeedPosterior.array[i]); | |
286 } | |
287 | |
288 tmpPosteriorForStorage.renormalise(); | |
289 | |
290 relativeSpeedPosterior.copyFromDynamicVector(tmpPosteriorForStorage); | |
195 } | 291 } |
196 | 292 |
197 void BayesianArrayStructure::calculateNewPriorOffset(const double& timeDifference){ | 293 void BayesianArrayStructure::calculateNewPriorOffset(const double& timeDifference){ |
198 | 294 |
199 double maxSpeed = relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.MAPestimate); | 295 double maxSpeed = relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.MAPestimate); |
244 if (index >= 0 && index < relativeSpeedPrior.length){ | 340 if (index >= 0 && index < relativeSpeedPrior.length){ |
245 //then we can do update | 341 //then we can do update |
246 | 342 |
247 //set new likelihood | 343 //set new likelihood |
248 relativeSpeedLikelihood.zero(); | 344 relativeSpeedLikelihood.zero(); |
249 relativeSpeedLikelihood.addConstant(0.05); | 345 relativeSpeedLikelihood.addConstant(speedLikelihoodNoise); |
250 | 346 |
251 relativeSpeedLikelihood.addGaussianShape(index , 5, 0.5*matchFactor); | 347 relativeSpeedLikelihood.addGaussianShape(index , 5, 0.5*matchFactor); |
252 | 348 |
253 | 349 |
254 //copy posterior to prior | 350 //copy posterior to prior |
301 | 397 |
302 //bayesArray.drawFloatArray(&bayesArray.prior[0], 0, 200); | 398 //bayesArray.drawFloatArray(&bayesArray.prior[0], 0, 200); |
303 //bayesArray.drawFloatArray(&bayesArray.prior[0], 0, 200); | 399 //bayesArray.drawFloatArray(&bayesArray.prior[0], 0, 200); |
304 | 400 |
305 int displaySize = prior.arraySize; | 401 int displaySize = prior.arraySize; |
306 ofSetColor(255,0,0); | 402 ofSetColor(0,0,255); |
307 prior.drawVector(0, displaySize); | 403 prior.drawVector(0, displaySize); |
308 ofSetColor(0,255,0); | 404 ofSetColor(0,255,0); |
309 likelihood.drawVector(0, displaySize); | 405 likelihood.drawVector(0, displaySize); |
310 ofSetColor(0,0,255); | 406 ofSetColor(255,0,255); |
311 posterior.drawVector(0, displaySize); | 407 posterior.drawVector(0, displaySize); |
312 ofSetColor(255,255,0); | 408 |
313 relativeSpeedPosterior.drawVector(0, relativeSpeedPosterior.arraySize); | |
314 | 409 |
315 // ofSetColor(255,255,255); | 410 // ofSetColor(255,255,255); |
316 // tmpPrior.drawVector(0,300); | 411 // tmpPrior.drawVector(0,300); |
317 | 412 |
318 } | 413 } |
325 ofSetColor(255,0,255); | 420 ofSetColor(255,0,255); |
326 relativeSpeedLikelihood.drawVector(0, relativeSpeedLikelihood.arraySize); | 421 relativeSpeedLikelihood.drawVector(0, relativeSpeedLikelihood.arraySize); |
327 | 422 |
328 ofSetColor(255,255,0); | 423 ofSetColor(255,255,0); |
329 relativeSpeedPosterior.drawVector(0, relativeSpeedPosterior.arraySize); | 424 relativeSpeedPosterior.drawVector(0, relativeSpeedPosterior.arraySize); |
425 | |
426 ofSetColor(0,0,255); | |
427 tmpPosteriorForStorage.drawVector(0, tmpPosteriorForStorage.arraySize); | |
330 | 428 |
331 ofSetColor(255,255, 255); | 429 ofSetColor(255,255, 255); |
332 ofLine(screenWidth/2, 0, screenWidth/2, ofGetHeight());//middle of screen | 430 ofLine(screenWidth/2, 0, screenWidth/2, ofGetHeight());//middle of screen |
333 | 431 |
334 ofSetColor(0, 255, 0); | 432 ofSetColor(0, 255, 0); |