andrew@0
|
1 /*
|
andrew@0
|
2 * DynamicVector.cpp
|
andrew@0
|
3 * midiCannamReader
|
andrew@0
|
4 *
|
andrew@0
|
5 * Created by Andrew on 18/07/2011.
|
andrew@0
|
6 * Copyright 2011 QMUL. All rights reserved.
|
andrew@0
|
7 *
|
andrew@0
|
8 */
|
andrew@0
|
9
|
andrew@2
|
10
|
andrew@0
|
11 #include "DynamicVector.h"
|
andrew@0
|
12
|
andrew@0
|
13 DynamicVector::DynamicVector(){
|
andrew@0
|
14 length = 0;
|
andrew@0
|
15 arraySize = 0;
|
andrew@0
|
16 maximumValue = 0;
|
andrew@0
|
17 MAPestimate = 0;
|
andrew@0
|
18 offset = 0;
|
andrew@0
|
19 scalar = 1;
|
andrew@0
|
20 }
|
andrew@0
|
21
|
andrew@0
|
22 void DynamicVector::copyFromDynamicVector(const DynamicVector& dynamicVec){
|
andrew@0
|
23 if (dynamicVec.length == length){
|
andrew@0
|
24 for (int i = 0;i < length;i++)
|
andrew@0
|
25 array[i] = dynamicVec.array[i];
|
andrew@0
|
26 }
|
andrew@0
|
27 else{
|
andrew@0
|
28 printf("CANNOT COPY VECTORS OF NON SAME LENGTH!!\n");
|
andrew@0
|
29 }
|
andrew@0
|
30 }
|
andrew@0
|
31
|
andrew@0
|
32 void DynamicVector::createVector(int len){
|
andrew@0
|
33 array.clear();
|
andrew@0
|
34 for (int i = 0; i < len;i++){
|
andrew@0
|
35 array.push_back(0);
|
andrew@0
|
36 }
|
andrew@0
|
37 length = len;
|
andrew@0
|
38 arraySize = array.size();
|
andrew@0
|
39 }
|
andrew@0
|
40
|
andrew@0
|
41
|
andrew@0
|
42 double DynamicVector::getMaximum(){
|
andrew@0
|
43 int i;
|
andrew@0
|
44 double max = 0;
|
andrew@0
|
45 for (i=0;i < length;i++){
|
andrew@0
|
46 if (array[i] > max){
|
andrew@0
|
47 max = array[i];
|
andrew@0
|
48 MAPestimate = i;
|
andrew@0
|
49 }
|
andrew@0
|
50 }
|
andrew@0
|
51 maximumValue = max;
|
andrew@0
|
52 return max;
|
andrew@0
|
53 }
|
andrew@0
|
54
|
andrew@2
|
55 double DynamicVector::getIntegratedEstimate(){
|
andrew@2
|
56 //returns the index of the integrated average - where the probability distribution is centred
|
andrew@2
|
57 double estimate = 0;
|
andrew@2
|
58 double integratedTotal = 0;
|
andrew@2
|
59 for (int i = 0;i < length;i++){
|
andrew@2
|
60 estimate += array[i]*i;
|
andrew@2
|
61 integratedTotal += array[i];
|
andrew@2
|
62 }
|
andrew@2
|
63 if (integratedTotal > 0){
|
andrew@2
|
64 estimate /= integratedTotal;
|
andrew@2
|
65 }
|
andrew@2
|
66 return estimate;
|
andrew@2
|
67 }
|
andrew@2
|
68
|
andrew@0
|
69 void DynamicVector::zero(){
|
andrew@0
|
70 for (int i = 0;i < array.size();i++)
|
andrew@0
|
71 array[i] = 0;
|
andrew@0
|
72 }
|
andrew@0
|
73
|
andrew@0
|
74 void DynamicVector::renormalise(){
|
andrew@0
|
75 double tmpMax = getMaximum();
|
andrew@0
|
76 if (tmpMax > 0){
|
andrew@0
|
77 // printf("renormalise : max is %f and size is %i\n", tmpMax, arraySize);
|
andrew@0
|
78 for (int i = 0;i < array.size();i++)
|
andrew@0
|
79 array[i] /= tmpMax;
|
andrew@0
|
80
|
andrew@0
|
81 }
|
andrew@0
|
82 //printArray();
|
andrew@0
|
83 }
|
andrew@0
|
84
|
andrew@0
|
85 void DynamicVector::doProduct(DynamicVector& arrayOne, DynamicVector& arrayTwo){
|
andrew@0
|
86
|
andrew@0
|
87 for (int i = 0;i < arrayOne.length;i++)
|
andrew@0
|
88 array[i] = arrayOne.array[i] * arrayTwo.array[i];
|
andrew@0
|
89 }
|
andrew@0
|
90
|
andrew@0
|
91
|
andrew@0
|
92 void DynamicVector::printArray(){
|
andrew@0
|
93 for (int i = 0;i < arraySize;i++){
|
andrew@0
|
94 printf("[%i] = %f\n", i, array[i]);
|
andrew@0
|
95 }
|
andrew@0
|
96 }
|
andrew@0
|
97
|
andrew@0
|
98 void DynamicVector::translateDistribution(int translationIndex){
|
andrew@0
|
99 int tmpIndex;
|
andrew@0
|
100 DoubleVector tmpArray;
|
andrew@0
|
101 int i;
|
andrew@0
|
102
|
andrew@0
|
103 for (i=0;i < arraySize;i++){
|
andrew@0
|
104 tmpArray.push_back(array[i]);
|
andrew@0
|
105 }
|
andrew@0
|
106 //translate values
|
andrew@0
|
107 for (i=0;i < arraySize;i++){
|
andrew@0
|
108 tmpIndex = (i + translationIndex + arraySize)%arraySize;
|
andrew@0
|
109 array[tmpIndex] = tmpArray[i];
|
andrew@0
|
110 }
|
andrew@0
|
111 tmpArray.clear();
|
andrew@0
|
112 //now delete tmp array
|
andrew@0
|
113 }
|
andrew@0
|
114
|
andrew@0
|
115 void DynamicVector::addGaussianShape(double mean, double StdDev, double factor){
|
andrew@0
|
116 int i;
|
andrew@2
|
117 factor *= (1/(StdDev*sqrt(2*PI)));
|
andrew@0
|
118 for (i=0;i<array.size();i++){
|
andrew@2
|
119 array[i] += factor*exp(-1*(i-mean)*(i-mean)/(2*StdDev*StdDev));
|
andrew@0
|
120 }
|
andrew@0
|
121 //printf("ADDED GAUSSIAN SHAPE %i\n", (int)array.size());
|
andrew@0
|
122 }
|
andrew@0
|
123
|
andrew@2
|
124 void DynamicVector::addTriangularShape(double mean, double width, double factor){
|
andrew@2
|
125 int i;
|
andrew@2
|
126
|
andrew@2
|
127 for (i= max(0., (double)(mean - width));i < min((mean+width), (double)array.size());i++){
|
andrew@2
|
128 array[i] += factor * abs(i - mean) / mean;
|
andrew@2
|
129 }
|
andrew@2
|
130
|
andrew@2
|
131 }
|
andrew@2
|
132
|
andrew@0
|
133 void DynamicVector::addConstant(double value){
|
andrew@0
|
134 for (int i=0;i<array.size();i++){
|
andrew@0
|
135 array[i] += value;
|
andrew@0
|
136 }
|
andrew@0
|
137 }
|
andrew@0
|
138
|
andrew@0
|
139
|
andrew@0
|
140 void DynamicVector::addToIndex(int index, double constant){
|
andrew@0
|
141 array[index] += constant;
|
andrew@0
|
142 }
|
andrew@0
|
143
|
andrew@0
|
144
|
andrew@0
|
145 double DynamicVector::getIndexInRealTerms(const int& index){
|
andrew@0
|
146 if (index < arraySize)
|
andrew@0
|
147 return (offset + scalar*index);
|
andrew@0
|
148 else
|
andrew@0
|
149 return 0;
|
andrew@0
|
150 }
|
andrew@0
|
151
|
andrew@0
|
152 double DynamicVector::getRealTermsAsIndex(double value){
|
andrew@0
|
153 value -= offset;
|
andrew@0
|
154 value /= scalar;
|
andrew@0
|
155
|
andrew@0
|
156 return value;
|
andrew@0
|
157
|
andrew@0
|
158 }
|
andrew@0
|
159
|
andrew@1
|
160 double DynamicVector::getValueAtMillis(const double& millis){
|
andrew@1
|
161
|
andrew@1
|
162 int index = round(getRealTermsAsIndex(millis));
|
andrew@1
|
163 if (index >= 0 && index < length)
|
andrew@1
|
164 return array[index];
|
andrew@1
|
165 else
|
andrew@1
|
166 return 0;
|
andrew@1
|
167 }
|
andrew@1
|
168
|
andrew@0
|
169 void DynamicVector::drawVector(const int& minIndex, const int& maxIndex){
|
andrew@0
|
170
|
andrew@0
|
171
|
andrew@0
|
172 double stepSize = ofGetWidth() / (double)(maxIndex - minIndex);
|
andrew@0
|
173 double screenHeight = ofGetHeight();
|
andrew@0
|
174 double maxVal = getMaximum();
|
andrew@0
|
175
|
andrew@0
|
176 for (int i = max(1,minIndex+1);i < min(maxIndex, (int)array.size());i++){
|
andrew@0
|
177 ofLine (stepSize*(i-1), screenHeight * (1 - array[i-1] / maxVal), stepSize*i, screenHeight * (1 - array[i] / maxVal) );
|
andrew@0
|
178 }
|
andrew@0
|
179
|
andrew@0
|
180 }
|
andrew@0
|
181
|
andrew@0
|
182
|
andrew@0
|
183 void DynamicVector::drawConstrainedVector(const int& minIndex, const int& maxIndex, const int& minScreenIndex, const int& maxScreenIndex){
|
andrew@0
|
184 //constrain the height and width
|
andrew@0
|
185
|
andrew@0
|
186 double stepSize = (maxScreenIndex - minScreenIndex) / (double)(maxIndex - minIndex);//step size in pixels per array bin
|
andrew@0
|
187 double screenHeight = ofGetHeight();
|
andrew@0
|
188 double maxVal = getMaximum();
|
andrew@0
|
189
|
andrew@0
|
190 //OPTIMIZE!! XXX could just add stepsize each time
|
andrew@0
|
191 //not add minindex each time
|
andrew@0
|
192 int i = max(1,minIndex+1);
|
andrew@0
|
193 // ofDrawBitmapString("i = "+ofToString(i)+" :: screen min: "+ofToString(minScreenIndex + stepSize*(i-minIndex-1)), 20, 640);
|
andrew@0
|
194
|
andrew@0
|
195 while ((minScreenIndex + stepSize*(i-minIndex)) < 0)
|
andrew@0
|
196 i++;//only draw what is on the screen
|
andrew@0
|
197
|
andrew@0
|
198 for ( ; i < min(maxIndex+1, (int)array.size());i++){
|
andrew@0
|
199 ofLine (minScreenIndex + (stepSize*(i-minIndex-1)), screenHeight * (1 - array[i-1] / maxVal),
|
andrew@0
|
200 minScreenIndex + (stepSize*(i-minIndex)), screenHeight * (1 - array[i] / maxVal) );
|
andrew@0
|
201
|
andrew@0
|
202 }
|
andrew@0
|
203
|
andrew@0
|
204 ofLine(minScreenIndex, screenHeight, minScreenIndex, screenHeight/2);
|
andrew@0
|
205 ofLine(maxScreenIndex, screenHeight, maxScreenIndex, screenHeight/2);
|
andrew@0
|
206
|
andrew@0
|
207 // ofDrawBitmapString(ofToString(stepSize, 2)+" "+ofToString(maxScreenIndex - minScreenIndex, 0), 20, 600);
|
andrew@0
|
208
|
andrew@0
|
209 }
|