annotate bayesianArraySrc/DynamicVector.cpp @ 3:5e188c0035b6

checking the offsets of the arrays
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Wed, 01 Feb 2012 16:05:26 +0000
parents 179c09199b3c
children 45b5cf9be377
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
andrew@0 11 #include "DynamicVector.h"
andrew@0 12
andrew@0 13 DynamicVector::DynamicVector(){
andrew@0 14 length = 0;
andrew@0 15 arraySize = 0;
andrew@0 16 maximumValue = 0;
andrew@0 17 MAPestimate = 0;
andrew@0 18 offset = 0;
andrew@0 19 scalar = 1;
andrew@0 20 integratedEstimate = length/2;
andrew@0 21
andrew@0 22 gaussianLookupMean = (double) GAUSSIAN_LOOKUP_LENGTH/2;
andrew@0 23 gaussianLookupStdDev = (double)(GAUSSIAN_LOOKUP_LENGTH/16);
andrew@0 24 double factor = 1.0;//(1.0 / (gaussianLookupStdDev*sqrt(2*PI)) );//1.0;//-1.0/(2*PI*sqrt(gaussianLookupStdDev));
andrew@0 25 for (int i = 0;i < GAUSSIAN_LOOKUP_LENGTH;i++){
andrew@0 26 gaussianLookupTable[i] = factor*exp(-1.0*(i-gaussianLookupMean)*(i-gaussianLookupMean)/(2.0*gaussianLookupStdDev*gaussianLookupStdDev));
andrew@0 27 }
andrew@0 28
andrew@0 29 }
andrew@0 30
andrew@0 31 void DynamicVector::copyFromDynamicVector(const DynamicVector& dynamicVec){
andrew@3 32
andrew@0 33 if (dynamicVec.length == length){
andrew@3 34 offset = dynamicVec.offset;
andrew@0 35 for (int i = 0;i < length;i++)
andrew@0 36 array[i] = dynamicVec.array[i];
andrew@0 37 }
andrew@0 38 else{
andrew@0 39 printf("CANNOT COPY VECTORS OF NON SAME LENGTH!!\n");
andrew@0 40 }
andrew@0 41 }
andrew@0 42
andrew@0 43 void DynamicVector::createVector(int len){
andrew@0 44 array.clear();
andrew@0 45 for (int i = 0; i < len;i++){
andrew@0 46 array.push_back(0);
andrew@0 47 }
andrew@0 48 length = len;
andrew@0 49 arraySize = array.size();
andrew@0 50 integratedEstimate = length/2;
andrew@0 51 }
andrew@0 52
andrew@0 53
andrew@0 54 double DynamicVector::getMaximum(){
andrew@0 55 int i;
andrew@0 56 double max = 0;
andrew@0 57 for (i=0;i < length;i++){
andrew@0 58 if (array[i] > max){
andrew@0 59 max = array[i];
andrew@0 60 MAPestimate = i;
andrew@0 61 }
andrew@0 62 }
andrew@0 63 maximumValue = max;
andrew@0 64 return max;
andrew@0 65 }
andrew@0 66
andrew@0 67 double DynamicVector::getIntegratedEstimate(){
andrew@0 68 //returns the index of the integrated average - where the probability distribution is centred
andrew@0 69 integratedEstimate = 0;
andrew@0 70 double integratedTotal = 0;
andrew@0 71 for (int i = 0;i < length;i++){
andrew@0 72 integratedEstimate += array[i]*i;
andrew@0 73 integratedTotal += array[i];
andrew@0 74 }
andrew@0 75 if (integratedTotal > 0){
andrew@0 76 integratedEstimate /= integratedTotal;
andrew@0 77 }
andrew@0 78 return integratedEstimate;
andrew@0 79 }
andrew@0 80
andrew@0 81 void DynamicVector::updateIntegratedEstimate(){
andrew@0 82 //returns the index of the integrated average - where the probability distribution is centred
andrew@0 83 integratedEstimate = 0;
andrew@0 84 double integratedTotal = 0;
andrew@0 85 for (int i = 0;i < length;i++){
andrew@0 86 integratedEstimate += array[i]*i;
andrew@0 87 integratedTotal += array[i];
andrew@0 88 }
andrew@0 89 if (integratedTotal > 0){
andrew@0 90 integratedEstimate /= integratedTotal;
andrew@0 91 }
andrew@0 92
andrew@0 93 }
andrew@0 94
andrew@0 95 void DynamicVector::updateLimitedIntegratedEstimate(){
andrew@0 96 //returns the index of the integrated average - where the probability distribution is centred
andrew@0 97 //but limited round the MAP estimate
andrew@0 98 double tmp = getMaximum();
andrew@0 99 int limit = min(MAPestimate, length - MAPestimate);
andrew@0 100 int start = max(0, MAPestimate - limit);
andrew@0 101 int end = min(MAPestimate + limit, length-1);
andrew@0 102
andrew@0 103 integratedEstimate = 0;
andrew@0 104 double integratedTotal = 0;
andrew@0 105 for (int i = start;i <= end;i++){
andrew@0 106 integratedEstimate += array[i]*i;
andrew@0 107 integratedTotal += array[i];
andrew@0 108 }
andrew@0 109 if (integratedTotal > 0){
andrew@0 110 integratedEstimate /= integratedTotal;
andrew@0 111 }
andrew@0 112
andrew@0 113 }
andrew@0 114
andrew@0 115
andrew@0 116 void DynamicVector::zero(){
andrew@0 117 for (int i = 0;i < array.size();i++)
andrew@0 118 array[i] = 0;
andrew@0 119 }
andrew@0 120
andrew@0 121 void DynamicVector::renormalise(){
andrew@0 122 double tmpMax = getMaximum();
andrew@0 123 if (tmpMax > 0){
andrew@0 124 // printf("renormalise : max is %f and size is %i\n", tmpMax, arraySize);
andrew@0 125 for (int i = 0;i < array.size();i++)
andrew@0 126 array[i] /= tmpMax;
andrew@0 127
andrew@0 128 }
andrew@0 129 //printArray();
andrew@0 130 }
andrew@0 131
andrew@0 132 void DynamicVector::doProduct(DynamicVector& arrayOne, DynamicVector& arrayTwo){
andrew@0 133
andrew@0 134 for (int i = 0;i < arrayOne.length;i++)
andrew@0 135 array[i] = arrayOne.array[i] * arrayTwo.array[i];
andrew@0 136 }
andrew@0 137
andrew@0 138
andrew@0 139 void DynamicVector::printArray(){
andrew@0 140 for (int i = 0;i < arraySize;i++){
andrew@0 141 printf("[%i] = %f\n", i, array[i]);
andrew@0 142 }
andrew@0 143 }
andrew@0 144
andrew@0 145 void DynamicVector::translateDistribution(int translationIndex){
andrew@0 146 int tmpIndex;
andrew@0 147 DoubleVector tmpArray;
andrew@0 148 int i;
andrew@0 149
andrew@0 150 for (i=0;i < arraySize;i++){
andrew@0 151 tmpArray.push_back(array[i]);
andrew@0 152 }
andrew@0 153 //translate values
andrew@0 154 for (i=0;i < arraySize;i++){
andrew@0 155 tmpIndex = (i + translationIndex + arraySize)%arraySize;
andrew@0 156 array[tmpIndex] = tmpArray[i];
andrew@0 157 }
andrew@0 158 tmpArray.clear();
andrew@0 159 //now delete tmp array
andrew@0 160 }
andrew@0 161
andrew@2 162
andrew@2 163
andrew@2 164 void DynamicVector::addGaussianShapeFromRealTime(const double& actualTime, const double& StdDev, double factor){
andrew@2 165
andrew@2 166 double mean = getRealTermsAsIndex(actualTime);
andrew@3 167 // printf("Gaussian realtime %f at index %f\n", actualTime, mean);
andrew@2 168 int i;
andrew@2 169 double std_dev_factor = (2*StdDev*StdDev);
andrew@2 170 factor *= (1/(StdDev*sqrt(2*PI)));
andrew@2 171 int maxVal = min((int) array.size(), (int)(mean + 4.8*StdDev));
andrew@2 172 int minVal = max(0, (int)(mean - 4.8*StdDev));
andrew@2 173
andrew@2 174 for (i=minVal;i < maxVal;i++){
andrew@2 175 array[i] += factor*exp(-1*(i-mean)*(i-mean)/(std_dev_factor));
andrew@2 176 }
andrew@2 177
andrew@2 178 // addGaussianShapeByLookupTable(mean, StdDev, factor);
andrew@2 179 }
andrew@2 180
andrew@2 181
andrew@0 182 void DynamicVector::addGaussianShape(const double& mean, const double& StdDev, double factor){
andrew@0 183
andrew@0 184 int i;
andrew@0 185 double std_dev_factor = (2*StdDev*StdDev);
andrew@0 186 factor *= (1/(StdDev*sqrt(2*PI)));
andrew@0 187 int maxVal = min((int) array.size(), (int)(mean + 4.8*StdDev));
andrew@0 188 int minVal = max(0, (int)(mean - 4.8*StdDev));
andrew@0 189
andrew@0 190 for (i=minVal;i < maxVal;i++){
andrew@0 191 array[i] += factor*exp(-1*(i-mean)*(i-mean)/(std_dev_factor));
andrew@0 192 }
andrew@0 193
andrew@0 194 // addGaussianShapeByLookupTable(mean, StdDev, factor);
andrew@0 195 }
andrew@0 196
andrew@0 197 void DynamicVector::addGaussianShapeByLookupTable(double& mean, double& StdDev, double factor){
andrew@0 198 int i;
andrew@0 199 int lookupIndex ;
andrew@0 200 factor *= (1/(StdDev*sqrt(2*PI)));
andrew@0 201 for (i=0;i<array.size()-1;i++){
andrew@0 202 lookupIndex = round(getLookupIndex(i, mean, StdDev));
andrew@0 203 array[i] += factor*gaussianLookupTable[lookupIndex];
andrew@0 204 }
andrew@0 205 //printf("ADDED GAUSSIAN SHAPE %i\n", (int)array.size());
andrew@0 206 }
andrew@0 207
andrew@0 208 double DynamicVector::getLookupIndex(const int& i, const double& mean, const double& StdDev){
andrew@0 209
andrew@0 210 double Z = ((double)i - mean)/StdDev;
andrew@0 211 double lookupIndex = Z*gaussianLookupStdDev + gaussianLookupMean;
andrew@0 212
andrew@0 213 if (lookupIndex < 0)
andrew@0 214 lookupIndex = 0;
andrew@0 215
andrew@0 216 if (lookupIndex >= GAUSSIAN_LOOKUP_LENGTH)
andrew@0 217 lookupIndex = GAUSSIAN_LOOKUP_LENGTH-1;
andrew@0 218
andrew@0 219 // (i - mean)*(i-mean)*(GAUSSIAN_LOOKUP_LENGTH*GAUSSIAN_LOOKUP_LENGTH/16.0)/(StdDev*StdDev);
andrew@0 220 return lookupIndex;
andrew@0 221 }
andrew@0 222
andrew@0 223 void DynamicVector::addTriangularShape(double mean, double width, double factor){
andrew@0 224 int i;
andrew@0 225
andrew@0 226 for (i= max(0., (double)(mean - width));i < min((mean+width), (double)array.size());i++){
andrew@0 227 array[i] += factor * abs(i - mean) / mean;
andrew@0 228 }
andrew@0 229
andrew@0 230 }
andrew@0 231
andrew@0 232 void DynamicVector::addConstant(const double& value){
andrew@0 233 for (int i=0;i<array.size();i++){
andrew@0 234 array[i] += value;
andrew@0 235 }
andrew@0 236 }
andrew@0 237
andrew@0 238
andrew@0 239 void DynamicVector::addToIndex(const int& index, const double& constant){
andrew@0 240 array[index] += constant;
andrew@0 241 }
andrew@0 242
andrew@0 243
andrew@0 244 double DynamicVector::getIndexInRealTerms(const int& index){
andrew@0 245 if (index < arraySize)
andrew@0 246 return (offset + scalar*index);
andrew@0 247 else
andrew@0 248 return 0;
andrew@0 249 }
andrew@0 250
andrew@0 251 double DynamicVector::getRealTermsAsIndex(double value){
andrew@0 252 value -= offset;
andrew@0 253 value /= scalar;
andrew@0 254
andrew@0 255 return value;
andrew@0 256
andrew@0 257 }
andrew@0 258
andrew@0 259 double DynamicVector::getValueAtMillis(const double& millis){
andrew@0 260
andrew@0 261 int index = round(getRealTermsAsIndex(millis));
andrew@0 262 if (index >= 0 && index < length)
andrew@0 263 return array[index];
andrew@0 264 else
andrew@0 265 return 0;
andrew@0 266 }
andrew@0 267
andrew@0 268 void DynamicVector::drawVector(const int& minIndex, const int& maxIndex){
andrew@0 269
andrew@0 270
andrew@0 271 double stepSize = ofGetWidth() / (double)(maxIndex - minIndex);
andrew@0 272 double screenHeight = (double) ofGetHeight();
andrew@0 273 double maxVal = getMaximum();
andrew@0 274
andrew@0 275 int startInt = max(1,minIndex+1);
andrew@0 276 int endInt = min(maxIndex, (int)array.size());
andrew@0 277 double heightConstant = screenHeight / maxVal;
andrew@0 278 int lastHeightPixel = heightConstant * (maxVal - array[startInt-1]);
andrew@0 279 int newHeightPixel;
andrew@0 280 for (int i = startInt;i < endInt;i++){
andrew@0 281 newHeightPixel = (int) heightConstant * (maxVal - array[i]);
andrew@3 282 int xPos = i - startInt;
andrew@3 283 ofLine (stepSize*(xPos-1), lastHeightPixel, stepSize*xPos, newHeightPixel);
andrew@0 284 lastHeightPixel = newHeightPixel;
andrew@0 285 }
andrew@0 286
andrew@0 287 }
andrew@0 288
andrew@0 289
andrew@0 290 void DynamicVector::drawVector(const int& minIndex, const int& maxIndex, ofxWindowRegion window){
andrew@0 291
andrew@0 292
andrew@0 293 double stepSize = window.width / (double)(maxIndex - minIndex);
andrew@0 294 double screenHeight = (double) window.height;
andrew@0 295 double maxVal = getMaximum();
andrew@0 296
andrew@0 297 int startInt = max(1,minIndex+1);
andrew@0 298 int endInt = min(maxIndex, (int)array.size());
andrew@0 299 double heightConstant = screenHeight / maxVal;
andrew@0 300 int lastHeightPixel = heightConstant * (maxVal - array[startInt-1]);
andrew@0 301 int newHeightPixel;
andrew@0 302 for (int i = startInt;i < endInt;i++){
andrew@0 303 newHeightPixel = (int) heightConstant * (maxVal - array[i]);
andrew@3 304 int xPos = i - startInt;
andrew@3 305 ofLine (window.x+stepSize*(xPos-1), window.y+lastHeightPixel, window.x+stepSize*xPos, window.y+newHeightPixel);
andrew@0 306 lastHeightPixel = newHeightPixel;
andrew@0 307 }
andrew@3 308 ofDrawBitmapString("start index "+ofToString(startInt)+" end "+ofToString(endInt), window.x+20, window.y+20);
andrew@0 309 }
andrew@0 310
andrew@0 311
andrew@0 312 void DynamicVector::drawConstrainedVector(const int& minIndex, const int& maxIndex, const int& minScreenIndex, const int& maxScreenIndex){
andrew@0 313 //constrain the height and width
andrew@0 314
andrew@0 315 double stepSize = (maxScreenIndex - minScreenIndex) / (double)(maxIndex - minIndex);//step size in pixels per array bin
andrew@0 316 double screenHeight = ofGetHeight();
andrew@0 317 double maxVal = getMaximum();
andrew@0 318
andrew@0 319 //OPTIMIZE!! XXX could just add stepsize each time
andrew@0 320 //not add minindex each time
andrew@0 321 int i = max(1,minIndex+1);
andrew@0 322 // ofDrawBitmapString("i = "+ofToString(i)+" :: screen min: "+ofToString(minScreenIndex + stepSize*(i-minIndex-1)), 20, 640);
andrew@0 323
andrew@0 324 while ((minScreenIndex + stepSize*(i-minIndex)) < 0)
andrew@0 325 i++;//only draw what is on the screen
andrew@0 326
andrew@0 327 for ( ; i < min(maxIndex+1, (int)array.size());i++){
andrew@0 328 ofLine (minScreenIndex + (stepSize*(i-minIndex-1)), screenHeight * (1 - array[i-1] / maxVal),
andrew@0 329 minScreenIndex + (stepSize*(i-minIndex)), screenHeight * (1 - array[i] / maxVal) );
andrew@0 330
andrew@0 331 }
andrew@0 332
andrew@0 333 ofLine(minScreenIndex, screenHeight, minScreenIndex, screenHeight/2);
andrew@0 334 ofLine(maxScreenIndex, screenHeight, maxScreenIndex, screenHeight/2);
andrew@0 335
andrew@0 336 // ofDrawBitmapString(ofToString(stepSize, 2)+" "+ofToString(maxScreenIndex - minScreenIndex, 0), 20, 600);
andrew@0 337
andrew@0 338 }