comparison src/DynamicVector.cpp @ 4:4a8e6a6cd224

optimised draw function in dynamic vector class. Added Gaussian lookup but not yet used.
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Fri, 19 Aug 2011 15:53:04 +0100
parents 5581023e0de4
children 75dcd1308658
comparison
equal deleted inserted replaced
3:de86d77f2612 4:4a8e6a6cd224
15 arraySize = 0; 15 arraySize = 0;
16 maximumValue = 0; 16 maximumValue = 0;
17 MAPestimate = 0; 17 MAPestimate = 0;
18 offset = 0; 18 offset = 0;
19 scalar = 1; 19 scalar = 1;
20
21 gaussianLookupMean = (double) GAUSSIAN_LOOKUP_LENGTH/2;
22 gaussianLookupStdDev = (double)(GAUSSIAN_LOOKUP_LENGTH/16);
23 double factor = 1.0;//(1.0 / (gaussianLookupStdDev*sqrt(2*PI)) );//1.0;//-1.0/(2*PI*sqrt(gaussianLookupStdDev));
24 for (int i = 0;i < GAUSSIAN_LOOKUP_LENGTH;i++){
25 gaussianLookupTable[i] = factor*exp(-1.0*(i-gaussianLookupMean)*(i-gaussianLookupMean)/(2.0*gaussianLookupStdDev*gaussianLookupStdDev));
26 }
27
20 } 28 }
21 29
22 void DynamicVector::copyFromDynamicVector(const DynamicVector& dynamicVec){ 30 void DynamicVector::copyFromDynamicVector(const DynamicVector& dynamicVec){
23 if (dynamicVec.length == length){ 31 if (dynamicVec.length == length){
24 for (int i = 0;i < length;i++) 32 for (int i = 0;i < length;i++)
111 tmpArray.clear(); 119 tmpArray.clear();
112 //now delete tmp array 120 //now delete tmp array
113 } 121 }
114 122
115 void DynamicVector::addGaussianShape(double mean, double StdDev, double factor){ 123 void DynamicVector::addGaussianShape(double mean, double StdDev, double factor){
124
116 int i; 125 int i;
117 factor *= (1/(StdDev*sqrt(2*PI))); 126 factor *= (1/(StdDev*sqrt(2*PI)));
118 for (i=0;i<array.size();i++){ 127 for (i=0;i<array.size();i++){
119 array[i] += factor*exp(-1*(i-mean)*(i-mean)/(2*StdDev*StdDev)); 128 array[i] += factor*exp(-1*(i-mean)*(i-mean)/(2*StdDev*StdDev));
120 } 129 }
130
131 //addGaussianShapeByLookupTable(mean, StdDev, factor);
132 }
133
134 void DynamicVector::addGaussianShapeByLookupTable(double& mean, double& StdDev, double& factor){
135 int i;
136 int lookupIndex ;
137 factor *= (1/(StdDev*sqrt(2*PI)));
138 for (i=0;i<array.size()-1;i++){
139 lookupIndex = round(getLookupIndex(i, mean, StdDev));
140 array[i] += factor*gaussianLookupTable[lookupIndex];
141 }
121 //printf("ADDED GAUSSIAN SHAPE %i\n", (int)array.size()); 142 //printf("ADDED GAUSSIAN SHAPE %i\n", (int)array.size());
143 }
144
145 double DynamicVector::getLookupIndex(const int& i, const double& mean, const double& StdDev){
146
147 double Z = ((double)i - mean)/StdDev;
148 double lookupIndex = Z*gaussianLookupStdDev + gaussianLookupMean;
149
150 if (lookupIndex < 0)
151 lookupIndex = 0;
152
153 if (lookupIndex >= GAUSSIAN_LOOKUP_LENGTH)
154 lookupIndex = GAUSSIAN_LOOKUP_LENGTH-1;
155
156 // (i - mean)*(i-mean)*(GAUSSIAN_LOOKUP_LENGTH*GAUSSIAN_LOOKUP_LENGTH/16.0)/(StdDev*StdDev);
157 return lookupIndex;
122 } 158 }
123 159
124 void DynamicVector::addTriangularShape(double mean, double width, double factor){ 160 void DynamicVector::addTriangularShape(double mean, double width, double factor){
125 int i; 161 int i;
126 162
170 206
171 207
172 double stepSize = ofGetWidth() / (double)(maxIndex - minIndex); 208 double stepSize = ofGetWidth() / (double)(maxIndex - minIndex);
173 double screenHeight = ofGetHeight(); 209 double screenHeight = ofGetHeight();
174 double maxVal = getMaximum(); 210 double maxVal = getMaximum();
175 211 int startInt = max(1,minIndex+1);
176 for (int i = max(1,minIndex+1);i < min(maxIndex, (int)array.size());i++){ 212 int endInt = min(maxIndex, (int)array.size());
177 ofLine (stepSize*(i-1), screenHeight * (1 - array[i-1] / maxVal), stepSize*i, screenHeight * (1 - array[i] / maxVal) ); 213 double heightConstant = screenHeight / maxVal;
214 int lastHeightPixel = heightConstant * (1 - array[startInt-1]);
215 int newHeightPixel;
216 for (int i = startInt;i < endInt;i++){
217 newHeightPixel = (int) heightConstant * (1 - array[i]);
218 ofLine (stepSize*(i-1), lastHeightPixel, stepSize*i, newHeightPixel);
219 lastHeightPixel = newHeightPixel;
178 } 220 }
179 221
180 } 222 }
181 223
182 224