annotate jnmr/DynamicVector.cpp @ 52:13194a9dca77 tip

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