# HG changeset patch # User Adam Stark # Date 1505170386 -3600 # Node ID 6b522d568ba49f7985b978c1586f6fa29aed29a6 # Parent 0a99d93955bb347c8db3ea9b55f81e5513739832 Made log-gaussian transition window calculated in a function, rather than with duplicated code diff -r 0a99d93955bb -r 6b522d568ba4 src/BTrack.cpp --- a/src/BTrack.cpp Sun Sep 03 12:21:25 2017 +0100 +++ b/src/BTrack.cpp Mon Sep 11 23:53:06 2017 +0100 @@ -628,23 +628,15 @@ int windowEnd = onsetDFBufferSize - round (beatPeriod / 2.); int windowSize = windowEnd - windowStart + 1; - double w1[windowSize]; - double v = -2. * beatPeriod; - - // create window - for (int i = 0; i < windowSize; i++) - { - double a = tightness * log (-v / beatPeriod); - w1[i] = exp ((-1. * a * a) / 2.); - v = v + 1.; - } + double logGaussianTransitionWeighting[windowSize]; + createLogGaussianTransitionWeighting (logGaussianTransitionWeighting, windowSize, beatPeriod); // calculate new cumulative score value double maxValue = 0; int n = 0; for (int i = windowStart; i <= windowEnd; i++) { - double weightedCumulativeScore = cumulativeScore[i] * w1[n]; + double weightedCumulativeScore = cumulativeScore[i] * logGaussianTransitionWeighting[n]; if (weightedCumulativeScore > maxValue) maxValue = weightedCumulativeScore; @@ -685,17 +677,13 @@ // one beat period in the past // This is W1 in Adam Stark's PhD thesis, equation 3.2, page 60 - v = -2 * beatPeriod; + int startIndex = onsetDFBufferSize - round (2 * beatPeriod); int endIndex = onsetDFBufferSize - round (beatPeriod / 2); int pastWindowSize = endIndex - startIndex + 1; + double logGaussianTransitionWeighting[pastWindowSize]; - - for (int i = 0; i < pastWindowSize; i++) - { - logGaussianTransitionWeighting[i] = exp((-1 * pow (tightness * log (-v / beatPeriod), 2) ) / 2); - v = v + 1; - } + createLogGaussianTransitionWeighting (logGaussianTransitionWeighting, pastWindowSize, beatPeriod); // Calculate the future cumulative score, using the log Gaussian transition weighting @@ -741,3 +729,16 @@ // set next prediction time m0 = beatCounter + round (beatPeriod / 2); } + +//======================================================================= +void BTrack::createLogGaussianTransitionWeighting (double* weightingArray, int numSamples, double beatPeriod) +{ + double v = -2. * beatPeriod; + + for (int i = 0; i < numSamples; i++) + { + double a = tightness * log (-v / beatPeriod); + weightingArray[i] = exp ((-1. * a * a) / 2.); + v++; + } +} diff -r 0a99d93955bb -r 6b522d568ba4 src/BTrack.h --- a/src/BTrack.h Sun Sep 03 12:21:25 2017 +0100 +++ b/src/BTrack.h Mon Sep 11 23:53:06 2017 +0100 @@ -164,6 +164,10 @@ /** Calculates the output of the comb filter bank */ void calculateOutputOfCombFilterBank(); + + void createLogGaussianTransitionWeighting (double* weightingArray, int numSamples, double beatPeriod); + + //=======================================================================