Mercurial > hg > btrack
comparison src/BTrack.cpp @ 103:6b522d568ba4
Made log-gaussian transition window calculated in a function, rather than with duplicated code
author | Adam Stark <adamstark.uk@gmail.com> |
---|---|
date | Mon, 11 Sep 2017 23:53:06 +0100 |
parents | 0a99d93955bb |
children | 677c44fe42e0 |
comparison
equal
deleted
inserted
replaced
102:0a99d93955bb | 103:6b522d568ba4 |
---|---|
626 { | 626 { |
627 int windowStart = onsetDFBufferSize - round (2. * beatPeriod); | 627 int windowStart = onsetDFBufferSize - round (2. * beatPeriod); |
628 int windowEnd = onsetDFBufferSize - round (beatPeriod / 2.); | 628 int windowEnd = onsetDFBufferSize - round (beatPeriod / 2.); |
629 int windowSize = windowEnd - windowStart + 1; | 629 int windowSize = windowEnd - windowStart + 1; |
630 | 630 |
631 double w1[windowSize]; | 631 double logGaussianTransitionWeighting[windowSize]; |
632 double v = -2. * beatPeriod; | 632 createLogGaussianTransitionWeighting (logGaussianTransitionWeighting, windowSize, beatPeriod); |
633 | |
634 // create window | |
635 for (int i = 0; i < windowSize; i++) | |
636 { | |
637 double a = tightness * log (-v / beatPeriod); | |
638 w1[i] = exp ((-1. * a * a) / 2.); | |
639 v = v + 1.; | |
640 } | |
641 | 633 |
642 // calculate new cumulative score value | 634 // calculate new cumulative score value |
643 double maxValue = 0; | 635 double maxValue = 0; |
644 int n = 0; | 636 int n = 0; |
645 for (int i = windowStart; i <= windowEnd; i++) | 637 for (int i = windowStart; i <= windowEnd; i++) |
646 { | 638 { |
647 double weightedCumulativeScore = cumulativeScore[i] * w1[n]; | 639 double weightedCumulativeScore = cumulativeScore[i] * logGaussianTransitionWeighting[n]; |
648 | 640 |
649 if (weightedCumulativeScore > maxValue) | 641 if (weightedCumulativeScore > maxValue) |
650 maxValue = weightedCumulativeScore; | 642 maxValue = weightedCumulativeScore; |
651 | 643 |
652 n++; | 644 n++; |
683 // It is a log-Gaussian transition weighting running from from 2 beat periods | 675 // It is a log-Gaussian transition weighting running from from 2 beat periods |
684 // in the past to half a beat period in the past. It favours the time exactly | 676 // in the past to half a beat period in the past. It favours the time exactly |
685 // one beat period in the past | 677 // one beat period in the past |
686 // This is W1 in Adam Stark's PhD thesis, equation 3.2, page 60 | 678 // This is W1 in Adam Stark's PhD thesis, equation 3.2, page 60 |
687 | 679 |
688 v = -2 * beatPeriod; | 680 |
689 int startIndex = onsetDFBufferSize - round (2 * beatPeriod); | 681 int startIndex = onsetDFBufferSize - round (2 * beatPeriod); |
690 int endIndex = onsetDFBufferSize - round (beatPeriod / 2); | 682 int endIndex = onsetDFBufferSize - round (beatPeriod / 2); |
691 int pastWindowSize = endIndex - startIndex + 1; | 683 int pastWindowSize = endIndex - startIndex + 1; |
684 | |
692 double logGaussianTransitionWeighting[pastWindowSize]; | 685 double logGaussianTransitionWeighting[pastWindowSize]; |
693 | 686 createLogGaussianTransitionWeighting (logGaussianTransitionWeighting, pastWindowSize, beatPeriod); |
694 for (int i = 0; i < pastWindowSize; i++) | |
695 { | |
696 logGaussianTransitionWeighting[i] = exp((-1 * pow (tightness * log (-v / beatPeriod), 2) ) / 2); | |
697 v = v + 1; | |
698 } | |
699 | 687 |
700 // Calculate the future cumulative score, using the log Gaussian transition weighting | 688 // Calculate the future cumulative score, using the log Gaussian transition weighting |
701 | 689 |
702 for (int i = onsetDFBufferSize; i < (onsetDFBufferSize + beatExpectationWindowSize); i++) | 690 for (int i = onsetDFBufferSize; i < (onsetDFBufferSize + beatExpectationWindowSize); i++) |
703 { | 691 { |
739 } | 727 } |
740 | 728 |
741 // set next prediction time | 729 // set next prediction time |
742 m0 = beatCounter + round (beatPeriod / 2); | 730 m0 = beatCounter + round (beatPeriod / 2); |
743 } | 731 } |
732 | |
733 //======================================================================= | |
734 void BTrack::createLogGaussianTransitionWeighting (double* weightingArray, int numSamples, double beatPeriod) | |
735 { | |
736 double v = -2. * beatPeriod; | |
737 | |
738 for (int i = 0; i < numSamples; i++) | |
739 { | |
740 double a = tightness * log (-v / beatPeriod); | |
741 weightingArray[i] = exp ((-1. * a * a) / 2.); | |
742 v++; | |
743 } | |
744 } |