comparison bayesianArraySrc/BayesianArrayStructure.cpp @ 10:cbadb9d05d29

Using timestamps for the scrolling alignment time
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Sat, 04 Feb 2012 19:59:27 +0000
parents 572564b7cb85
children 9a2b008c4706
comparison
equal deleted inserted replaced
9:bc62266af280 10:cbadb9d05d29
209 */ 209 */
210 210
211 void BayesianArrayStructure::updateBestEstimate(const double& timeDifference){ 211 void BayesianArrayStructure::updateBestEstimate(const double& timeDifference){
212 // double timeDiff = ofGetElapsedTimeMillis() - lastEventTime;// 212 // double timeDiff = ofGetElapsedTimeMillis() - lastEventTime;//
213 double tmp = bestEstimate; 213 double tmp = bestEstimate;
214 printf("best est routine: posterior offset %f\n", posterior.offset); 214 // printf("best est routine: posterior offset %f\n", posterior.offset);
215 215
216 double timeDiff = timeDifference; 216 double timeDiff = timeDifference;
217 217
218 //Using timedifferencfe here will make it go wrong. Is time since beginning of playing 218 //Using timedifferencfe here will make it go wrong. Is time since beginning of playing
219 219
230 speedEstimateIndex = getSpeedEstimateIndex(); 230 speedEstimateIndex = getSpeedEstimateIndex();
231 231
232 speedEstimate = relativeSpeedPosterior.getIndexInRealTerms(speedEstimateIndex); 232 speedEstimate = relativeSpeedPosterior.getIndexInRealTerms(speedEstimateIndex);
233 bestEstimate = posterior.getIndexInRealTerms(posterior.MAPestimate) + timeDiff*speedEstimate; 233 bestEstimate = posterior.getIndexInRealTerms(posterior.MAPestimate) + timeDiff*speedEstimate;
234 234
235 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, 235 // 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,
236 posterior.MAPestimate, posterior.getIndexInRealTerms(posterior.MAPestimate), speedEstimateIndex, speedEstimate, timeDiff*speedEstimate); 236 // posterior.MAPestimate, posterior.getIndexInRealTerms(posterior.MAPestimate), speedEstimateIndex, speedEstimate, timeDiff*speedEstimate);
237 } 237 printf("BEST ESTIMATE %f\n", bestEstimate);
238 }
239
238 240
239 void BayesianArrayStructure::calculatePosterior(){ 241 void BayesianArrayStructure::calculatePosterior(){
240 //posterior.doProduct(prior, likelihood); 242 //posterior.doProduct(prior, likelihood);
241 assert(posterior.offset == prior.offset); 243 assert(posterior.offset == prior.offset);
242 244
282 //need to get new MAP position and set the offset of the arrays 284 //need to get new MAP position and set the offset of the arrays
283 //currently bestEstimate is the approx for the new MAP position 285 //currently bestEstimate is the approx for the new MAP position
284 // int tmpMap = bayesianStruct.posterior.getMAPestimate(); 286 // int tmpMap = bayesianStruct.posterior.getMAPestimate();
285 287
286 double timeDifference = newEventTime - lastEventTime; 288 double timeDifference = newEventTime - lastEventTime;
287 // printf("updating distributions at time %f diff %f offset %f tmpmap est %i\n", newEventTime, timeDifference, bayesianStruct.posterior.offset, tmpMap); 289 printf("updating distributions at time %f diff %f offset %f\n", newEventTime, timeDifference, posterior.offset);
288 290
289 //addnoise to the tempo distribution 291 //addnoise to the tempo distribution
290 //bayesianStruct.decaySpeedDistribution(timeDifference); 292 //bayesianStruct.decaySpeedDistribution(timeDifference);
291 293
292 if (timeDifference > 50 && updatingSpeedDistribution){ 294 if (timeDifference > 50 && updatingSpeedDistribution){
310 // printf("Using prior offset of %f not %f\n", tmpPrior, prior.offset); 312 // printf("Using prior offset of %f not %f\n", tmpPrior, prior.offset);
311 313
312 lastEventTime = newEventTime;//lastEventTime = ofGetElapsedTimeMillis(); 314 lastEventTime = newEventTime;//lastEventTime = ofGetElapsedTimeMillis();
313 315
314 } 316 }
317
318
315 319
316 320
317 void BayesianArrayStructure::crossUpdateArrays(DynamicVector& position, DynamicVector& speed, double timeDifference){ 321 void BayesianArrayStructure::crossUpdateArrays(DynamicVector& position, DynamicVector& speed, double timeDifference){
318 322
319 //set the cutoff for offset of position first! XXX 323 //set the cutoff for offset of position first! XXX
655 void BayesianArrayStructure::printPostOffset(){ 659 void BayesianArrayStructure::printPostOffset(){
656 double tmp = posterior.getMAPestimate(); 660 double tmp = posterior.getMAPestimate();
657 printf(" MAP index %i post offset %f == %f ms\n", posterior.MAPestimate, posterior.offset, posterior.getIndexInRealTerms(posterior.MAPestimate)); 661 printf(" MAP index %i post offset %f == %f ms\n", posterior.MAPestimate, posterior.offset, posterior.getIndexInRealTerms(posterior.MAPestimate));
658 } 662 }
659 663
664 //PROJECT PRIOR CODE
665 /*
666 void BayesianArrayStructure::projectDistribution(const double& newEventTime, DynamicVector& projectedPrior){
667
668 projectedPrior.copyFromDynamicVector(posterior);
669
670 double timeDifference = newEventTime - lastEventTime;
671
672 // if (timeDifference > 50 && updatingSpeedDistribution){
673 // addGaussianNoiseToSpeedPosterior(timeDifference * 10.0 / 100.);
674 // }
675
676
677 updateBestEstimate(timeDifference);
678 lastBestEstimateUpdateTime = newEventTime;//getTimeNow(timePlayed);
679
680 //set TARGETS - commented tenmporarily
681 setNewDistributionOffsets(max(0., bestEstimate - (prior.scalar*prior.arraySize/2)));
682
683 crossUpdateArrays(posterior, relativeSpeedPosterior, timeDifference);
684
685 //i.e. using the same offset as prior
686 posterior.offset = prior.offset;//
687
688 // float tmpPrior = max(0., bestEstimate - (prior.scalar*prior.arraySize/2));// prior.offset = max(0., bestEstimate - (prior.scalar*prior.arraySize/2));
689 // printf("Using prior offset of %f not %f\n", tmpPrior, prior.offset);
690
691 lastEventTime = newEventTime;//lastEventTime = ofGetElapsedTimeMillis();
692
693 }
694
695
696
697
698 void BayesianArrayStructure::crossUpdateArrays(DynamicVector& position, DynamicVector& speed, double timeDifference){
699
700 //set the cutoff for offset of position first! XXX
701
702 double timeDifferenceInPositionVectorUnits = timeDifference / prior.scalar;
703
704 printf("CROSS UPDATE time diff %f ms is %f units; ", timeDifference, timeDifferenceInPositionVectorUnits);
705 prior.zero();//kill prior
706
707 // calculateNewPriorOffset(timeDifference);//dioesnt do anything
708
709 // printf("new prior offset %f and post offset %f\n", prior.offset, posterior.offset);
710
711 if (timeDifferenceInPositionVectorUnits > crossUpdateTimeThreshold)
712 complexCrossUpdate(timeDifferenceInPositionVectorUnits);
713 else
714 translateByMaximumSpeed(timeDifferenceInPositionVectorUnits);
715
716
717 updateCounter++;
718 prior.renormalise();//not strictly necessary??
719
720 }
721
722 void BayesianArrayStructure::complexCrossUpdate(const double& timeDifferenceInPositionVectorUnits){
723
724
725 printf("before cross c : posterior map is %i = %f ms time diff pos vec %f\n", posterior.getMAPestimate(), posterior.getIndexInRealTerms(prior.getMAPestimate()), timeDifferenceInPositionVectorUnits);
726
727 int distanceMoved, newPriorIndex;
728
729 double speedValue = relativeSpeedPosterior.offset;
730
731 for (int i = 0;i < relativeSpeedPosterior.arraySize;i++){
732
733 // double speedValue = relativeSpeedPosterior.getIndexInRealTerms(i);//so for scalar 0.01, 50 -> speed value of 0.5
734
735 //so we have moved
736 distanceMoved = round(timeDifferenceInPositionVectorUnits * speedValue);//round the value
737 // printf("Speed value %f time %f gives distance %i\n", speedValue, timeDifferenceInPositionVectorUnits, distanceMoved);
738
739 if (relativeSpeedPosterior.array[i] != 0){
740
741 double speedContribution = relativeSpeedPosterior.array[i];
742
743 // printf("speed [%i](val[%f]) gives %f moved %i in %f units \n", i, relativeSpeedPosterior.array[i], speedValue, distanceMoved, timeDifferenceInPositionVectorUnits);
744
745 //1/2/12 deleted line
746 newPriorIndex = posterior.offset - prior.offset + distanceMoved;//i.e. where post[0] goes to in terms of prior at this speed
747 int postIndex = 0;//index of posterior that will contribute
748
749 while (postIndex < posterior.arraySize && newPriorIndex < prior.arraySize){
750
751 //did use a for loop
752 // for (postIndex = 0;postIndex < posterior.arraySize;postIndex++){
753 //old posterior contributing to new prior
754
755 //would use this method
756 //newPriorIndex = postIndex + posterior.offset - prior.offset + distanceMoved;
757
758 if (newPriorIndex >= 0){
759 prior.addToIndex(newPriorIndex, posterior.array[postIndex]*speedContribution);
760 // printf("speed index %i new prior index %i post val %f speed contrib %f dist %i\n", i, newPriorIndex, posterior.array[postIndex], speedContribution, distanceMoved);
761 }
762 //but we actually do this for simplicity
763 newPriorIndex++;
764 postIndex++;
765 }//end for. now while
766
767
768 }//if not zero
769
770 speedValue += relativeSpeedPosterior.scalar;//optimised line
771 //as we wanted:
772 // double speedValue = relativeSpeedPosterior.getIndexInRealTerms(i);//so for scalar 0.01, 50 -> speed value of 0.5
773
774 }//end speed
775
776
777 printf("after cross c : prior map is %i = %f ms\n", prior.getMAPestimate(), prior.getIndexInRealTerms(prior.getMAPestimate()));
778 }
779
780
781
782 void BayesianArrayStructure::translateByMaximumSpeed(const double& timeDifferenceInPositionVectorUnits){
783
784
785 int distanceMoved, newPriorIndex;
786 double speedIndex = getSpeedEstimateIndex();
787 double speedValue = relativeSpeedPosterior.getIndexInRealTerms(speedIndex);
788
789 // double speedValue = relativeSpeedPosterior.getIndexInRealTerms(relativeSpeedPosterior.integratedEstimate);
790
791 //so for scalar 0.01, 50 -> speed value of 0.5
792 double speedContribution = relativeSpeedPosterior.array[(int)round(speedIndex)];
793 //so we have moved
794 distanceMoved = round(timeDifferenceInPositionVectorUnits * speedValue);//round the value
795
796 // printf("speed [%i] gives %f moved %i in %f units \n", i, speedValue, distanceMoved, timeDifferenceInPositionVectorUnits);
797
798 for (int postIndex = 0;postIndex < posterior.arraySize;postIndex++){
799 //old posterior contributing to new prior
800 newPriorIndex = postIndex + posterior.offset - prior.offset + distanceMoved;
801 if (newPriorIndex >= 0 && newPriorIndex < prior.arraySize){
802 prior.addToIndex(newPriorIndex, posterior.array[postIndex]*speedContribution);
803 }
804
805 }
806
807 }
808 */
809
810 //END PROJECT PRIOR CODE
811
812
660 /* 813 /*
661 814
662 void BayesianArrayStructure::updateTempoDistribution(const double& speedRatio, const double& matchFactor){ 815 void BayesianArrayStructure::updateTempoDistribution(const double& speedRatio, const double& matchFactor){
663 //speedratio is speed of played relative to the recording 816 //speedratio is speed of played relative to the recording
664 817