annotate hackday/DynamicVector.cpp @ 28:49a5b023df1e

Hackday files comitted - version as demo'd at London hackday
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Mon, 05 Dec 2011 07:00:47 +0000
parents
children 9a70d9abdc8b
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@28 93
andrew@28 94 void DynamicVector::zero(){
andrew@28 95 for (int i = 0;i < array.size();i++)
andrew@28 96 array[i] = 0;
andrew@28 97 }
andrew@28 98
andrew@28 99 void DynamicVector::renormalise(){
andrew@28 100 double tmpMax = getMaximum();
andrew@28 101 if (tmpMax > 0){
andrew@28 102 // printf("renormalise : max is %f and size is %i\n", tmpMax, arraySize);
andrew@28 103 for (int i = 0;i < array.size();i++)
andrew@28 104 array[i] /= tmpMax;
andrew@28 105
andrew@28 106 }
andrew@28 107 //printArray();
andrew@28 108 }
andrew@28 109
andrew@28 110 void DynamicVector::doProduct(DynamicVector& arrayOne, DynamicVector& arrayTwo){
andrew@28 111
andrew@28 112 for (int i = 0;i < arrayOne.length;i++)
andrew@28 113 array[i] = arrayOne.array[i] * arrayTwo.array[i];
andrew@28 114 }
andrew@28 115
andrew@28 116
andrew@28 117 void DynamicVector::printArray(){
andrew@28 118 for (int i = 0;i < arraySize;i++){
andrew@28 119 printf("[%i] = %f\n", i, array[i]);
andrew@28 120 }
andrew@28 121 }
andrew@28 122
andrew@28 123 void DynamicVector::translateDistribution(int translationIndex){
andrew@28 124 int tmpIndex;
andrew@28 125 DoubleVector tmpArray;
andrew@28 126 int i;
andrew@28 127
andrew@28 128 for (i=0;i < arraySize;i++){
andrew@28 129 tmpArray.push_back(array[i]);
andrew@28 130 }
andrew@28 131 //translate values
andrew@28 132 for (i=0;i < arraySize;i++){
andrew@28 133 tmpIndex = (i + translationIndex + arraySize)%arraySize;
andrew@28 134 array[tmpIndex] = tmpArray[i];
andrew@28 135 }
andrew@28 136 tmpArray.clear();
andrew@28 137 //now delete tmp array
andrew@28 138 }
andrew@28 139
andrew@28 140 void DynamicVector::addGaussianShape(const double& mean, const double& StdDev, double factor){
andrew@28 141
andrew@28 142 int i;
andrew@28 143 double std_dev_factor = (2*StdDev*StdDev);
andrew@28 144 factor *= (1/(StdDev*sqrt(2*PI)));
andrew@28 145 int maxVal = min((int) array.size(), (int)(mean + 4.8*StdDev));
andrew@28 146 int minVal = max(0, (int)(mean - 4.8*StdDev));
andrew@28 147
andrew@28 148 for (i=minVal;i < maxVal;i++){
andrew@28 149 array[i] += factor*exp(-1*(i-mean)*(i-mean)/(std_dev_factor));
andrew@28 150 }
andrew@28 151
andrew@28 152 // addGaussianShapeByLookupTable(mean, StdDev, factor);
andrew@28 153 }
andrew@28 154
andrew@28 155 void DynamicVector::addGaussianShapeByLookupTable(double& mean, double& StdDev, double factor){
andrew@28 156 int i;
andrew@28 157 int lookupIndex ;
andrew@28 158 factor *= (1/(StdDev*sqrt(2*PI)));
andrew@28 159 for (i=0;i<array.size()-1;i++){
andrew@28 160 lookupIndex = round(getLookupIndex(i, mean, StdDev));
andrew@28 161 array[i] += factor*gaussianLookupTable[lookupIndex];
andrew@28 162 }
andrew@28 163 //printf("ADDED GAUSSIAN SHAPE %i\n", (int)array.size());
andrew@28 164 }
andrew@28 165
andrew@28 166 double DynamicVector::getLookupIndex(const int& i, const double& mean, const double& StdDev){
andrew@28 167
andrew@28 168 double Z = ((double)i - mean)/StdDev;
andrew@28 169 double lookupIndex = Z*gaussianLookupStdDev + gaussianLookupMean;
andrew@28 170
andrew@28 171 if (lookupIndex < 0)
andrew@28 172 lookupIndex = 0;
andrew@28 173
andrew@28 174 if (lookupIndex >= GAUSSIAN_LOOKUP_LENGTH)
andrew@28 175 lookupIndex = GAUSSIAN_LOOKUP_LENGTH-1;
andrew@28 176
andrew@28 177 // (i - mean)*(i-mean)*(GAUSSIAN_LOOKUP_LENGTH*GAUSSIAN_LOOKUP_LENGTH/16.0)/(StdDev*StdDev);
andrew@28 178 return lookupIndex;
andrew@28 179 }
andrew@28 180
andrew@28 181 void DynamicVector::addTriangularShape(double mean, double width, double factor){
andrew@28 182 int i;
andrew@28 183
andrew@28 184 for (i= max(0., (double)(mean - width));i < min((mean+width), (double)array.size());i++){
andrew@28 185 array[i] += factor * abs(i - mean) / mean;
andrew@28 186 }
andrew@28 187
andrew@28 188 }
andrew@28 189
andrew@28 190 void DynamicVector::addConstant(const double& value){
andrew@28 191 for (int i=0;i<array.size();i++){
andrew@28 192 array[i] += value;
andrew@28 193 }
andrew@28 194 }
andrew@28 195
andrew@28 196
andrew@28 197 void DynamicVector::addToIndex(const int& index, const double& constant){
andrew@28 198 array[index] += constant;
andrew@28 199 }
andrew@28 200
andrew@28 201
andrew@28 202 double DynamicVector::getIndexInRealTerms(const int& index){
andrew@28 203 if (index < arraySize)
andrew@28 204 return (offset + scalar*index);
andrew@28 205 else
andrew@28 206 return 0;
andrew@28 207 }
andrew@28 208
andrew@28 209 double DynamicVector::getRealTermsAsIndex(double value){
andrew@28 210 value -= offset;
andrew@28 211 value /= scalar;
andrew@28 212
andrew@28 213 return value;
andrew@28 214
andrew@28 215 }
andrew@28 216
andrew@28 217 double DynamicVector::getValueAtMillis(const double& millis){
andrew@28 218
andrew@28 219 int index = round(getRealTermsAsIndex(millis));
andrew@28 220 if (index >= 0 && index < length)
andrew@28 221 return array[index];
andrew@28 222 else
andrew@28 223 return 0;
andrew@28 224 }
andrew@28 225
andrew@28 226 void DynamicVector::drawVector(const int& minIndex, const int& maxIndex){
andrew@28 227
andrew@28 228
andrew@28 229 double stepSize = ofGetWidth() / (double)(maxIndex - minIndex);
andrew@28 230 double screenHeight = (double) ofGetHeight();
andrew@28 231 double maxVal = getMaximum();
andrew@28 232
andrew@28 233 int startInt = max(1,minIndex+1);
andrew@28 234 int endInt = min(maxIndex, (int)array.size());
andrew@28 235 double heightConstant = screenHeight / maxVal;
andrew@28 236 int lastHeightPixel = heightConstant * (maxVal - array[startInt-1]);
andrew@28 237 int newHeightPixel;
andrew@28 238 for (int i = startInt;i < endInt;i++){
andrew@28 239 newHeightPixel = (int) heightConstant * (maxVal - array[i]);
andrew@28 240 ofLine (stepSize*(i-1), lastHeightPixel, stepSize*i, newHeightPixel);
andrew@28 241 lastHeightPixel = newHeightPixel;
andrew@28 242 }
andrew@28 243
andrew@28 244 }
andrew@28 245
andrew@28 246
andrew@28 247 void DynamicVector::drawConstrainedVector(const int& minIndex, const int& maxIndex, const int& minScreenIndex, const int& maxScreenIndex){
andrew@28 248 //constrain the height and width
andrew@28 249
andrew@28 250 double stepSize = (maxScreenIndex - minScreenIndex) / (double)(maxIndex - minIndex);//step size in pixels per array bin
andrew@28 251 double screenHeight = ofGetHeight();
andrew@28 252 double maxVal = getMaximum();
andrew@28 253
andrew@28 254 //OPTIMIZE!! XXX could just add stepsize each time
andrew@28 255 //not add minindex each time
andrew@28 256 int i = max(1,minIndex+1);
andrew@28 257 // ofDrawBitmapString("i = "+ofToString(i)+" :: screen min: "+ofToString(minScreenIndex + stepSize*(i-minIndex-1)), 20, 640);
andrew@28 258
andrew@28 259 while ((minScreenIndex + stepSize*(i-minIndex)) < 0)
andrew@28 260 i++;//only draw what is on the screen
andrew@28 261
andrew@28 262 for ( ; i < min(maxIndex+1, (int)array.size());i++){
andrew@28 263 ofLine (minScreenIndex + (stepSize*(i-minIndex-1)), screenHeight * (1 - array[i-1] / maxVal),
andrew@28 264 minScreenIndex + (stepSize*(i-minIndex)), screenHeight * (1 - array[i] / maxVal) );
andrew@28 265
andrew@28 266 }
andrew@28 267
andrew@28 268 ofLine(minScreenIndex, screenHeight, minScreenIndex, screenHeight/2);
andrew@28 269 ofLine(maxScreenIndex, screenHeight, maxScreenIndex, screenHeight/2);
andrew@28 270
andrew@28 271 // ofDrawBitmapString(ofToString(stepSize, 2)+" "+ofToString(maxScreenIndex - minScreenIndex, 0), 20, 600);
andrew@28 272
andrew@28 273 }