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