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