annotate src/DynamicVector.cpp @ 0:b299a65a3ad0

start project
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Tue, 16 Aug 2011 11:29:59 +0100
parents
children 1a32ce016bb9
rev   line source
andrew@0 1 /*
andrew@0 2 * DynamicVector.cpp
andrew@0 3 * midiCannamReader
andrew@0 4 *
andrew@0 5 * Created by Andrew on 18/07/2011.
andrew@0 6 * Copyright 2011 QMUL. All rights reserved.
andrew@0 7 *
andrew@0 8 */
andrew@0 9
andrew@0 10 #include "DynamicVector.h"
andrew@0 11
andrew@0 12 DynamicVector::DynamicVector(){
andrew@0 13 length = 0;
andrew@0 14 arraySize = 0;
andrew@0 15 maximumValue = 0;
andrew@0 16 MAPestimate = 0;
andrew@0 17 offset = 0;
andrew@0 18 scalar = 1;
andrew@0 19 }
andrew@0 20
andrew@0 21 void DynamicVector::copyFromDynamicVector(const DynamicVector& dynamicVec){
andrew@0 22 if (dynamicVec.length == length){
andrew@0 23 for (int i = 0;i < length;i++)
andrew@0 24 array[i] = dynamicVec.array[i];
andrew@0 25 }
andrew@0 26 else{
andrew@0 27 printf("CANNOT COPY VECTORS OF NON SAME LENGTH!!\n");
andrew@0 28 }
andrew@0 29 }
andrew@0 30
andrew@0 31 void DynamicVector::createVector(int len){
andrew@0 32 array.clear();
andrew@0 33 for (int i = 0; i < len;i++){
andrew@0 34 array.push_back(0);
andrew@0 35 }
andrew@0 36 length = len;
andrew@0 37 arraySize = array.size();
andrew@0 38 }
andrew@0 39
andrew@0 40
andrew@0 41 double DynamicVector::getMaximum(){
andrew@0 42 int i;
andrew@0 43 double max = 0;
andrew@0 44 for (i=0;i < length;i++){
andrew@0 45 if (array[i] > max){
andrew@0 46 max = array[i];
andrew@0 47 MAPestimate = i;
andrew@0 48 }
andrew@0 49 }
andrew@0 50 maximumValue = max;
andrew@0 51 return max;
andrew@0 52 }
andrew@0 53
andrew@0 54 void DynamicVector::zero(){
andrew@0 55 for (int i = 0;i < array.size();i++)
andrew@0 56 array[i] = 0;
andrew@0 57 }
andrew@0 58
andrew@0 59 void DynamicVector::renormalise(){
andrew@0 60 double tmpMax = getMaximum();
andrew@0 61 if (tmpMax > 0){
andrew@0 62 // printf("renormalise : max is %f and size is %i\n", tmpMax, arraySize);
andrew@0 63 for (int i = 0;i < array.size();i++)
andrew@0 64 array[i] /= tmpMax;
andrew@0 65
andrew@0 66 }
andrew@0 67 //printArray();
andrew@0 68 }
andrew@0 69
andrew@0 70 void DynamicVector::doProduct(DynamicVector& arrayOne, DynamicVector& arrayTwo){
andrew@0 71
andrew@0 72 for (int i = 0;i < arrayOne.length;i++)
andrew@0 73 array[i] = arrayOne.array[i] * arrayTwo.array[i];
andrew@0 74 }
andrew@0 75
andrew@0 76
andrew@0 77 void DynamicVector::printArray(){
andrew@0 78 for (int i = 0;i < arraySize;i++){
andrew@0 79 printf("[%i] = %f\n", i, array[i]);
andrew@0 80 }
andrew@0 81 }
andrew@0 82
andrew@0 83 void DynamicVector::translateDistribution(int translationIndex){
andrew@0 84 int tmpIndex;
andrew@0 85 DoubleVector tmpArray;
andrew@0 86 int i;
andrew@0 87
andrew@0 88 for (i=0;i < arraySize;i++){
andrew@0 89 tmpArray.push_back(array[i]);
andrew@0 90 }
andrew@0 91 //translate values
andrew@0 92 for (i=0;i < arraySize;i++){
andrew@0 93 tmpIndex = (i + translationIndex + arraySize)%arraySize;
andrew@0 94 array[tmpIndex] = tmpArray[i];
andrew@0 95 }
andrew@0 96 tmpArray.clear();
andrew@0 97 //now delete tmp array
andrew@0 98 }
andrew@0 99
andrew@0 100 void DynamicVector::addGaussianShape(double mean, double StdDev, double factor){
andrew@0 101 int i;
andrew@0 102 for (i=0;i<array.size();i++){
andrew@0 103 array[i] += factor*(1/(StdDev*sqrt(2*PI)))*exp(-1*(i-mean)*(i-mean)/(2*StdDev*StdDev));
andrew@0 104 }
andrew@0 105 //printf("ADDED GAUSSIAN SHAPE %i\n", (int)array.size());
andrew@0 106 }
andrew@0 107
andrew@0 108 void DynamicVector::addConstant(double value){
andrew@0 109 for (int i=0;i<array.size();i++){
andrew@0 110 array[i] += value;
andrew@0 111 }
andrew@0 112 }
andrew@0 113
andrew@0 114
andrew@0 115 void DynamicVector::addToIndex(int index, double constant){
andrew@0 116 array[index] += constant;
andrew@0 117 }
andrew@0 118
andrew@0 119
andrew@0 120 double DynamicVector::getIndexInRealTerms(const int& index){
andrew@0 121 if (index < arraySize)
andrew@0 122 return (offset + scalar*index);
andrew@0 123 else
andrew@0 124 return 0;
andrew@0 125 }
andrew@0 126
andrew@0 127 double DynamicVector::getRealTermsAsIndex(double value){
andrew@0 128 value -= offset;
andrew@0 129 value /= scalar;
andrew@0 130
andrew@0 131 return value;
andrew@0 132
andrew@0 133 }
andrew@0 134
andrew@0 135 void DynamicVector::drawVector(const int& minIndex, const int& maxIndex){
andrew@0 136
andrew@0 137
andrew@0 138 double stepSize = ofGetWidth() / (double)(maxIndex - minIndex);
andrew@0 139 double screenHeight = ofGetHeight();
andrew@0 140 double maxVal = getMaximum();
andrew@0 141
andrew@0 142 for (int i = max(1,minIndex+1);i < min(maxIndex, (int)array.size());i++){
andrew@0 143 ofLine (stepSize*(i-1), screenHeight * (1 - array[i-1] / maxVal), stepSize*i, screenHeight * (1 - array[i] / maxVal) );
andrew@0 144 }
andrew@0 145
andrew@0 146 }
andrew@0 147
andrew@0 148
andrew@0 149 void DynamicVector::drawConstrainedVector(const int& minIndex, const int& maxIndex, const int& minScreenIndex, const int& maxScreenIndex){
andrew@0 150 //constrain the height and width
andrew@0 151
andrew@0 152 double stepSize = (maxScreenIndex - minScreenIndex) / (double)(maxIndex - minIndex);//step size in pixels per array bin
andrew@0 153 double screenHeight = ofGetHeight();
andrew@0 154 double maxVal = getMaximum();
andrew@0 155
andrew@0 156 //OPTIMIZE!! XXX could just add stepsize each time
andrew@0 157 //not add minindex each time
andrew@0 158 int i = max(1,minIndex+1);
andrew@0 159 // ofDrawBitmapString("i = "+ofToString(i)+" :: screen min: "+ofToString(minScreenIndex + stepSize*(i-minIndex-1)), 20, 640);
andrew@0 160
andrew@0 161 while ((minScreenIndex + stepSize*(i-minIndex)) < 0)
andrew@0 162 i++;//only draw what is on the screen
andrew@0 163
andrew@0 164 for ( ; i < min(maxIndex+1, (int)array.size());i++){
andrew@0 165 ofLine (minScreenIndex + (stepSize*(i-minIndex-1)), screenHeight * (1 - array[i-1] / maxVal),
andrew@0 166 minScreenIndex + (stepSize*(i-minIndex)), screenHeight * (1 - array[i] / maxVal) );
andrew@0 167
andrew@0 168 }
andrew@0 169
andrew@0 170 ofLine(minScreenIndex, screenHeight, minScreenIndex, screenHeight/2);
andrew@0 171 ofLine(maxScreenIndex, screenHeight, maxScreenIndex, screenHeight/2);
andrew@0 172
andrew@0 173 // ofDrawBitmapString(ofToString(stepSize, 2)+" "+ofToString(maxScreenIndex - minScreenIndex, 0), 20, 600);
andrew@0 174
andrew@0 175 }