annotate bayesianArraySrc/BayesianArrayStructure.cpp @ 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
rev   line source
andrew@0 1 /*
andrew@0 2 * BayesianArrayStructure.cpp
andrew@0 3 * midiCannamReader
andrew@0 4 *
andrew@0 5 * Created by Andrew on 17/07/2011.
andrew@0 6 * Copyright 2011 QMUL. All rights reserved.
andrew@0 7 *
andrew@0 8 */
andrew@0 9
andrew@0 10 //look at reset speed to one - what does this do? - get rid of?
andrew@0 11
andrew@0 12
andrew@0 13 #include "BayesianArrayStructure.h"
andrew@0 14
andrew@0 15 BayesianArrayStructure::BayesianArrayStructure(){
andrew@0 16 printf("Bayesian structure: DeFault constructor called");
andrew@0 17
andrew@4 18 usingIntegratedTempoEstimate = false;// use max index
andrew@0 19
andrew@0 20 relativeSpeedLikelihoodStdDev = 5.0;
andrew@0 21
andrew@0 22 prior.createVector(1);
andrew@0 23 likelihood.createVector(1);
andrew@0 24 posterior.createVector(1);
andrew@0 25
andrew@0 26 speedPriorValue = 1.0;//default value for the speed prior
andrew@0 27 speedEstimate = speedPriorValue;
andrew@0 28
andrew@0 29 lastEventTime = 0;//ofGetElapsedTimeMillis();
andrew@0 30
andrew@0 31 tmpBestEstimate = 0;
andrew@4 32 crossUpdateTimeThreshold = 60000;
andrew@3 33 priorWidth = 20;
andrew@0 34
andrew@0 35 }
andrew@0 36
andrew@0 37 BayesianArrayStructure::BayesianArrayStructure(int length){
andrew@0 38 printf("BAYESIAN STURCTURE CREATED LENGTH: %i\n", length);
andrew@0 39 //this constructor isnt called it seems
andrew@0 40 prior.createVector(length);
andrew@0 41 likelihood.createVector(length);
andrew@0 42 posterior.createVector(length);
andrew@0 43
andrew@0 44 lastEventTime = 0;
andrew@0 45
andrew@0 46
andrew@0 47 }
andrew@0 48
andrew@0 49
andrew@0 50
andrew@0 51 void BayesianArrayStructure::resetSize(int length){
andrew@0 52 printf("BAYESIAN STRUCTURE size is : %i\n", length);
andrew@0 53
andrew@0 54 prior.createVector(length);
andrew@0 55 likelihood.createVector(length);
andrew@0 56 posterior.createVector(length);
andrew@0 57
andrew@0 58 acceleration.createVector(length);
andrew@0 59
andrew@0 60 }
andrew@0 61
andrew@0 62 void BayesianArrayStructure::resetSpeedSize(int length){
andrew@0 63 printf("BAYESIAN reset SPEED size is : %i\n", length);
andrew@0 64
andrew@0 65 relativeSpeedPrior.createVector(length);
andrew@0 66 relativeSpeedLikelihood.createVector(length);
andrew@0 67 relativeSpeedPosterior.createVector(length);
andrew@0 68 tmpPosteriorForStorage.createVector(length);
andrew@0 69
andrew@0 70
andrew@0 71
andrew@0 72 }
andrew@0 73
andrew@0 74 void BayesianArrayStructure::setRelativeSpeedScalar(double f){
andrew@0 75 relativeSpeedPrior.scalar = f;
andrew@0 76 relativeSpeedPosterior.scalar = f;
andrew@0 77 relativeSpeedLikelihood.scalar = f;
andrew@0 78 }
andrew@0 79
andrew@0 80 void BayesianArrayStructure::resetSpeedToOne(){
andrew@0 81 relativeSpeedPrior.zero();
andrew@0 82 relativeSpeedPosterior.zero();
andrew@0 83 relativeSpeedLikelihood.zero();
andrew@0 84
andrew@0 85
andrew@0 86 relativeSpeedPosterior.addGaussianShape(100, 20, 0.8);
andrew@0 87 relativeSpeedPosterior.renormalise();
andrew@0 88 relativeSpeedPosterior.getMaximum();
andrew@0 89
andrew@0 90 setSpeedPrior(speedPriorValue);
andrew@0 91 speedEstimate = speedPriorValue;
andrew@0 92
andrew@0 93 prior.zero();
andrew@0 94 posterior.zero();
andrew@0 95
andrew@0 96 posterior.addToIndex(0, 1);
andrew@0 97 posterior.renormalise();
andrew@0 98
andrew@0 99 }
andrew@0 100
andrew@0 101 void BayesianArrayStructure::setSpeedPrior(double f){
andrew@0 102 speedPriorValue = f;
andrew@0 103 int index = relativeSpeedPosterior.getRealTermsAsIndex(speedPriorValue);
andrew@0 104 relativeSpeedPosterior.zero();
andrew@0 105 relativeSpeedPosterior.addGaussianShape(index, priorWidth, 0.8);
andrew@4 106 printf("speed adding to index for 1 = %f\n", relativeSpeedPosterior.getRealTermsAsIndex(1));
andrew@4 107 relativeSpeedPosterior.addToIndex(relativeSpeedPosterior.getRealTermsAsIndex(1), 1);
andrew@4 108
andrew@0 109 relativeSpeedPosterior.renormalise();
andrew@0 110 relativeSpeedPosterior.getMaximum();
andrew@0 111 relativeSpeedPrior.copyFromDynamicVector(relativeSpeedPosterior);
andrew@0 112 printf("BAYES STRUCTU ' SPEED PRIOR %f . index %i\n", speedPriorValue, index);
andrew@0 113
andrew@0 114 }
andrew@0 115
andrew@0 116
andrew@0 117 void BayesianArrayStructure::setPositionDistributionScalar(double f){
andrew@0 118 if (f > 0){
andrew@0 119 prior.scalar = f;
andrew@0 120 posterior.scalar = f;
andrew@0 121 likelihood.scalar = f;
andrew@0 122 }
andrew@0 123 }
andrew@0 124
andrew@0 125 void BayesianArrayStructure::simpleExample(){
andrew@0 126 relativeSpeedPosterior.getMaximum();
andrew@0 127 relativeSpeedPrior.copyFromDynamicVector(relativeSpeedPosterior);
andrew@0 128 }
andrew@0 129
andrew@0 130 void BayesianArrayStructure::copyPriorToPosterior(){
andrew@0 131
andrew@0 132 for (int i = 0;i < prior.arraySize;i++){
andrew@0 133 posterior.array[i] = prior.array[i];
andrew@0 134 }
andrew@0 135 }
andrew@0 136
andrew@0 137 void BayesianArrayStructure::setStartPlaying(){
andrew@0 138
andrew@0 139 lastEventTime = 0;
andrew@0 140 bestEstimate = 0;
andrew@0 141 lastBestEstimateUpdateTime = 0;
andrew@3 142
andrew@0 143 if (*realTimeMode)
andrew@0 144 lastBestEstimateUpdateTime = ofGetElapsedTimeMillis();
andrew@0 145 //cannot just be zero - offline bug
andrew@0 146 //printf("start playing - best estimate %f\n", lastBestEstimateUpdateTime);
andrew@0 147
andrew@0 148 resetArrays();
andrew@0 149 }
andrew@0 150
andrew@0 151 void BayesianArrayStructure::resetArrays(){
andrew@0 152 //called when we start playing
andrew@0 153
andrew@0 154 prior.zero();
andrew@0 155 likelihood.zero();
andrew@0 156 posterior.zero();
andrew@0 157
andrew@0 158 updateCounter = 0;
andrew@0 159
andrew@0 160 posterior.offset = -1000;
andrew@0 161 setNewDistributionOffsets(0);
andrew@0 162
andrew@0 163 int zeroIndex = posterior.getRealTermsAsIndex(0);
andrew@3 164 printf("ZERO INDEX %i\n", zeroIndex);
andrew@0 165
andrew@3 166 posterior.addGaussianShapeFromRealTime(0, 500, 1);//one way to add at x msec
andrew@3 167 posterior.addGaussianShape(posterior.getRealTermsAsIndex(10), 50, 1);//alternative way
andrew@3 168
andrew@3 169 //posterior.addToIndex(0, 1);
andrew@0 170 likelihood.addConstant(1);
andrew@0 171
andrew@0 172 updateCounter = 0;
andrew@0 173
andrew@0 174
andrew@0 175 printf("bayes reset arrays - best estimate %f\n", lastBestEstimateUpdateTime);
andrew@0 176
andrew@0 177 setSpeedPrior(speedPriorValue);
andrew@3 178 relativeSpeedPosterior.copyFromDynamicVector(relativeSpeedPrior);
andrew@3 179
andrew@0 180 }
andrew@0 181
andrew@0 182
andrew@0 183 void BayesianArrayStructure::zeroArrays(){
andrew@0 184 prior.zero();
andrew@0 185 likelihood.zero();
andrew@0 186 posterior.zero();
andrew@0 187
andrew@0 188 relativeSpeedPrior.zero();
andrew@0 189 relativeSpeedPosterior.zero();
andrew@0 190 relativeSpeedLikelihood.zero();
andrew@0 191
andrew@0 192 }
andrew@0 193
andrew@4 194 /*
andrew@0 195 void BayesianArrayStructure::updateTmpBestEstimate(const double& timeDifference){
andrew@0 196 //input is the time since the start of playing
andrew@0 197 // double timeDiff = ofGetElapsedTimeMillis() - lastEventTime;//lastBestEstimateUpdateTime;
andrew@0 198 double timeDiff = timeDifference;
andrew@0 199 if (*realTimeMode)
andrew@0 200 timeDiff = ofGetElapsedTimeMillis() - lastBestEstimateUpdateTime;
andrew@0 201
andrew@0 202 double tmp = relativeSpeedPosterior.getIntegratedEstimate();
andrew@0 203 tmpBestEstimate = posterior.getIndexInRealTerms(posterior.MAPestimate) + timeDiff*relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate);
andrew@0 204 //
andrew@0 205 //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));
andrew@0 206 //lastBestEstimateUpdateTime = ofGetElapsedTimeMillis();
andrew@0 207 }
andrew@4 208 */
andrew@4 209
andrew@0 210 void BayesianArrayStructure::updateBestEstimate(const double& timeDifference){
andrew@0 211 // double timeDiff = ofGetElapsedTimeMillis() - lastEventTime;//
andrew@3 212 double tmp = bestEstimate;
andrew@4 213 printf("best est routine: posterior offset %f\n", posterior.offset);
andrew@0 214
andrew@0 215 double timeDiff = timeDifference;
andrew@0 216
andrew@0 217 //Using timedifferencfe here will make it go wrong. Is time since beginning of playing
andrew@0 218
andrew@0 219 if (*realTimeMode)
andrew@0 220 timeDiff = ofGetElapsedTimeMillis() - lastBestEstimateUpdateTime;
andrew@0 221
andrew@0 222 //lastbest is time we started playing
andrew@4 223 /*
andrew@0 224 if (usingIntegratedTempoEstimate)
andrew@0 225 speedEstimateIndex = relativeSpeedPosterior.getIntegratedEstimate();
andrew@0 226 else
andrew@4 227 speedEstimateIndex = relativeSpeedPosterior.getMAPestimate();
andrew@4 228 */
andrew@4 229 speedEstimateIndex = getSpeedEstimateIndex();
andrew@0 230
andrew@0 231 speedEstimate = relativeSpeedPosterior.getIndexInRealTerms(speedEstimateIndex);
andrew@0 232 bestEstimate = posterior.getIndexInRealTerms(posterior.MAPestimate) + timeDiff*speedEstimate;
andrew@0 233
andrew@4 234 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,
andrew@4 235 posterior.MAPestimate, posterior.getIndexInRealTerms(posterior.MAPestimate), speedEstimateIndex, speedEstimate, timeDiff*speedEstimate);
andrew@0 236 }
andrew@0 237
andrew@0 238 void BayesianArrayStructure::calculatePosterior(){
andrew@0 239 //posterior.doProduct(prior, likelihood);
andrew@4 240 assert(posterior.offset == prior.offset);
andrew@0 241
andrew@0 242 int i;
andrew@0 243 for (i = 0;i < posterior.length;i++){
andrew@0 244 posterior.array[i] = likelihood.array[i] * prior.array[i];
andrew@0 245 }
andrew@0 246
andrew@0 247 posterior.renormalise();
andrew@0 248
andrew@0 249 }
andrew@0 250
andrew@0 251
andrew@4 252 double BayesianArrayStructure::getSpeedEstimateIndex(){
andrew@4 253 if (usingIntegratedTempoEstimate)
andrew@4 254 return relativeSpeedPosterior.getIntegratedEstimate();
andrew@4 255 else
andrew@4 256 return relativeSpeedPosterior.getMAPestimate();
andrew@4 257 }
andrew@0 258
andrew@0 259
andrew@0 260 void BayesianArrayStructure::setNewDistributionOffsets(const double& newOffset){
andrew@4 261
andrew@4 262 printf("prior offset was %f now %f\n", prior.offset, newOffset);
andrew@4 263
andrew@0 264 prior.offset = newOffset;
andrew@0 265 likelihood.offset = newOffset;
andrew@4 266
andrew@0 267 //posterior.offset = newOffset;
andrew@0 268 }
andrew@0 269
andrew@0 270
andrew@0 271 void BayesianArrayStructure::crossUpdateArrays(DynamicVector& position, DynamicVector& speed, double timeDifference){
andrew@0 272 //set the cutoff for offset of position first! XXX
andrew@0 273
andrew@0 274 // printf("time difference %f, ", timeDifference);
andrew@0 275
andrew@0 276 double timeDifferenceInPositionVectorUnits = timeDifference / prior.scalar;
andrew@0 277
andrew@4 278 printf("CROSS UPDATE time diff %f ms is %f units; ", timeDifference, timeDifferenceInPositionVectorUnits);
andrew@0 279 prior.zero();//kill prior
andrew@4 280
andrew@4 281 // calculateNewPriorOffset(timeDifference);//dioesnt do anything
andrew@4 282
andrew@4 283 printf("new prior offset %f and post offset %f\n", prior.offset, posterior.offset);
andrew@0 284
andrew@0 285 if (timeDifferenceInPositionVectorUnits > crossUpdateTimeThreshold)
andrew@0 286 complexCrossUpdate(timeDifferenceInPositionVectorUnits);
andrew@0 287 else
andrew@0 288 translateByMaximumSpeed(timeDifferenceInPositionVectorUnits);
andrew@0 289
andrew@0 290
andrew@0 291 updateCounter++;
andrew@0 292 prior.renormalise();
andrew@0 293
andrew@0 294 }
andrew@0 295
andrew@0 296 void BayesianArrayStructure::complexCrossUpdate(const double& timeDifferenceInPositionVectorUnits){
andrew@4 297
andrew@0 298 int distanceMoved, newPriorIndex;
andrew@0 299
andrew@0 300 double speedValue = relativeSpeedPosterior.offset;
andrew@0 301
andrew@0 302 for (int i = 0;i < relativeSpeedPosterior.arraySize;i++){
andrew@0 303
andrew@0 304 // double speedValue = relativeSpeedPosterior.getIndexInRealTerms(i);//so for scalar 0.01, 50 -> speed value of 0.5
andrew@0 305
andrew@0 306 //so we have moved
andrew@0 307 distanceMoved = round(timeDifferenceInPositionVectorUnits * speedValue);//round the value
andrew@0 308
andrew@0 309 if (relativeSpeedPosterior.array[i] != 0){
andrew@4 310
andrew@0 311 double speedContribution = relativeSpeedPosterior.array[i];
andrew@0 312
andrew@4 313 // printf("speed [%i](val[%f]) gives %f moved %i in %f units \n", i, relativeSpeedPosterior.array[i], speedValue, distanceMoved, timeDifferenceInPositionVectorUnits);
andrew@0 314
andrew@4 315 //1/2/12 deleted line
andrew@4 316 newPriorIndex = posterior.offset - prior.offset + distanceMoved;//i.e. where post[0] goes to in terms of prior at this speed
andrew@4 317 int postIndex = 0;
andrew@4 318 while (postIndex < posterior.arraySize && newPriorIndex < prior.arraySize){
andrew@4 319
andrew@4 320 //did use a for loop
andrew@4 321 // for (postIndex = 0;postIndex < posterior.arraySize;postIndex++){
andrew@0 322 //old posterior contributing to new prior
andrew@4 323
andrew@4 324 //would use this method
andrew@4 325 //newPriorIndex = postIndex + posterior.offset - prior.offset + distanceMoved;
andrew@0 326
andrew@4 327 if (newPriorIndex >= 0){
andrew@0 328 prior.addToIndex(newPriorIndex, posterior.array[postIndex]*speedContribution);
andrew@4 329 // printf("speed index %i new prior index %i post val %f speed contrib %f dist %i\n", i, newPriorIndex, posterior.array[postIndex], speedContribution, distanceMoved);
andrew@0 330 }
andrew@4 331 //but we actually do this for simplicity
andrew@4 332 newPriorIndex++;
andrew@4 333 postIndex++;
andrew@4 334 }//end for. now while
andrew@0 335
andrew@0 336
andrew@0 337 }//if not zero
andrew@0 338 speedValue += relativeSpeedPosterior.scalar;
andrew@0 339 //optimised line
andrew@0 340 //as we wanted:
andrew@0 341 // double speedValue = relativeSpeedPosterior.getIndexInRealTerms(i);//so for scalar 0.01, 50 -> speed value of 0.5
andrew@0 342 }//end speed
andrew@0 343 }
andrew@0 344
andrew@0 345
andrew@0 346
andrew@0 347 void BayesianArrayStructure::translateByMaximumSpeed(const double& timeDifferenceInPositionVectorUnits){
andrew@0 348
andrew@0 349 int distanceMoved, newPriorIndex;
andrew@0 350
andrew@0 351 double speedValue = relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate);
andrew@0 352 //so for scalar 0.01, 50 -> speed value of 0.5
andrew@0 353 double speedContribution = relativeSpeedPosterior.array[relativeSpeedPosterior.integratedEstimate];
andrew@0 354 //so we have moved
andrew@0 355 distanceMoved = round(timeDifferenceInPositionVectorUnits * speedValue);//round the value
andrew@0 356 // printf("speed [%i] gives %f moved %i in %f units \n", i, speedValue, distanceMoved, timeDifferenceInPositionVectorUnits);
andrew@0 357
andrew@0 358 for (int postIndex = 0;postIndex < posterior.arraySize;postIndex++){
andrew@0 359 //old posterior contributing to new prior
andrew@0 360 newPriorIndex = postIndex + posterior.offset - prior.offset + distanceMoved;
andrew@0 361 if (newPriorIndex >= 0 && newPriorIndex < prior.arraySize){
andrew@0 362 prior.addToIndex(newPriorIndex, posterior.array[postIndex]*speedContribution);
andrew@0 363 }
andrew@0 364
andrew@0 365 }
andrew@0 366
andrew@0 367 }
andrew@0 368
andrew@0 369 void BayesianArrayStructure::addGaussianNoiseToSpeedPosterior(const double& std_dev){
andrew@0 370 tmpPosteriorForStorage.copyFromDynamicVector(relativeSpeedPosterior);
andrew@0 371
andrew@0 372 for (int i = 0;i < relativeSpeedPosterior.length;i++){
andrew@0 373 tmpPosteriorForStorage.addGaussianShape(i, std_dev, relativeSpeedPosterior.array[i]);
andrew@0 374 }
andrew@0 375
andrew@0 376 tmpPosteriorForStorage.renormalise();
andrew@0 377
andrew@0 378 relativeSpeedPosterior.copyFromDynamicVector(tmpPosteriorForStorage);
andrew@0 379 }
andrew@0 380
andrew@0 381
andrew@0 382 void BayesianArrayStructure::addTriangularNoiseToSpeedPosterior(const double& std_dev){
andrew@0 383 tmpPosteriorForStorage.copyFromDynamicVector(relativeSpeedPosterior);
andrew@0 384
andrew@0 385 for (int i = 0;i < relativeSpeedPosterior.length;i++){
andrew@0 386 //adding a linear amount depending on distance
andrew@0 387 tmpPosteriorForStorage.addTriangularShape(i, std_dev*2.0, relativeSpeedPosterior.array[i]);
andrew@0 388 }
andrew@0 389
andrew@0 390 tmpPosteriorForStorage.renormalise();
andrew@0 391
andrew@0 392 relativeSpeedPosterior.copyFromDynamicVector(tmpPosteriorForStorage);
andrew@0 393 }
andrew@0 394
andrew@0 395 void BayesianArrayStructure::calculateNewPriorOffset(const double& timeDifference){
andrew@0 396
andrew@4 397 // double maxSpeed = relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate);
andrew@4 398
andrew@4 399 double maxSpeed = relativeSpeedPosterior.getIndexInRealTerms(getSpeedEstimateIndex());//either integrated or MAP
andrew@0 400 // printf("Maxspeed is %f\n", maxSpeed);
andrew@0 401
andrew@0 402 double priorMax = posterior.getMaximum();
andrew@0 403 double distanceTravelled = maxSpeed * (timeDifference / prior.scalar);
andrew@0 404 double newMaxLocation = posterior.MAPestimate + distanceTravelled;
andrew@0 405 // printf("MAP: %i, tim df %f, distance %f, new location %f\n", posterior.MAPestimate, timeDifference, distanceTravelled, newMaxLocation);
andrew@0 406
andrew@0 407 }
andrew@0 408
andrew@0 409
andrew@0 410 void BayesianArrayStructure::decaySpeedDistribution(double timeDifference){
andrew@0 411
andrew@0 412 // commented for the moment
andrew@0 413 double relativeAmount = max(1.0, timeDifference/1000.);
andrew@0 414 // printf("decay %f around %i \n", timeDifference, relativeSpeedPosterior.MAPestimate);
andrew@0 415 relativeAmount *= speedDecayAmount;
andrew@0 416 relativeSpeedPosterior.renormalise();
andrew@0 417 relativeSpeedPosterior.addGaussianShape(relativeSpeedPosterior.MAPestimate, speedDecayWidth, relativeAmount);
andrew@0 418
andrew@0 419 relativeSpeedPosterior.renormalise();
andrew@0 420 double newMax = relativeSpeedPosterior.getMaximum();
andrew@0 421
andrew@0 422 //old code
andrew@0 423 // relativeSpeedPosterior.addGaussianShape(relativeSpeedPosterior.MAPestimate, speedDecayWidth, 10);
andrew@0 424 //relativeSpeedPosterior.addConstant(1);
andrew@0 425
andrew@0 426 /*
andrew@0 427 relativeSpeedPrior.copyFromDynamicVector(relativeSpeedPosterior);
andrew@0 428 relativeSpeedLikelihood.zero();
andrew@0 429 relativeSpeedLikelihood.addConstant(0.2);
andrew@0 430 relativeSpeedLikelihood.addGaussianShape(relativeSpeedPosterior.maximumValue, speedDecayWidth, relativeAmount);
andrew@0 431 relativeSpeedPosterior.doProduct(relativeSpeedPrior, relativeSpeedLikelihood);
andrew@0 432 relativeSpeedPosterior.renormalise();
andrew@0 433 */
andrew@0 434
andrew@0 435
andrew@0 436
andrew@0 437 }
andrew@0 438
andrew@0 439 void BayesianArrayStructure::setLikelihoodToConstant(){
andrew@0 440 //set new likelihood
andrew@0 441 relativeSpeedLikelihood.zero();
andrew@0 442 relativeSpeedLikelihood.addConstant(speedLikelihoodNoise);
andrew@0 443 }
andrew@0 444
andrew@0 445
andrew@0 446 void BayesianArrayStructure::updateTempoLikelihood(const double& speedRatio, const double& matchFactor){
andrew@0 447
andrew@0 448 //speedratio is speed of played relative to the recording
andrew@0 449
andrew@0 450 double index = relativeSpeedLikelihood.getRealTermsAsIndex(speedRatio);
andrew@0 451 // printf("index of likelihood would be %f for ratio %f\n", index, speedRatio);
andrew@0 452 if (index >= 0 && index < relativeSpeedPrior.length){
andrew@0 453 relativeSpeedLikelihood.addGaussianShape(index , relativeSpeedLikelihoodStdDev, matchFactor);
andrew@0 454 }
andrew@0 455 }
andrew@0 456
andrew@0 457
andrew@0 458
andrew@0 459 void BayesianArrayStructure::updateTempoDistribution(){
andrew@0 460
andrew@0 461 //copy posterior to prior
andrew@0 462 relativeSpeedPrior.copyFromDynamicVector(relativeSpeedPosterior);
andrew@0 463
andrew@0 464 //update
andrew@0 465 relativeSpeedPosterior.doProduct(relativeSpeedPrior, relativeSpeedLikelihood);
andrew@0 466
andrew@0 467 //normalise
andrew@0 468 relativeSpeedPosterior.renormalise();
andrew@0 469
andrew@0 470 relativeSpeedPosterior.getMaximum();
andrew@0 471
andrew@0 472 //relativeSpeedPosterior.updateIntegratedEstimate(); - could be swayed when off to side
andrew@0 473 //so now limited to where there is room sround the MAP estimate
andrew@0 474 relativeSpeedPosterior.updateLimitedIntegratedEstimate();
andrew@0 475
andrew@0 476 speedEstimate = relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate);
andrew@0 477 }
andrew@0 478
andrew@0 479
andrew@0 480 void BayesianArrayStructure::calculateTempoUpdate(){
andrew@0 481 //copy posterior to prior
andrew@0 482 relativeSpeedPrior.copyFromDynamicVector(relativeSpeedPosterior);
andrew@0 483
andrew@0 484 //update
andrew@0 485 relativeSpeedPosterior.doProduct(relativeSpeedPrior, relativeSpeedLikelihood);
andrew@0 486
andrew@0 487 //normalise
andrew@0 488 relativeSpeedPosterior.renormalise();
andrew@0 489
andrew@0 490 relativeSpeedPosterior.getMaximum();
andrew@0 491
andrew@0 492 }
andrew@0 493
andrew@0 494
andrew@0 495 void BayesianArrayStructure::drawArrays(){
andrew@0 496
andrew@0 497 //bayesArray.drawFloatArray(&bayesArray.prior[0], 0, 200);
andrew@0 498 //bayesArray.drawFloatArray(&bayesArray.prior[0], 0, 200);
andrew@0 499
andrew@0 500 int displaySize = prior.arraySize;
andrew@0 501 ofSetColor(0,0,255);
andrew@0 502 prior.drawVector(0, displaySize);
andrew@0 503 ofSetColor(0,255,0);
andrew@0 504 likelihood.drawVector(0, displaySize);
andrew@0 505 ofSetColor(255,0,255);
andrew@0 506 posterior.drawVector(0, displaySize);
andrew@0 507
andrew@0 508
andrew@0 509
andrew@0 510 }
andrew@0 511
andrew@0 512
andrew@0 513 void BayesianArrayStructure::drawTempoArrays(){
andrew@0 514 ofSetColor(0,0,255);
andrew@0 515 // relativeSpeedPrior.drawVector(0, relativeSpeedPrior.arraySize);
andrew@0 516
andrew@0 517 ofSetColor(0,150,255);
andrew@0 518 relativeSpeedLikelihood.drawVector(0, relativeSpeedLikelihood.arraySize);
andrew@0 519
andrew@0 520 // relativeSpeedLikelihood.drawConstrainedVector(0, 199, 0, 1000);// relativeSpeedLikelihood.arraySize);
andrew@0 521 ofSetColor(255,0,0);
andrew@0 522 relativeSpeedPosterior.drawVector(0, relativeSpeedPosterior.arraySize);
andrew@0 523
andrew@0 524 // ofSetColor(0,0,255);
andrew@0 525 // tmpPosteriorForStorage.drawVector(0, tmpPosteriorForStorage.arraySize);
andrew@0 526
andrew@0 527 ofSetColor(255,255, 255);
andrew@0 528 ofLine(screenWidth/2, 0, screenWidth/2, ofGetHeight());//middle of screen
andrew@0 529
andrew@0 530 ofSetColor(25, 0, 250);
andrew@0 531 double fractionOfScreen = ((double)relativeSpeedPosterior.integratedEstimate / relativeSpeedPosterior.length);
andrew@0 532 ofLine(screenWidth * fractionOfScreen, 0, screenWidth * fractionOfScreen, ofGetHeight());
andrew@0 533
andrew@0 534 ofSetColor(255, 0, 20);
andrew@0 535 fractionOfScreen = ((double)relativeSpeedPosterior.MAPestimate / relativeSpeedPosterior.length);
andrew@0 536 ofLine(screenWidth * fractionOfScreen, 0, screenWidth * fractionOfScreen, ofGetHeight());
andrew@0 537
andrew@0 538
andrew@0 539 }
andrew@0 540
andrew@0 541
andrew@0 542 void BayesianArrayStructure::drawArraysRelativeToTimeframe(const double& startTimeMillis, const double& endTimeMillis){
andrew@0 543
andrew@0 544 screenWidth = ofGetWidth();
andrew@0 545
andrew@0 546 int startArrayIndex = 0;
andrew@0 547
andrew@0 548 if (prior.getIndexInRealTerms(prior.arraySize-1) > startTimeMillis){
andrew@0 549 //i.e. the array is on the page
andrew@0 550
andrew@0 551 while (prior.getIndexInRealTerms(startArrayIndex) < startTimeMillis){
andrew@0 552 startArrayIndex++;
andrew@0 553 }
andrew@0 554 int endArrayIndex = prior.arraySize-1;
andrew@0 555 //could find constraints here
andrew@0 556 if (prior.getIndexInRealTerms(prior.arraySize-1) > endTimeMillis)
andrew@0 557 endArrayIndex = (floor)((endTimeMillis - prior.offset)/prior.scalar);
andrew@0 558
andrew@0 559 //so we need to figure where start and end array are on screen
andrew@0 560 int startScreenPosition, endScreenPosition;
andrew@0 561 double screenWidthMillis = endTimeMillis - startTimeMillis;
andrew@0 562
andrew@0 563 startScreenPosition = (prior.getIndexInRealTerms(startArrayIndex) - startTimeMillis)*screenWidth/screenWidthMillis;
andrew@0 564 endScreenPosition = (double)(prior.getIndexInRealTerms(endArrayIndex) - startTimeMillis)*screenWidth/screenWidthMillis;
andrew@0 565
andrew@0 566 ofSetColor(0,0,100);
andrew@0 567 string relativeString = " offset "+ofToString(prior.offset, 1);//starttimes("+ofToString(startTimeMillis)+", "+ofToString(endTimeMillis);
andrew@0 568 relativeString += ": index "+ofToString(startArrayIndex)+" , "+ofToString(endArrayIndex)+" [";
andrew@0 569 // relativeString += ofToString(prior.getIndexInRealTerms(endArrayIndex), 3)+"] (sc-width:"+ofToString(screenWidthMillis, 1)+") ";
andrew@0 570 relativeString += " mapped to screen "+ofToString(startScreenPosition)+" , "+ofToString(endScreenPosition);
andrew@0 571 // ofDrawBitmapString(relativeString, 100, 180);
andrew@0 572
andrew@0 573
andrew@0 574
andrew@0 575 ofSetColor(100,100,100);//255, 255, 0);
andrew@0 576 likelihood.drawConstrainedVector(startArrayIndex, endArrayIndex, startScreenPosition, endScreenPosition);
andrew@0 577
andrew@0 578 // ofSetColor(0,0,200);
andrew@0 579 ofSetColor(170,170,170);//00,200);
andrew@0 580 prior.drawConstrainedVector(startArrayIndex, endArrayIndex, startScreenPosition, endScreenPosition);
andrew@0 581
andrew@0 582 ofSetColor(0,0,150);
andrew@0 583 // ofSetColor(200, 0, 0);
andrew@0 584 posterior.drawConstrainedVector(startArrayIndex, endArrayIndex, startScreenPosition, endScreenPosition);
andrew@0 585
andrew@0 586
andrew@0 587 // ofSetColor(0, 200, 255);
andrew@0 588 // acceleration.drawConstrainedVector(startArrayIndex, endArrayIndex, startScreenPosition, endScreenPosition);
andrew@0 589
andrew@0 590
andrew@0 591 }
andrew@0 592
andrew@0 593 }
andrew@0 594
andrew@0 595
andrew@0 596 /*
andrew@0 597
andrew@0 598 void BayesianArrayStructure::updateTempoDistribution(const double& speedRatio, const double& matchFactor){
andrew@0 599 //speedratio is speed of played relative to the recording
andrew@0 600
andrew@0 601 double index = relativeSpeedLikelihood.getRealTermsAsIndex(speedRatio);
andrew@0 602 // printf("\nindex of likelihood would be %f\n", index);
andrew@0 603 if (index >= 0 && index < relativeSpeedPrior.length){
andrew@0 604 //then we can do update
andrew@0 605
andrew@0 606 //set new likelihood
andrew@0 607 relativeSpeedLikelihood.zero();
andrew@0 608 relativeSpeedLikelihood.addConstant(speedLikelihoodNoise);
andrew@0 609
andrew@0 610 relativeSpeedLikelihood.addGaussianShape(index , 5, 0.5*matchFactor);
andrew@0 611
andrew@0 612
andrew@0 613 //copy posterior to prior
andrew@0 614 relativeSpeedPrior.copyFromDynamicVector(relativeSpeedPosterior);
andrew@0 615
andrew@0 616 //update
andrew@0 617 relativeSpeedPosterior.doProduct(relativeSpeedPrior, relativeSpeedLikelihood);
andrew@0 618
andrew@0 619 //normalise
andrew@0 620 relativeSpeedPosterior.renormalise();
andrew@0 621
andrew@0 622 relativeSpeedPosterior.getMaximum();
andrew@0 623 }//end if within range
andrew@0 624
andrew@0 625
andrew@0 626 }
andrew@0 627
andrew@0 628 */