annotate bayesianArraySrc/BayesianArrayStructure.cpp @ 36:eb43b2a007ea

changed newMatchOnset to have different way to calculate onsets vs noise, using the onsetLikelihoodNoise factor
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Tue, 24 Apr 2012 01:19:24 +0100
parents 02f659277346
children 9806a4f22fd0
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@7 19 updatingSpeedDistribution = false;
andrew@0 20
andrew@0 21 relativeSpeedLikelihoodStdDev = 5.0;
andrew@0 22
andrew@0 23 prior.createVector(1);
andrew@0 24 likelihood.createVector(1);
andrew@0 25 posterior.createVector(1);
andrew@0 26
andrew@0 27 speedPriorValue = 1.0;//default value for the speed prior
andrew@0 28 speedEstimate = speedPriorValue;
andrew@0 29
andrew@0 30 lastEventTime = 0;//ofGetElapsedTimeMillis();
andrew@0 31
andrew@0 32 tmpBestEstimate = 0;
andrew@5 33 crossUpdateTimeThreshold = 60;
andrew@36 34 priorWidth = 12;
andrew@36 35
andrew@36 36
andrew@0 37
andrew@0 38 }
andrew@0 39
andrew@0 40 BayesianArrayStructure::BayesianArrayStructure(int length){
andrew@0 41 printf("BAYESIAN STURCTURE CREATED LENGTH: %i\n", length);
andrew@0 42 //this constructor isnt called it seems
andrew@0 43 prior.createVector(length);
andrew@0 44 likelihood.createVector(length);
andrew@0 45 posterior.createVector(length);
andrew@0 46
andrew@0 47 lastEventTime = 0;
andrew@0 48
andrew@0 49
andrew@0 50 }
andrew@0 51
andrew@0 52
andrew@0 53
andrew@0 54 void BayesianArrayStructure::resetSize(int length){
andrew@0 55 printf("BAYESIAN STRUCTURE size is : %i\n", length);
andrew@0 56
andrew@0 57 prior.createVector(length);
andrew@0 58 likelihood.createVector(length);
andrew@0 59 posterior.createVector(length);
andrew@0 60
andrew@0 61 acceleration.createVector(length);
andrew@36 62 printf("scalar %f\n", posterior.scalar);
andrew@0 63
andrew@0 64 }
andrew@0 65
andrew@0 66 void BayesianArrayStructure::resetSpeedSize(int length){
andrew@0 67 printf("BAYESIAN reset SPEED size is : %i\n", length);
andrew@0 68
andrew@0 69 relativeSpeedPrior.createVector(length);
andrew@0 70 relativeSpeedLikelihood.createVector(length);
andrew@0 71 relativeSpeedPosterior.createVector(length);
andrew@0 72 tmpPosteriorForStorage.createVector(length);
andrew@0 73
andrew@0 74
andrew@0 75
andrew@0 76 }
andrew@0 77
andrew@0 78 void BayesianArrayStructure::setRelativeSpeedScalar(double f){
andrew@0 79 relativeSpeedPrior.scalar = f;
andrew@0 80 relativeSpeedPosterior.scalar = f;
andrew@0 81 relativeSpeedLikelihood.scalar = f;
andrew@0 82 }
andrew@0 83
andrew@0 84 void BayesianArrayStructure::resetSpeedToOne(){
andrew@0 85 relativeSpeedPrior.zero();
andrew@0 86 relativeSpeedPosterior.zero();
andrew@0 87 relativeSpeedLikelihood.zero();
andrew@0 88
andrew@0 89
andrew@20 90 // relativeSpeedPosterior.addGaussianShape(100, 8, 0.1);
andrew@20 91 // relativeSpeedPosterior.renormalise();
andrew@20 92 // relativeSpeedPosterior.getMaximum();
andrew@0 93
andrew@0 94 setSpeedPrior(speedPriorValue);
andrew@0 95 speedEstimate = speedPriorValue;
andrew@0 96
andrew@0 97 prior.zero();
andrew@0 98 posterior.zero();
andrew@0 99
andrew@0 100 posterior.addToIndex(0, 1);
andrew@0 101 posterior.renormalise();
andrew@0 102
andrew@0 103 }
andrew@0 104
andrew@0 105 void BayesianArrayStructure::setSpeedPrior(double f){
andrew@0 106 speedPriorValue = f;
andrew@0 107 int index = relativeSpeedPosterior.getRealTermsAsIndex(speedPriorValue);
andrew@0 108 relativeSpeedPosterior.zero();
andrew@20 109 relativeSpeedPosterior.addGaussianShape(index, priorWidth, 0.9);
andrew@36 110 relativeSpeedPosterior.addGaussianShape(relativeSpeedPosterior.getRealTermsAsIndex(1), 4, 1);
andrew@36 111
andrew@4 112 printf("speed adding to index for 1 = %f\n", relativeSpeedPosterior.getRealTermsAsIndex(1));
andrew@36 113 // relativeSpeedPosterior.addToIndex(relativeSpeedPosterior.getRealTermsAsIndex(1), 0.5);
andrew@20 114 //relativeSpeedPosterior.addGaussianShapeFromRealTime(1, 3, 0.5);
andrew@11 115
andrew@11 116
andrew@11 117 //tmp debug test
andrew@11 118 //relativeSpeedPosterior.addToIndex(relativeSpeedPosterior.getRealTermsAsIndex(1.8), 0.15);
andrew@4 119
andrew@0 120 relativeSpeedPosterior.renormalise();
andrew@0 121 relativeSpeedPosterior.getMaximum();
andrew@11 122
andrew@11 123
andrew@0 124 relativeSpeedPrior.copyFromDynamicVector(relativeSpeedPosterior);
andrew@11 125
andrew@11 126
andrew@11 127
andrew@0 128 printf("BAYES STRUCTU ' SPEED PRIOR %f . index %i\n", speedPriorValue, index);
andrew@0 129
andrew@0 130 }
andrew@0 131
andrew@0 132
andrew@0 133 void BayesianArrayStructure::setPositionDistributionScalar(double f){
andrew@0 134 if (f > 0){
andrew@0 135 prior.scalar = f;
andrew@0 136 posterior.scalar = f;
andrew@0 137 likelihood.scalar = f;
andrew@36 138 printf("SET POS DISTBN SCALAR %f\n", f);
andrew@0 139 }
andrew@0 140 }
andrew@0 141
andrew@0 142 void BayesianArrayStructure::simpleExample(){
andrew@0 143 relativeSpeedPosterior.getMaximum();
andrew@0 144 relativeSpeedPrior.copyFromDynamicVector(relativeSpeedPosterior);
andrew@0 145 }
andrew@0 146
andrew@0 147 void BayesianArrayStructure::copyPriorToPosterior(){
andrew@0 148
andrew@0 149 for (int i = 0;i < prior.arraySize;i++){
andrew@0 150 posterior.array[i] = prior.array[i];
andrew@0 151 }
andrew@0 152 }
andrew@0 153
andrew@0 154 void BayesianArrayStructure::setStartPlaying(){
andrew@0 155
andrew@0 156 lastEventTime = 0;
andrew@0 157 bestEstimate = 0;
andrew@0 158 lastBestEstimateUpdateTime = 0;
andrew@3 159
andrew@0 160 if (*realTimeMode)
andrew@0 161 lastBestEstimateUpdateTime = ofGetElapsedTimeMillis();
andrew@0 162 //cannot just be zero - offline bug
andrew@0 163 //printf("start playing - best estimate %f\n", lastBestEstimateUpdateTime);
andrew@0 164
andrew@0 165 resetArrays();
andrew@0 166 }
andrew@0 167
andrew@0 168 void BayesianArrayStructure::resetArrays(){
andrew@0 169 //called when we start playing
andrew@0 170
andrew@0 171 prior.zero();
andrew@0 172 likelihood.zero();
andrew@0 173 posterior.zero();
andrew@0 174
andrew@0 175 updateCounter = 0;
andrew@0 176
andrew@0 177 posterior.offset = -1000;
andrew@0 178 setNewDistributionOffsets(0);
andrew@0 179
andrew@0 180 int zeroIndex = posterior.getRealTermsAsIndex(0);
andrew@3 181 printf("ZERO INDEX %i\n", zeroIndex);
andrew@0 182
andrew@31 183 posterior.addGaussianShapeFromRealTime(0, 50, 1);//one way to add at x msec
andrew@7 184 // posterior.addGaussianShape(posterior.getRealTermsAsIndex(10), 50, 1);//alternative way
andrew@3 185
andrew@0 186 likelihood.addConstant(1);
andrew@0 187
andrew@0 188 updateCounter = 0;
andrew@0 189
andrew@0 190
andrew@0 191 printf("bayes reset arrays - best estimate %f\n", lastBestEstimateUpdateTime);
andrew@0 192
andrew@0 193 setSpeedPrior(speedPriorValue);
andrew@3 194 relativeSpeedPosterior.copyFromDynamicVector(relativeSpeedPrior);
andrew@3 195
andrew@0 196 }
andrew@0 197
andrew@0 198
andrew@0 199 void BayesianArrayStructure::zeroArrays(){
andrew@0 200 prior.zero();
andrew@0 201 likelihood.zero();
andrew@0 202 posterior.zero();
andrew@0 203
andrew@0 204 relativeSpeedPrior.zero();
andrew@0 205 relativeSpeedPosterior.zero();
andrew@0 206 relativeSpeedLikelihood.zero();
andrew@0 207
andrew@0 208 }
andrew@0 209
andrew@4 210 /*
andrew@0 211 void BayesianArrayStructure::updateTmpBestEstimate(const double& timeDifference){
andrew@0 212 //input is the time since the start of playing
andrew@0 213 // double timeDiff = ofGetElapsedTimeMillis() - lastEventTime;//lastBestEstimateUpdateTime;
andrew@0 214 double timeDiff = timeDifference;
andrew@0 215 if (*realTimeMode)
andrew@0 216 timeDiff = ofGetElapsedTimeMillis() - lastBestEstimateUpdateTime;
andrew@0 217
andrew@0 218 double tmp = relativeSpeedPosterior.getIntegratedEstimate();
andrew@0 219 tmpBestEstimate = posterior.getIndexInRealTerms(posterior.MAPestimate) + timeDiff*relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate);
andrew@0 220 //
andrew@0 221 //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 222 //lastBestEstimateUpdateTime = ofGetElapsedTimeMillis();
andrew@0 223 }
andrew@4 224 */
andrew@4 225
andrew@0 226 void BayesianArrayStructure::updateBestEstimate(const double& timeDifference){
andrew@0 227 // double timeDiff = ofGetElapsedTimeMillis() - lastEventTime;//
andrew@3 228 double tmp = bestEstimate;
andrew@10 229 // printf("best est routine: posterior offset %f\n", posterior.offset);
andrew@0 230
andrew@0 231 double timeDiff = timeDifference;
andrew@0 232
andrew@0 233 //Using timedifferencfe here will make it go wrong. Is time since beginning of playing
andrew@0 234
andrew@0 235 if (*realTimeMode)
andrew@0 236 timeDiff = ofGetElapsedTimeMillis() - lastBestEstimateUpdateTime;
andrew@0 237
andrew@0 238 //lastbest is time we started playing
andrew@4 239 /*
andrew@0 240 if (usingIntegratedTempoEstimate)
andrew@0 241 speedEstimateIndex = relativeSpeedPosterior.getIntegratedEstimate();
andrew@0 242 else
andrew@4 243 speedEstimateIndex = relativeSpeedPosterior.getMAPestimate();
andrew@4 244 */
andrew@4 245 speedEstimateIndex = getSpeedEstimateIndex();
andrew@0 246
andrew@0 247 speedEstimate = relativeSpeedPosterior.getIndexInRealTerms(speedEstimateIndex);
andrew@0 248 bestEstimate = posterior.getIndexInRealTerms(posterior.MAPestimate) + timeDiff*speedEstimate;
andrew@0 249
andrew@10 250 // 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@10 251 // posterior.MAPestimate, posterior.getIndexInRealTerms(posterior.MAPestimate), speedEstimateIndex, speedEstimate, timeDiff*speedEstimate);
andrew@17 252
andrew@17 253 printf("BEST ESTIMATE %f\n", bestEstimate);
andrew@0 254 }
andrew@0 255
andrew@10 256
andrew@0 257 void BayesianArrayStructure::calculatePosterior(){
andrew@0 258 //posterior.doProduct(prior, likelihood);
andrew@4 259 assert(posterior.offset == prior.offset);
andrew@0 260
andrew@0 261 int i;
andrew@0 262 for (i = 0;i < posterior.length;i++){
andrew@0 263 posterior.array[i] = likelihood.array[i] * prior.array[i];
andrew@0 264 }
andrew@0 265
andrew@0 266 posterior.renormalise();
andrew@0 267
andrew@17 268 // printf("After CALC");
andrew@17 269 // printPostOffset();
andrew@6 270
andrew@0 271 }
andrew@0 272
andrew@0 273
andrew@4 274 double BayesianArrayStructure::getSpeedEstimateIndex(){
andrew@4 275 if (usingIntegratedTempoEstimate)
andrew@4 276 return relativeSpeedPosterior.getIntegratedEstimate();
andrew@4 277 else
andrew@4 278 return relativeSpeedPosterior.getMAPestimate();
andrew@4 279 }
andrew@0 280
andrew@0 281
andrew@0 282 void BayesianArrayStructure::setNewDistributionOffsets(const double& newOffset){
andrew@4 283
andrew@4 284 printf("prior offset was %f now %f\n", prior.offset, newOffset);
andrew@4 285
andrew@0 286 prior.offset = newOffset;
andrew@0 287 likelihood.offset = newOffset;
andrew@4 288
andrew@0 289 //posterior.offset = newOffset;
andrew@0 290 }
andrew@0 291
andrew@0 292
andrew@6 293 void BayesianArrayStructure::updateBayesianDistributions(const double& newEventTime){
andrew@6 294
andrew@6 295 //NEED TO CHECK HERE THAT THEY HAVE THE SAME OFFSETS
andrew@6 296 prior.copyFromDynamicVector(posterior);//try the otehr way
andrew@6 297
andrew@6 298 //bayesianStruct.copyPriorToPosterior();
andrew@6 299 //need to get new MAP position and set the offset of the arrays
andrew@6 300 //currently bestEstimate is the approx for the new MAP position
andrew@6 301 // int tmpMap = bayesianStruct.posterior.getMAPestimate();
andrew@6 302
andrew@6 303 double timeDifference = newEventTime - lastEventTime;
andrew@17 304 // printf("updating distributions at time %f diff %f offset %f\n", newEventTime, timeDifference, posterior.offset);
andrew@6 305
andrew@6 306 //addnoise to the tempo distribution
andrew@6 307 //bayesianStruct.decaySpeedDistribution(timeDifference);
andrew@6 308
andrew@7 309 if (timeDifference > 50 && updatingSpeedDistribution){
andrew@6 310 addGaussianNoiseToSpeedPosterior(timeDifference * 10.0 / 100.);
andrew@6 311 }
andrew@6 312
andrew@6 313 printPostOffset();
andrew@6 314
andrew@6 315 updateBestEstimate(timeDifference);
andrew@6 316 lastBestEstimateUpdateTime = newEventTime;//getTimeNow(timePlayed);
andrew@6 317
andrew@8 318 //set TARGETS - commented tenmporarily
andrew@8 319 setNewDistributionOffsets(max(0., bestEstimate - (prior.scalar*prior.arraySize/2)));
andrew@6 320
andrew@6 321 crossUpdateArrays(posterior, relativeSpeedPosterior, timeDifference);
andrew@6 322
andrew@6 323 //i.e. using the same offset as prior
andrew@6 324 posterior.offset = prior.offset;//
andrew@6 325
andrew@6 326 // float tmpPrior = max(0., bestEstimate - (prior.scalar*prior.arraySize/2));// prior.offset = max(0., bestEstimate - (prior.scalar*prior.arraySize/2));
andrew@6 327 // printf("Using prior offset of %f not %f\n", tmpPrior, prior.offset);
andrew@6 328
andrew@6 329 lastEventTime = newEventTime;//lastEventTime = ofGetElapsedTimeMillis();
andrew@6 330
andrew@6 331 }
andrew@6 332
andrew@6 333
andrew@10 334
andrew@10 335
andrew@0 336 void BayesianArrayStructure::crossUpdateArrays(DynamicVector& position, DynamicVector& speed, double timeDifference){
andrew@8 337
andrew@0 338 //set the cutoff for offset of position first! XXX
andrew@0 339
andrew@0 340 double timeDifferenceInPositionVectorUnits = timeDifference / prior.scalar;
andrew@0 341
andrew@17 342 //printf("CROSS UPDATE time diff %f ms is %f units; ", timeDifference, timeDifferenceInPositionVectorUnits);
andrew@0 343 prior.zero();//kill prior
andrew@4 344
andrew@4 345 // calculateNewPriorOffset(timeDifference);//dioesnt do anything
andrew@4 346
andrew@5 347 // printf("new prior offset %f and post offset %f\n", prior.offset, posterior.offset);
andrew@0 348
andrew@0 349 if (timeDifferenceInPositionVectorUnits > crossUpdateTimeThreshold)
andrew@0 350 complexCrossUpdate(timeDifferenceInPositionVectorUnits);
andrew@0 351 else
andrew@0 352 translateByMaximumSpeed(timeDifferenceInPositionVectorUnits);
andrew@0 353
andrew@0 354
andrew@0 355 updateCounter++;
andrew@8 356 prior.renormalise();//not strictly necessary??
andrew@0 357
andrew@0 358 }
andrew@0 359
andrew@0 360 void BayesianArrayStructure::complexCrossUpdate(const double& timeDifferenceInPositionVectorUnits){
andrew@4 361
andrew@5 362
andrew@17 363 // printf("before cross c : posterior map is %i = %f ms time diff pos vec %f\n", posterior.getMAPestimate(), posterior.getIndexInRealTerms(prior.getMAPestimate()), timeDifferenceInPositionVectorUnits);
andrew@5 364
andrew@0 365 int distanceMoved, newPriorIndex;
andrew@0 366
andrew@0 367 double speedValue = relativeSpeedPosterior.offset;
andrew@0 368
andrew@0 369 for (int i = 0;i < relativeSpeedPosterior.arraySize;i++){
andrew@0 370
andrew@0 371 // double speedValue = relativeSpeedPosterior.getIndexInRealTerms(i);//so for scalar 0.01, 50 -> speed value of 0.5
andrew@0 372
andrew@0 373 //so we have moved
andrew@0 374 distanceMoved = round(timeDifferenceInPositionVectorUnits * speedValue);//round the value
andrew@5 375 // printf("Speed value %f time %f gives distance %i\n", speedValue, timeDifferenceInPositionVectorUnits, distanceMoved);
andrew@5 376
andrew@0 377 if (relativeSpeedPosterior.array[i] != 0){
andrew@4 378
andrew@0 379 double speedContribution = relativeSpeedPosterior.array[i];
andrew@0 380
andrew@4 381 // printf("speed [%i](val[%f]) gives %f moved %i in %f units \n", i, relativeSpeedPosterior.array[i], speedValue, distanceMoved, timeDifferenceInPositionVectorUnits);
andrew@0 382
andrew@4 383 //1/2/12 deleted line
andrew@15 384 newPriorIndex = (int)posterior.millisToVectorUnits(posterior.offset - prior.offset) + distanceMoved;//i.e. where post[0] goes to in terms of prior at this speed
andrew@5 385 int postIndex = 0;//index of posterior that will contribute
andrew@5 386
andrew@4 387 while (postIndex < posterior.arraySize && newPriorIndex < prior.arraySize){
andrew@4 388
andrew@4 389 //did use a for loop
andrew@4 390 // for (postIndex = 0;postIndex < posterior.arraySize;postIndex++){
andrew@0 391 //old posterior contributing to new prior
andrew@4 392
andrew@4 393 //would use this method
andrew@4 394 //newPriorIndex = postIndex + posterior.offset - prior.offset + distanceMoved;
andrew@0 395
andrew@4 396 if (newPriorIndex >= 0){
andrew@0 397 prior.addToIndex(newPriorIndex, posterior.array[postIndex]*speedContribution);
andrew@4 398 // 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 399 }
andrew@4 400 //but we actually do this for simplicity
andrew@4 401 newPriorIndex++;
andrew@4 402 postIndex++;
andrew@4 403 }//end for. now while
andrew@0 404
andrew@0 405
andrew@0 406 }//if not zero
andrew@5 407
andrew@5 408 speedValue += relativeSpeedPosterior.scalar;//optimised line
andrew@0 409 //as we wanted:
andrew@0 410 // double speedValue = relativeSpeedPosterior.getIndexInRealTerms(i);//so for scalar 0.01, 50 -> speed value of 0.5
andrew@5 411
andrew@0 412 }//end speed
andrew@5 413
andrew@5 414
andrew@17 415 // printf("after cross c : prior map is %i = %f ms\n", prior.getMAPestimate(), prior.getIndexInRealTerms(prior.getMAPestimate()));
andrew@0 416 }
andrew@0 417
andrew@0 418
andrew@0 419
andrew@0 420 void BayesianArrayStructure::translateByMaximumSpeed(const double& timeDifferenceInPositionVectorUnits){
andrew@0 421
andrew@8 422
andrew@0 423 int distanceMoved, newPriorIndex;
andrew@8 424 double speedIndex = getSpeedEstimateIndex();
andrew@8 425 double speedValue = relativeSpeedPosterior.getIndexInRealTerms(speedIndex);
andrew@0 426
andrew@8 427 // double speedValue = relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate);
andrew@8 428
andrew@0 429 //so for scalar 0.01, 50 -> speed value of 0.5
andrew@8 430 double speedContribution = relativeSpeedPosterior.array[(int)round(speedIndex)];
andrew@0 431 //so we have moved
andrew@0 432 distanceMoved = round(timeDifferenceInPositionVectorUnits * speedValue);//round the value
andrew@8 433
andrew@8 434 // printf("speed [%i] gives %f moved %i in %f units \n", i, speedValue, distanceMoved, timeDifferenceInPositionVectorUnits);
andrew@0 435
andrew@0 436 for (int postIndex = 0;postIndex < posterior.arraySize;postIndex++){
andrew@0 437 //old posterior contributing to new prior
andrew@15 438 newPriorIndex = (int)round(posterior.millisToVectorUnits(posterior.offset - prior.offset)) + postIndex + distanceMoved;
andrew@0 439 if (newPriorIndex >= 0 && newPriorIndex < prior.arraySize){
andrew@0 440 prior.addToIndex(newPriorIndex, posterior.array[postIndex]*speedContribution);
andrew@0 441 }
andrew@0 442
andrew@0 443 }
andrew@0 444
andrew@0 445 }
andrew@0 446
andrew@0 447 void BayesianArrayStructure::addGaussianNoiseToSpeedPosterior(const double& std_dev){
andrew@0 448 tmpPosteriorForStorage.copyFromDynamicVector(relativeSpeedPosterior);
andrew@0 449
andrew@0 450 for (int i = 0;i < relativeSpeedPosterior.length;i++){
andrew@0 451 tmpPosteriorForStorage.addGaussianShape(i, std_dev, relativeSpeedPosterior.array[i]);
andrew@0 452 }
andrew@0 453
andrew@0 454 tmpPosteriorForStorage.renormalise();
andrew@0 455
andrew@0 456 relativeSpeedPosterior.copyFromDynamicVector(tmpPosteriorForStorage);
andrew@0 457 }
andrew@0 458
andrew@0 459
andrew@0 460 void BayesianArrayStructure::addTriangularNoiseToSpeedPosterior(const double& std_dev){
andrew@0 461 tmpPosteriorForStorage.copyFromDynamicVector(relativeSpeedPosterior);
andrew@0 462
andrew@0 463 for (int i = 0;i < relativeSpeedPosterior.length;i++){
andrew@0 464 //adding a linear amount depending on distance
andrew@0 465 tmpPosteriorForStorage.addTriangularShape(i, std_dev*2.0, relativeSpeedPosterior.array[i]);
andrew@0 466 }
andrew@0 467
andrew@0 468 tmpPosteriorForStorage.renormalise();
andrew@0 469
andrew@0 470 relativeSpeedPosterior.copyFromDynamicVector(tmpPosteriorForStorage);
andrew@0 471 }
andrew@0 472
andrew@0 473 void BayesianArrayStructure::calculateNewPriorOffset(const double& timeDifference){
andrew@0 474
andrew@4 475 // double maxSpeed = relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate);
andrew@4 476
andrew@4 477 double maxSpeed = relativeSpeedPosterior.getIndexInRealTerms(getSpeedEstimateIndex());//either integrated or MAP
andrew@0 478 // printf("Maxspeed is %f\n", maxSpeed);
andrew@0 479
andrew@0 480 double priorMax = posterior.getMaximum();
andrew@0 481 double distanceTravelled = maxSpeed * (timeDifference / prior.scalar);
andrew@0 482 double newMaxLocation = posterior.MAPestimate + distanceTravelled;
andrew@0 483 // printf("MAP: %i, tim df %f, distance %f, new location %f\n", posterior.MAPestimate, timeDifference, distanceTravelled, newMaxLocation);
andrew@0 484
andrew@0 485 }
andrew@0 486
andrew@0 487
andrew@0 488 void BayesianArrayStructure::decaySpeedDistribution(double timeDifference){
andrew@0 489
andrew@0 490 // commented for the moment
andrew@0 491 double relativeAmount = max(1.0, timeDifference/1000.);
andrew@0 492 // printf("decay %f around %i \n", timeDifference, relativeSpeedPosterior.MAPestimate);
andrew@0 493 relativeAmount *= speedDecayAmount;
andrew@0 494 relativeSpeedPosterior.renormalise();
andrew@0 495 relativeSpeedPosterior.addGaussianShape(relativeSpeedPosterior.MAPestimate, speedDecayWidth, relativeAmount);
andrew@0 496
andrew@0 497 relativeSpeedPosterior.renormalise();
andrew@0 498 double newMax = relativeSpeedPosterior.getMaximum();
andrew@0 499
andrew@0 500 //old code
andrew@0 501 // relativeSpeedPosterior.addGaussianShape(relativeSpeedPosterior.MAPestimate, speedDecayWidth, 10);
andrew@0 502 //relativeSpeedPosterior.addConstant(1);
andrew@0 503
andrew@0 504 /*
andrew@0 505 relativeSpeedPrior.copyFromDynamicVector(relativeSpeedPosterior);
andrew@0 506 relativeSpeedLikelihood.zero();
andrew@0 507 relativeSpeedLikelihood.addConstant(0.2);
andrew@0 508 relativeSpeedLikelihood.addGaussianShape(relativeSpeedPosterior.maximumValue, speedDecayWidth, relativeAmount);
andrew@0 509 relativeSpeedPosterior.doProduct(relativeSpeedPrior, relativeSpeedLikelihood);
andrew@0 510 relativeSpeedPosterior.renormalise();
andrew@0 511 */
andrew@0 512
andrew@0 513
andrew@0 514
andrew@0 515 }
andrew@0 516
andrew@0 517 void BayesianArrayStructure::setLikelihoodToConstant(){
andrew@0 518 //set new likelihood
andrew@0 519 relativeSpeedLikelihood.zero();
andrew@0 520 relativeSpeedLikelihood.addConstant(speedLikelihoodNoise);
andrew@0 521 }
andrew@0 522
andrew@0 523
andrew@0 524 void BayesianArrayStructure::updateTempoLikelihood(const double& speedRatio, const double& matchFactor){
andrew@0 525
andrew@0 526 //speedratio is speed of played relative to the recording
andrew@0 527
andrew@0 528 double index = relativeSpeedLikelihood.getRealTermsAsIndex(speedRatio);
andrew@0 529 // printf("index of likelihood would be %f for ratio %f\n", index, speedRatio);
andrew@0 530 if (index >= 0 && index < relativeSpeedPrior.length){
andrew@0 531 relativeSpeedLikelihood.addGaussianShape(index , relativeSpeedLikelihoodStdDev, matchFactor);
andrew@0 532 }
andrew@0 533 }
andrew@0 534
andrew@0 535
andrew@0 536
andrew@0 537 void BayesianArrayStructure::updateTempoDistribution(){
andrew@0 538
andrew@0 539 //copy posterior to prior
andrew@0 540 relativeSpeedPrior.copyFromDynamicVector(relativeSpeedPosterior);
andrew@0 541
andrew@0 542 //update
andrew@0 543 relativeSpeedPosterior.doProduct(relativeSpeedPrior, relativeSpeedLikelihood);
andrew@0 544
andrew@0 545 //normalise
andrew@0 546 relativeSpeedPosterior.renormalise();
andrew@0 547
andrew@0 548 relativeSpeedPosterior.getMaximum();
andrew@0 549
andrew@0 550 //relativeSpeedPosterior.updateIntegratedEstimate(); - could be swayed when off to side
andrew@0 551 //so now limited to where there is room sround the MAP estimate
andrew@0 552 relativeSpeedPosterior.updateLimitedIntegratedEstimate();
andrew@0 553
andrew@0 554 speedEstimate = relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate);
andrew@0 555 }
andrew@0 556
andrew@0 557
andrew@0 558 void BayesianArrayStructure::calculateTempoUpdate(){
andrew@0 559 //copy posterior to prior
andrew@0 560 relativeSpeedPrior.copyFromDynamicVector(relativeSpeedPosterior);
andrew@0 561
andrew@0 562 //update
andrew@0 563 relativeSpeedPosterior.doProduct(relativeSpeedPrior, relativeSpeedLikelihood);
andrew@0 564
andrew@0 565 //normalise
andrew@0 566 relativeSpeedPosterior.renormalise();
andrew@0 567
andrew@0 568 relativeSpeedPosterior.getMaximum();
andrew@0 569
andrew@0 570 }
andrew@0 571
andrew@0 572
andrew@0 573 void BayesianArrayStructure::drawArrays(){
andrew@0 574
andrew@0 575 //bayesArray.drawFloatArray(&bayesArray.prior[0], 0, 200);
andrew@0 576 //bayesArray.drawFloatArray(&bayesArray.prior[0], 0, 200);
andrew@0 577
andrew@0 578 int displaySize = prior.arraySize;
andrew@0 579 ofSetColor(0,0,255);
andrew@0 580 prior.drawVector(0, displaySize);
andrew@0 581 ofSetColor(0,255,0);
andrew@0 582 likelihood.drawVector(0, displaySize);
andrew@0 583 ofSetColor(255,0,255);
andrew@0 584 posterior.drawVector(0, displaySize);
andrew@0 585
andrew@0 586
andrew@0 587
andrew@0 588 }
andrew@0 589
andrew@0 590
andrew@0 591 void BayesianArrayStructure::drawTempoArrays(){
andrew@0 592 ofSetColor(0,0,255);
andrew@0 593 // relativeSpeedPrior.drawVector(0, relativeSpeedPrior.arraySize);
andrew@0 594
andrew@0 595 ofSetColor(0,150,255);
andrew@0 596 relativeSpeedLikelihood.drawVector(0, relativeSpeedLikelihood.arraySize);
andrew@0 597
andrew@0 598 // relativeSpeedLikelihood.drawConstrainedVector(0, 199, 0, 1000);// relativeSpeedLikelihood.arraySize);
andrew@0 599 ofSetColor(255,0,0);
andrew@0 600 relativeSpeedPosterior.drawVector(0, relativeSpeedPosterior.arraySize);
andrew@0 601
andrew@0 602 // ofSetColor(0,0,255);
andrew@0 603 // tmpPosteriorForStorage.drawVector(0, tmpPosteriorForStorage.arraySize);
andrew@0 604
andrew@0 605 ofSetColor(255,255, 255);
andrew@0 606 ofLine(screenWidth/2, 0, screenWidth/2, ofGetHeight());//middle of screen
andrew@0 607
andrew@0 608 ofSetColor(25, 0, 250);
andrew@0 609 double fractionOfScreen = ((double)relativeSpeedPosterior.integratedEstimate / relativeSpeedPosterior.length);
andrew@0 610 ofLine(screenWidth * fractionOfScreen, 0, screenWidth * fractionOfScreen, ofGetHeight());
andrew@0 611
andrew@0 612 ofSetColor(255, 0, 20);
andrew@0 613 fractionOfScreen = ((double)relativeSpeedPosterior.MAPestimate / relativeSpeedPosterior.length);
andrew@0 614 ofLine(screenWidth * fractionOfScreen, 0, screenWidth * fractionOfScreen, ofGetHeight());
andrew@0 615
andrew@0 616
andrew@0 617 }
andrew@0 618
andrew@0 619
andrew@0 620 void BayesianArrayStructure::drawArraysRelativeToTimeframe(const double& startTimeMillis, const double& endTimeMillis){
andrew@0 621
andrew@0 622 screenWidth = ofGetWidth();
andrew@0 623
andrew@0 624 int startArrayIndex = 0;
andrew@0 625
andrew@0 626 if (prior.getIndexInRealTerms(prior.arraySize-1) > startTimeMillis){
andrew@0 627 //i.e. the array is on the page
andrew@0 628
andrew@0 629 while (prior.getIndexInRealTerms(startArrayIndex) < startTimeMillis){
andrew@0 630 startArrayIndex++;
andrew@0 631 }
andrew@0 632 int endArrayIndex = prior.arraySize-1;
andrew@0 633 //could find constraints here
andrew@0 634 if (prior.getIndexInRealTerms(prior.arraySize-1) > endTimeMillis)
andrew@0 635 endArrayIndex = (floor)((endTimeMillis - prior.offset)/prior.scalar);
andrew@0 636
andrew@0 637 //so we need to figure where start and end array are on screen
andrew@0 638 int startScreenPosition, endScreenPosition;
andrew@0 639 double screenWidthMillis = endTimeMillis - startTimeMillis;
andrew@0 640
andrew@0 641 startScreenPosition = (prior.getIndexInRealTerms(startArrayIndex) - startTimeMillis)*screenWidth/screenWidthMillis;
andrew@0 642 endScreenPosition = (double)(prior.getIndexInRealTerms(endArrayIndex) - startTimeMillis)*screenWidth/screenWidthMillis;
andrew@0 643
andrew@0 644 ofSetColor(0,0,100);
andrew@0 645 string relativeString = " offset "+ofToString(prior.offset, 1);//starttimes("+ofToString(startTimeMillis)+", "+ofToString(endTimeMillis);
andrew@0 646 relativeString += ": index "+ofToString(startArrayIndex)+" , "+ofToString(endArrayIndex)+" [";
andrew@0 647 // relativeString += ofToString(prior.getIndexInRealTerms(endArrayIndex), 3)+"] (sc-width:"+ofToString(screenWidthMillis, 1)+") ";
andrew@0 648 relativeString += " mapped to screen "+ofToString(startScreenPosition)+" , "+ofToString(endScreenPosition);
andrew@0 649 // ofDrawBitmapString(relativeString, 100, 180);
andrew@0 650
andrew@0 651
andrew@0 652
andrew@0 653 ofSetColor(100,100,100);//255, 255, 0);
andrew@0 654 likelihood.drawConstrainedVector(startArrayIndex, endArrayIndex, startScreenPosition, endScreenPosition);
andrew@0 655
andrew@0 656 // ofSetColor(0,0,200);
andrew@0 657 ofSetColor(170,170,170);//00,200);
andrew@0 658 prior.drawConstrainedVector(startArrayIndex, endArrayIndex, startScreenPosition, endScreenPosition);
andrew@0 659
andrew@0 660 ofSetColor(0,0,150);
andrew@0 661 // ofSetColor(200, 0, 0);
andrew@0 662 posterior.drawConstrainedVector(startArrayIndex, endArrayIndex, startScreenPosition, endScreenPosition);
andrew@0 663
andrew@0 664
andrew@0 665 // ofSetColor(0, 200, 255);
andrew@0 666 // acceleration.drawConstrainedVector(startArrayIndex, endArrayIndex, startScreenPosition, endScreenPosition);
andrew@0 667
andrew@0 668
andrew@0 669 }
andrew@0 670
andrew@0 671 }
andrew@0 672
andrew@0 673
andrew@6 674 void BayesianArrayStructure::printPostOffset(){
andrew@6 675 double tmp = posterior.getMAPestimate();
andrew@6 676 printf(" MAP index %i post offset %f == %f ms\n", posterior.MAPestimate, posterior.offset, posterior.getIndexInRealTerms(posterior.MAPestimate));
andrew@6 677 }
andrew@6 678
andrew@10 679 //PROJECT PRIOR CODE
andrew@11 680
andrew@11 681 void BayesianArrayStructure::projectDistribution(const double& newEventTime, const double& newAlignmentPosition, DynamicVector& projectedPrior){
andrew@10 682
andrew@10 683 projectedPrior.copyFromDynamicVector(posterior);
andrew@10 684
andrew@10 685 double timeDifference = newEventTime - lastEventTime;
andrew@10 686
andrew@10 687 // if (timeDifference > 50 && updatingSpeedDistribution){
andrew@10 688 // addGaussianNoiseToSpeedPosterior(timeDifference * 10.0 / 100.);
andrew@10 689 // }
andrew@10 690
andrew@10 691
andrew@11 692 // updateBestEstimate(timeDifference);
andrew@11 693 // lastBestEstimateUpdateTime = newEventTime;//getTimeNow(timePlayed);
andrew@10 694
andrew@10 695 //set TARGETS - commented tenmporarily
andrew@11 696
andrew@11 697 //setNewDistributionOffsets(max(0., bestEstimate - (prior.scalar*prior.arraySize/2)));
andrew@11 698 projectedPrior.offset = posterior.offset;//max(0., newAlignmentPosition - (projectedPrior.scalar*projectedPrior.arraySize/2));
andrew@11 699
andrew@11 700 // int timeDifference = newEventTime - lastEventTime;
andrew@11 701
andrew@11 702 double timeDifferenceInPositionVectorUnits = timeDifference / projectedPrior.scalar;
andrew@11 703
andrew@11 704 // printf("CROSS UPDATE time diff %f ms is %f units; ", timeDifference, timeDifferenceInPositionVectorUnits);
andrew@11 705 projectedPrior.zero();//kill prior
andrew@11 706
andrew@11 707 // calculateNewPriorOffset(timeDifference);//dioesnt do anything
andrew@11 708
andrew@11 709 // printf("new prior offset %f and post offset %f\n", prior.offset, posterior.offset);
andrew@11 710
andrew@11 711 if (timeDifferenceInPositionVectorUnits > crossUpdateTimeThreshold)
andrew@11 712 complexCrossUpdateProjection(projectedPrior, timeDifferenceInPositionVectorUnits);
andrew@11 713 else
andrew@11 714 translatePosteriorByMaximumSpeed(projectedPrior, timeDifferenceInPositionVectorUnits);
andrew@11 715
andrew@11 716
andrew@11 717 // updateCounter++;
andrew@11 718 projectedPrior.renormalise();//not strictly necessary??
andrew@11 719
andrew@11 720
andrew@10 721 //i.e. using the same offset as prior
andrew@11 722 // posterior.offset = prior.offset;//
andrew@10 723
andrew@10 724 // float tmpPrior = max(0., bestEstimate - (prior.scalar*prior.arraySize/2));// prior.offset = max(0., bestEstimate - (prior.scalar*prior.arraySize/2));
andrew@10 725 // printf("Using prior offset of %f not %f\n", tmpPrior, prior.offset);
andrew@10 726
andrew@11 727 // lastEventTime = newEventTime;//lastEventTime = ofGetElapsedTimeMillis();
andrew@10 728
andrew@10 729 }
andrew@10 730
andrew@10 731
andrew@10 732
andrew@11 733 void BayesianArrayStructure::complexCrossUpdateProjection(DynamicVector& projectedPrior, const double& timeDifferenceInPositionVectorUnits){
andrew@10 734
andrew@10 735 int distanceMoved, newPriorIndex;
andrew@11 736
andrew@10 737 double speedValue = relativeSpeedPosterior.offset;
andrew@10 738
andrew@10 739 for (int i = 0;i < relativeSpeedPosterior.arraySize;i++){
andrew@10 740
andrew@10 741 distanceMoved = round(timeDifferenceInPositionVectorUnits * speedValue);//round the value
andrew@10 742
andrew@10 743 if (relativeSpeedPosterior.array[i] != 0){
andrew@10 744
andrew@10 745 double speedContribution = relativeSpeedPosterior.array[i];
andrew@10 746
andrew@11 747 newPriorIndex = posterior.offset - projectedPrior.offset + distanceMoved;//i.e. where post[0] goes to in terms of prior at this speed
andrew@10 748 int postIndex = 0;//index of posterior that will contribute
andrew@10 749
andrew@11 750 while (postIndex < posterior.arraySize && newPriorIndex < projectedPrior.arraySize){
andrew@11 751
andrew@10 752 //would use this method
andrew@10 753 //newPriorIndex = postIndex + posterior.offset - prior.offset + distanceMoved;
andrew@10 754
andrew@10 755 if (newPriorIndex >= 0){
andrew@11 756 projectedPrior.addToIndex(newPriorIndex, posterior.array[postIndex]*speedContribution);
andrew@10 757 // 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@10 758 }
andrew@10 759 //but we actually do this for simplicity
andrew@10 760 newPriorIndex++;
andrew@10 761 postIndex++;
andrew@10 762 }//end for. now while
andrew@10 763
andrew@10 764
andrew@10 765 }//if not zero
andrew@10 766
andrew@10 767 speedValue += relativeSpeedPosterior.scalar;//optimised line
andrew@10 768 //as we wanted:
andrew@10 769 // double speedValue = relativeSpeedPosterior.getIndexInRealTerms(i);//so for scalar 0.01, 50 -> speed value of 0.5
andrew@10 770
andrew@10 771 }//end speed
andrew@11 772
andrew@10 773 }
andrew@10 774
andrew@10 775
andrew@10 776
andrew@11 777 void BayesianArrayStructure::translatePosteriorByMaximumSpeed(DynamicVector& translatedPosterior, const double& timeDifferenceInPositionVectorUnits){
andrew@10 778
andrew@10 779
andrew@10 780 int distanceMoved, newPriorIndex;
andrew@10 781 double speedIndex = getSpeedEstimateIndex();
andrew@10 782 double speedValue = relativeSpeedPosterior.getIndexInRealTerms(speedIndex);
andrew@10 783
andrew@10 784 // double speedValue = relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate);
andrew@10 785
andrew@10 786 //so for scalar 0.01, 50 -> speed value of 0.5
andrew@10 787 double speedContribution = relativeSpeedPosterior.array[(int)round(speedIndex)];
andrew@10 788 //so we have moved
andrew@10 789 distanceMoved = round(timeDifferenceInPositionVectorUnits * speedValue);//round the value
andrew@10 790
andrew@10 791 // printf("speed [%i] gives %f moved %i in %f units \n", i, speedValue, distanceMoved, timeDifferenceInPositionVectorUnits);
andrew@10 792
andrew@10 793 for (int postIndex = 0;postIndex < posterior.arraySize;postIndex++){
andrew@10 794 //old posterior contributing to new prior
andrew@10 795 newPriorIndex = postIndex + posterior.offset - prior.offset + distanceMoved;
andrew@10 796 if (newPriorIndex >= 0 && newPriorIndex < prior.arraySize){
andrew@11 797 translatedPosterior.addToIndex(newPriorIndex, posterior.array[postIndex]*speedContribution);
andrew@10 798 }
andrew@10 799
andrew@10 800 }
andrew@10 801
andrew@10 802 }
andrew@11 803
andrew@10 804
andrew@10 805 //END PROJECT PRIOR CODE
andrew@10 806
andrew@10 807
andrew@11 808
andrew@0 809 /*
andrew@0 810
andrew@0 811 void BayesianArrayStructure::updateTempoDistribution(const double& speedRatio, const double& matchFactor){
andrew@0 812 //speedratio is speed of played relative to the recording
andrew@0 813
andrew@0 814 double index = relativeSpeedLikelihood.getRealTermsAsIndex(speedRatio);
andrew@0 815 // printf("\nindex of likelihood would be %f\n", index);
andrew@0 816 if (index >= 0 && index < relativeSpeedPrior.length){
andrew@0 817 //then we can do update
andrew@0 818
andrew@0 819 //set new likelihood
andrew@0 820 relativeSpeedLikelihood.zero();
andrew@0 821 relativeSpeedLikelihood.addConstant(speedLikelihoodNoise);
andrew@0 822
andrew@0 823 relativeSpeedLikelihood.addGaussianShape(index , 5, 0.5*matchFactor);
andrew@0 824
andrew@0 825
andrew@0 826 //copy posterior to prior
andrew@0 827 relativeSpeedPrior.copyFromDynamicVector(relativeSpeedPosterior);
andrew@0 828
andrew@0 829 //update
andrew@0 830 relativeSpeedPosterior.doProduct(relativeSpeedPrior, relativeSpeedLikelihood);
andrew@0 831
andrew@0 832 //normalise
andrew@0 833 relativeSpeedPosterior.renormalise();
andrew@0 834
andrew@0 835 relativeSpeedPosterior.getMaximum();
andrew@0 836 }//end if within range
andrew@0 837
andrew@0 838
andrew@0 839 }
andrew@0 840
andrew@0 841 */