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