Mercurial > hg > midi-score-follower
diff src/DynamicVector.cpp @ 4:4a8e6a6cd224
optimised draw function in dynamic vector class. Added Gaussian lookup but not yet used.
author | Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk> |
---|---|
date | Fri, 19 Aug 2011 15:53:04 +0100 |
parents | 5581023e0de4 |
children | 75dcd1308658 |
line wrap: on
line diff
--- a/src/DynamicVector.cpp Fri Aug 19 02:36:34 2011 +0100 +++ b/src/DynamicVector.cpp Fri Aug 19 15:53:04 2011 +0100 @@ -17,6 +17,14 @@ MAPestimate = 0; offset = 0; scalar = 1; + + gaussianLookupMean = (double) GAUSSIAN_LOOKUP_LENGTH/2; + gaussianLookupStdDev = (double)(GAUSSIAN_LOOKUP_LENGTH/16); + double factor = 1.0;//(1.0 / (gaussianLookupStdDev*sqrt(2*PI)) );//1.0;//-1.0/(2*PI*sqrt(gaussianLookupStdDev)); + for (int i = 0;i < GAUSSIAN_LOOKUP_LENGTH;i++){ + gaussianLookupTable[i] = factor*exp(-1.0*(i-gaussianLookupMean)*(i-gaussianLookupMean)/(2.0*gaussianLookupStdDev*gaussianLookupStdDev)); + } + } void DynamicVector::copyFromDynamicVector(const DynamicVector& dynamicVec){ @@ -113,14 +121,42 @@ } void DynamicVector::addGaussianShape(double mean, double StdDev, double factor){ + int i; factor *= (1/(StdDev*sqrt(2*PI))); for (i=0;i<array.size();i++){ array[i] += factor*exp(-1*(i-mean)*(i-mean)/(2*StdDev*StdDev)); } + + //addGaussianShapeByLookupTable(mean, StdDev, factor); +} + +void DynamicVector::addGaussianShapeByLookupTable(double& mean, double& StdDev, double& factor){ + int i; + int lookupIndex ; + factor *= (1/(StdDev*sqrt(2*PI))); + for (i=0;i<array.size()-1;i++){ + lookupIndex = round(getLookupIndex(i, mean, StdDev)); + array[i] += factor*gaussianLookupTable[lookupIndex]; + } //printf("ADDED GAUSSIAN SHAPE %i\n", (int)array.size()); } +double DynamicVector::getLookupIndex(const int& i, const double& mean, const double& StdDev){ + + double Z = ((double)i - mean)/StdDev; + double lookupIndex = Z*gaussianLookupStdDev + gaussianLookupMean; + + if (lookupIndex < 0) + lookupIndex = 0; + + if (lookupIndex >= GAUSSIAN_LOOKUP_LENGTH) + lookupIndex = GAUSSIAN_LOOKUP_LENGTH-1; + +// (i - mean)*(i-mean)*(GAUSSIAN_LOOKUP_LENGTH*GAUSSIAN_LOOKUP_LENGTH/16.0)/(StdDev*StdDev); + return lookupIndex; +} + void DynamicVector::addTriangularShape(double mean, double width, double factor){ int i; @@ -172,9 +208,15 @@ double stepSize = ofGetWidth() / (double)(maxIndex - minIndex); double screenHeight = ofGetHeight(); double maxVal = getMaximum(); - - for (int i = max(1,minIndex+1);i < min(maxIndex, (int)array.size());i++){ - ofLine (stepSize*(i-1), screenHeight * (1 - array[i-1] / maxVal), stepSize*i, screenHeight * (1 - array[i] / maxVal) ); + int startInt = max(1,minIndex+1); + int endInt = min(maxIndex, (int)array.size()); + double heightConstant = screenHeight / maxVal; + int lastHeightPixel = heightConstant * (1 - array[startInt-1]); + int newHeightPixel; + for (int i = startInt;i < endInt;i++){ + newHeightPixel = (int) heightConstant * (1 - array[i]); + ofLine (stepSize*(i-1), lastHeightPixel, stepSize*i, newHeightPixel); + lastHeightPixel = newHeightPixel; } }