Mercurial > hg > midi-score-follower
comparison src/DynamicVector.cpp @ 0:b299a65a3ad0
start project
author | Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk> |
---|---|
date | Tue, 16 Aug 2011 11:29:59 +0100 |
parents | |
children | 1a32ce016bb9 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b299a65a3ad0 |
---|---|
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 #include "DynamicVector.h" | |
11 | |
12 DynamicVector::DynamicVector(){ | |
13 length = 0; | |
14 arraySize = 0; | |
15 maximumValue = 0; | |
16 MAPestimate = 0; | |
17 offset = 0; | |
18 scalar = 1; | |
19 } | |
20 | |
21 void DynamicVector::copyFromDynamicVector(const DynamicVector& dynamicVec){ | |
22 if (dynamicVec.length == length){ | |
23 for (int i = 0;i < length;i++) | |
24 array[i] = dynamicVec.array[i]; | |
25 } | |
26 else{ | |
27 printf("CANNOT COPY VECTORS OF NON SAME LENGTH!!\n"); | |
28 } | |
29 } | |
30 | |
31 void DynamicVector::createVector(int len){ | |
32 array.clear(); | |
33 for (int i = 0; i < len;i++){ | |
34 array.push_back(0); | |
35 } | |
36 length = len; | |
37 arraySize = array.size(); | |
38 } | |
39 | |
40 | |
41 double DynamicVector::getMaximum(){ | |
42 int i; | |
43 double max = 0; | |
44 for (i=0;i < length;i++){ | |
45 if (array[i] > max){ | |
46 max = array[i]; | |
47 MAPestimate = i; | |
48 } | |
49 } | |
50 maximumValue = max; | |
51 return max; | |
52 } | |
53 | |
54 void DynamicVector::zero(){ | |
55 for (int i = 0;i < array.size();i++) | |
56 array[i] = 0; | |
57 } | |
58 | |
59 void DynamicVector::renormalise(){ | |
60 double tmpMax = getMaximum(); | |
61 if (tmpMax > 0){ | |
62 // printf("renormalise : max is %f and size is %i\n", tmpMax, arraySize); | |
63 for (int i = 0;i < array.size();i++) | |
64 array[i] /= tmpMax; | |
65 | |
66 } | |
67 //printArray(); | |
68 } | |
69 | |
70 void DynamicVector::doProduct(DynamicVector& arrayOne, DynamicVector& arrayTwo){ | |
71 | |
72 for (int i = 0;i < arrayOne.length;i++) | |
73 array[i] = arrayOne.array[i] * arrayTwo.array[i]; | |
74 } | |
75 | |
76 | |
77 void DynamicVector::printArray(){ | |
78 for (int i = 0;i < arraySize;i++){ | |
79 printf("[%i] = %f\n", i, array[i]); | |
80 } | |
81 } | |
82 | |
83 void DynamicVector::translateDistribution(int translationIndex){ | |
84 int tmpIndex; | |
85 DoubleVector tmpArray; | |
86 int i; | |
87 | |
88 for (i=0;i < arraySize;i++){ | |
89 tmpArray.push_back(array[i]); | |
90 } | |
91 //translate values | |
92 for (i=0;i < arraySize;i++){ | |
93 tmpIndex = (i + translationIndex + arraySize)%arraySize; | |
94 array[tmpIndex] = tmpArray[i]; | |
95 } | |
96 tmpArray.clear(); | |
97 //now delete tmp array | |
98 } | |
99 | |
100 void DynamicVector::addGaussianShape(double mean, double StdDev, double factor){ | |
101 int i; | |
102 for (i=0;i<array.size();i++){ | |
103 array[i] += factor*(1/(StdDev*sqrt(2*PI)))*exp(-1*(i-mean)*(i-mean)/(2*StdDev*StdDev)); | |
104 } | |
105 //printf("ADDED GAUSSIAN SHAPE %i\n", (int)array.size()); | |
106 } | |
107 | |
108 void DynamicVector::addConstant(double value){ | |
109 for (int i=0;i<array.size();i++){ | |
110 array[i] += value; | |
111 } | |
112 } | |
113 | |
114 | |
115 void DynamicVector::addToIndex(int index, double constant){ | |
116 array[index] += constant; | |
117 } | |
118 | |
119 | |
120 double DynamicVector::getIndexInRealTerms(const int& index){ | |
121 if (index < arraySize) | |
122 return (offset + scalar*index); | |
123 else | |
124 return 0; | |
125 } | |
126 | |
127 double DynamicVector::getRealTermsAsIndex(double value){ | |
128 value -= offset; | |
129 value /= scalar; | |
130 | |
131 return value; | |
132 | |
133 } | |
134 | |
135 void DynamicVector::drawVector(const int& minIndex, const int& maxIndex){ | |
136 | |
137 | |
138 double stepSize = ofGetWidth() / (double)(maxIndex - minIndex); | |
139 double screenHeight = ofGetHeight(); | |
140 double maxVal = getMaximum(); | |
141 | |
142 for (int i = max(1,minIndex+1);i < min(maxIndex, (int)array.size());i++){ | |
143 ofLine (stepSize*(i-1), screenHeight * (1 - array[i-1] / maxVal), stepSize*i, screenHeight * (1 - array[i] / maxVal) ); | |
144 } | |
145 | |
146 } | |
147 | |
148 | |
149 void DynamicVector::drawConstrainedVector(const int& minIndex, const int& maxIndex, const int& minScreenIndex, const int& maxScreenIndex){ | |
150 //constrain the height and width | |
151 | |
152 double stepSize = (maxScreenIndex - minScreenIndex) / (double)(maxIndex - minIndex);//step size in pixels per array bin | |
153 double screenHeight = ofGetHeight(); | |
154 double maxVal = getMaximum(); | |
155 | |
156 //OPTIMIZE!! XXX could just add stepsize each time | |
157 //not add minindex each time | |
158 int i = max(1,minIndex+1); | |
159 // ofDrawBitmapString("i = "+ofToString(i)+" :: screen min: "+ofToString(minScreenIndex + stepSize*(i-minIndex-1)), 20, 640); | |
160 | |
161 while ((minScreenIndex + stepSize*(i-minIndex)) < 0) | |
162 i++;//only draw what is on the screen | |
163 | |
164 for ( ; i < min(maxIndex+1, (int)array.size());i++){ | |
165 ofLine (minScreenIndex + (stepSize*(i-minIndex-1)), screenHeight * (1 - array[i-1] / maxVal), | |
166 minScreenIndex + (stepSize*(i-minIndex)), screenHeight * (1 - array[i] / maxVal) ); | |
167 | |
168 } | |
169 | |
170 ofLine(minScreenIndex, screenHeight, minScreenIndex, screenHeight/2); | |
171 ofLine(maxScreenIndex, screenHeight, maxScreenIndex, screenHeight/2); | |
172 | |
173 // ofDrawBitmapString(ofToString(stepSize, 2)+" "+ofToString(maxScreenIndex - minScreenIndex, 0), 20, 600); | |
174 | |
175 } |