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