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@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@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@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@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@0: void DynamicVector::addGaussianShape(double mean, double StdDev, double factor){ andrew@0: int i; andrew@0: for (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@0: double screenHeight = ofGetHeight(); andrew@0: double maxVal = getMaximum(); andrew@0: andrew@0: for (int i = max(1,minIndex+1);i < min(maxIndex, (int)array.size());i++){ andrew@0: ofLine (stepSize*(i-1), screenHeight * (1 - array[i-1] / maxVal), stepSize*i, screenHeight * (1 - array[i] / maxVal) ); 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: }