andrew@33: /* andrew@33: * DynamicVector.cpp andrew@33: * midiCannamReader andrew@33: * andrew@33: * Created by Andrew on 18/07/2011. andrew@33: * Copyright 2011 QMUL. All rights reserved. andrew@33: * andrew@33: */ andrew@33: andrew@33: andrew@33: #include "DynamicVector.h" andrew@33: andrew@33: DynamicVector::DynamicVector(){ andrew@33: length = 0; andrew@33: arraySize = 0; andrew@33: maximumValue = 0; andrew@33: MAPestimate = 0; andrew@33: offset = 0; andrew@33: scalar = 1; andrew@33: integratedEstimate = length/2; andrew@33: andrew@33: gaussianLookupMean = (double) GAUSSIAN_LOOKUP_LENGTH/2; andrew@33: gaussianLookupStdDev = (double)(GAUSSIAN_LOOKUP_LENGTH/16); andrew@33: double factor = 1.0;//(1.0 / (gaussianLookupStdDev*sqrt(2*PI)) );//1.0;//-1.0/(2*PI*sqrt(gaussianLookupStdDev)); andrew@33: for (int i = 0;i < GAUSSIAN_LOOKUP_LENGTH;i++){ andrew@33: gaussianLookupTable[i] = factor*exp(-1.0*(i-gaussianLookupMean)*(i-gaussianLookupMean)/(2.0*gaussianLookupStdDev*gaussianLookupStdDev)); andrew@33: } andrew@33: andrew@33: } andrew@33: andrew@33: void DynamicVector::copyFromDynamicVector(const DynamicVector& dynamicVec){ andrew@33: if (dynamicVec.length == length){ andrew@44: offset = dynamicVec.offset; andrew@33: for (int i = 0;i < length;i++) andrew@33: array[i] = dynamicVec.array[i]; andrew@33: } andrew@33: else{ andrew@33: printf("CANNOT COPY VECTORS OF NON SAME LENGTH!!\n"); andrew@33: } andrew@33: } andrew@33: andrew@33: void DynamicVector::createVector(int len){ andrew@33: array.clear(); andrew@33: for (int i = 0; i < len;i++){ andrew@33: array.push_back(0); andrew@33: } andrew@33: length = len; andrew@33: arraySize = array.size(); andrew@33: integratedEstimate = length/2; andrew@46: MAPestimate = length/2; andrew@46: printf("array size is %i\n", arraySize); andrew@33: } andrew@33: andrew@33: andrew@33: double DynamicVector::getMaximum(){ andrew@33: int i; andrew@33: double max = 0; andrew@33: for (i=0;i < length;i++){ andrew@33: if (array[i] > max){ andrew@33: max = array[i]; andrew@33: MAPestimate = i; andrew@33: } andrew@33: } andrew@33: maximumValue = max; andrew@33: return max; andrew@33: } andrew@33: andrew@46: int DynamicVector::getMAPestimate(){ andrew@46: int i; andrew@46: double max = 0; andrew@46: for (i=0;i < length;i++){ andrew@46: if (array[i] > max){ andrew@46: max = array[i]; andrew@46: MAPestimate = i; andrew@46: } andrew@46: } andrew@46: maximumValue = max; andrew@46: return MAPestimate; andrew@46: } andrew@46: andrew@33: double DynamicVector::getIntegratedEstimate(){ andrew@33: //returns the index of the integrated average - where the probability distribution is centred andrew@33: integratedEstimate = 0; andrew@33: double integratedTotal = 0; andrew@33: for (int i = 0;i < length;i++){ andrew@33: integratedEstimate += array[i]*i; andrew@33: integratedTotal += array[i]; andrew@33: } andrew@33: if (integratedTotal > 0){ andrew@33: integratedEstimate /= integratedTotal; andrew@33: } andrew@33: return integratedEstimate; andrew@33: } andrew@33: andrew@33: void DynamicVector::updateIntegratedEstimate(){ andrew@33: //returns the index of the integrated average - where the probability distribution is centred andrew@33: integratedEstimate = 0; andrew@33: double integratedTotal = 0; andrew@33: for (int i = 0;i < length;i++){ andrew@33: integratedEstimate += array[i]*i; andrew@33: integratedTotal += array[i]; andrew@33: } andrew@33: if (integratedTotal > 0){ andrew@33: integratedEstimate /= integratedTotal; andrew@33: } andrew@33: andrew@33: } andrew@33: andrew@33: void DynamicVector::updateLimitedIntegratedEstimate(){ andrew@33: //returns the index of the integrated average - where the probability distribution is centred andrew@33: //but limited round the MAP estimate andrew@33: double tmp = getMaximum(); andrew@33: int limit = min(MAPestimate, length - MAPestimate); andrew@33: int start = max(0, MAPestimate - limit); andrew@33: int end = min(MAPestimate + limit, length-1); andrew@33: andrew@33: integratedEstimate = 0; andrew@33: double integratedTotal = 0; andrew@33: for (int i = start;i <= end;i++){ andrew@33: integratedEstimate += array[i]*i; andrew@33: integratedTotal += array[i]; andrew@33: } andrew@33: if (integratedTotal > 0){ andrew@33: integratedEstimate /= integratedTotal; andrew@33: } andrew@33: andrew@33: } andrew@33: andrew@33: andrew@33: void DynamicVector::zero(){ andrew@33: for (int i = 0;i < array.size();i++) andrew@33: array[i] = 0; andrew@33: } andrew@33: andrew@33: void DynamicVector::renormalise(){ andrew@33: double tmpMax = getMaximum(); andrew@33: if (tmpMax > 0){ andrew@33: // printf("renormalise : max is %f and size is %i\n", tmpMax, arraySize); andrew@33: for (int i = 0;i < array.size();i++) andrew@33: array[i] /= tmpMax; andrew@33: andrew@33: } andrew@33: //printArray(); andrew@33: } andrew@33: andrew@33: void DynamicVector::doProduct(DynamicVector& arrayOne, DynamicVector& arrayTwo){ andrew@33: andrew@33: for (int i = 0;i < arrayOne.length;i++) andrew@33: array[i] = arrayOne.array[i] * arrayTwo.array[i]; andrew@33: } andrew@33: andrew@33: andrew@33: void DynamicVector::printArray(){ andrew@33: for (int i = 0;i < arraySize;i++){ andrew@33: printf("[%i] = %f\n", i, array[i]); andrew@33: } andrew@46: printf("Offset %f map estimate %i == %f ms\n", offset, MAPestimate, getIndexInRealTerms(MAPestimate)); andrew@33: } andrew@33: andrew@33: void DynamicVector::translateDistribution(int translationIndex){ andrew@33: int tmpIndex; andrew@33: DoubleVector tmpArray; andrew@33: int i; andrew@33: andrew@33: for (i=0;i < arraySize;i++){ andrew@33: tmpArray.push_back(array[i]); andrew@33: } andrew@33: //translate values andrew@33: for (i=0;i < arraySize;i++){ andrew@33: tmpIndex = (i + translationIndex + arraySize)%arraySize; andrew@33: array[tmpIndex] = tmpArray[i]; andrew@33: } andrew@33: tmpArray.clear(); andrew@33: //now delete tmp array andrew@33: } andrew@33: andrew@33: void DynamicVector::addGaussianShape(const double& mean, const double& StdDev, double factor){ andrew@46: //USES THE INDICES OF THE MATRIX AS MEAN AND STD DEV andrew@33: int i; andrew@33: double std_dev_factor = (2*StdDev*StdDev); andrew@33: factor *= (1/(StdDev*sqrt(2*PI))); andrew@33: int maxVal = min((int) array.size(), (int)(mean + 4.8*StdDev)); andrew@33: int minVal = max(0, (int)(mean - 4.8*StdDev)); andrew@33: andrew@33: for (i=minVal;i < maxVal;i++){ andrew@33: array[i] += factor*exp(-1*(i-mean)*(i-mean)/(std_dev_factor)); andrew@33: } andrew@33: andrew@33: // addGaussianShapeByLookupTable(mean, StdDev, factor); andrew@33: } andrew@33: andrew@44: andrew@44: void DynamicVector::addGaussianShapeFromRealTime(const double& actualTime, const double& StdDev, double factor){ andrew@46: //USES THE ACTUAL VALUES IN MS AS MEAN (TIME) AND STD DEV andrew@44: andrew@46: double StdDevAsIndex = StdDev/scalar; andrew@44: double mean = getRealTermsAsIndex(actualTime); andrew@46: // printf("Gaussian realtime %f at index %f std dev %f in index %f \n", actualTime, mean, StdDev, StdDevAsIndex); andrew@44: int i; andrew@46: double std_dev_factor = (2*StdDevAsIndex*StdDevAsIndex); andrew@46: factor *= (1/(StdDevAsIndex*sqrt(2*PI))); andrew@46: int maxVal = min((int) array.size(), (int)(mean + 4.8*StdDevAsIndex)); andrew@46: int minVal = max(0, (int)(mean - 4.8*StdDevAsIndex)); andrew@44: andrew@44: for (i=minVal;i < maxVal;i++){ andrew@44: array[i] += factor*exp(-1*(i-mean)*(i-mean)/(std_dev_factor)); andrew@44: } andrew@44: andrew@44: // addGaussianShapeByLookupTable(mean, StdDev, factor); andrew@44: } andrew@44: andrew@44: andrew@33: void DynamicVector::addGaussianShapeByLookupTable(double& mean, double& StdDev, double factor){ andrew@33: int i; andrew@33: int lookupIndex ; andrew@33: factor *= (1/(StdDev*sqrt(2*PI))); andrew@33: for (i=0;i= GAUSSIAN_LOOKUP_LENGTH) andrew@33: lookupIndex = GAUSSIAN_LOOKUP_LENGTH-1; andrew@33: andrew@33: // (i - mean)*(i-mean)*(GAUSSIAN_LOOKUP_LENGTH*GAUSSIAN_LOOKUP_LENGTH/16.0)/(StdDev*StdDev); andrew@33: return lookupIndex; andrew@33: } andrew@33: andrew@33: void DynamicVector::addTriangularShape(double mean, double width, double factor){ andrew@33: int i; andrew@33: andrew@33: for (i= max(0., (double)(mean - width));i < min((mean+width), (double)array.size());i++){ andrew@33: array[i] += factor * abs(i - mean) / mean; andrew@33: } andrew@33: andrew@33: } andrew@33: andrew@33: void DynamicVector::addConstant(const double& value){ andrew@33: for (int i=0;i= 0 && index < length) andrew@33: return array[index]; andrew@33: else andrew@33: return 0; andrew@33: } andrew@33: andrew@33: void DynamicVector::drawVector(const int& minIndex, const int& maxIndex){ andrew@33: andrew@33: andrew@33: double stepSize = ofGetWidth() / (double)(maxIndex - minIndex); andrew@33: double screenHeight = (double) ofGetHeight(); andrew@33: double maxVal = getMaximum(); andrew@33: andrew@33: int startInt = max(1,minIndex+1); andrew@33: int endInt = min(maxIndex, (int)array.size()); andrew@33: double heightConstant = screenHeight / maxVal; andrew@33: int lastHeightPixel = heightConstant * (maxVal - array[startInt-1]); andrew@33: int newHeightPixel; andrew@33: for (int i = startInt;i < endInt;i++){ andrew@33: newHeightPixel = (int) heightConstant * (maxVal - array[i]); andrew@44: int xPos = i - startInt; andrew@44: ofLine (stepSize*(xPos-1), lastHeightPixel, stepSize*xPos, newHeightPixel); andrew@33: lastHeightPixel = newHeightPixel; andrew@33: } andrew@33: andrew@33: } andrew@33: andrew@33: andrew@33: void DynamicVector::drawConstrainedVector(const int& minIndex, const int& maxIndex, const int& minScreenIndex, const int& maxScreenIndex){ andrew@33: //constrain the height and width andrew@33: andrew@33: double stepSize = (maxScreenIndex - minScreenIndex) / (double)(maxIndex - minIndex);//step size in pixels per array bin andrew@33: double screenHeight = ofGetHeight(); andrew@33: double maxVal = getMaximum(); andrew@33: andrew@33: //OPTIMIZE!! XXX could just add stepsize each time andrew@33: //not add minindex each time andrew@33: int i = max(1,minIndex+1); andrew@33: // ofDrawBitmapString("i = "+ofToString(i)+" :: screen min: "+ofToString(minScreenIndex + stepSize*(i-minIndex-1)), 20, 640); andrew@33: andrew@33: while ((minScreenIndex + stepSize*(i-minIndex)) < 0) andrew@33: i++;//only draw what is on the screen andrew@33: andrew@33: for ( ; i < min(maxIndex+1, (int)array.size());i++){ andrew@33: ofLine (minScreenIndex + (stepSize*(i-minIndex-1)), screenHeight * (1 - array[i-1] / maxVal), andrew@33: minScreenIndex + (stepSize*(i-minIndex)), screenHeight * (1 - array[i] / maxVal) ); andrew@33: andrew@33: } andrew@33: andrew@33: ofLine(minScreenIndex, screenHeight, minScreenIndex, screenHeight/2); andrew@33: ofLine(maxScreenIndex, screenHeight, maxScreenIndex, screenHeight/2); andrew@33: andrew@33: // ofDrawBitmapString(ofToString(stepSize, 2)+" "+ofToString(maxScreenIndex - minScreenIndex, 0), 20, 600); andrew@33: andrew@33: }