comparison src/EM.cpp @ 100:8259193b3b16 bqvec

Cut allocations with temporary storage ready to swap in
author Chris Cannam
date Tue, 06 May 2014 14:57:09 +0100
parents 9ecad4c9c2a2
children 19f6832fdc8a
comparison
equal deleted inserted replaced
99:9ecad4c9c2a2 100:8259193b3b16
42 m_sourceSparsity(1.3), 42 m_sourceSparsity(1.3),
43 m_lowestPitch(silvet_templates_lowest_note), 43 m_lowestPitch(silvet_templates_lowest_note),
44 m_highestPitch(silvet_templates_highest_note) 44 m_highestPitch(silvet_templates_highest_note)
45 { 45 {
46 m_pitches = allocate<double>(m_noteCount); 46 m_pitches = allocate<double>(m_noteCount);
47 m_updatePitches = allocate<double>(m_noteCount);
47 for (int n = 0; n < m_noteCount; ++n) { 48 for (int n = 0; n < m_noteCount; ++n) {
48 m_pitches[n] = drand48(); 49 m_pitches[n] = drand48();
49 } 50 }
50 51
51 m_shifts = allocate_channels<double>(m_shiftCount, m_noteCount); 52 m_shifts = allocate_channels<double>(m_shiftCount, m_noteCount);
53 m_updateShifts = allocate_channels<double>(m_shiftCount, m_noteCount);
52 for (int f = 0; f < m_shiftCount; ++f) { 54 for (int f = 0; f < m_shiftCount; ++f) {
53 for (int n = 0; n < m_noteCount; ++n) { 55 for (int n = 0; n < m_noteCount; ++n) {
54 m_shifts[f][n] = drand48(); 56 m_shifts[f][n] = drand48();
55 } 57 }
56 } 58 }
57 59
58 m_sources = allocate_channels<double>(m_sourceCount, m_noteCount); 60 m_sources = allocate_channels<double>(m_sourceCount, m_noteCount);
61 m_updateSources = allocate_channels<double>(m_sourceCount, m_noteCount);
59 for (int i = 0; i < m_sourceCount; ++i) { 62 for (int i = 0; i < m_sourceCount; ++i) {
60 for (int n = 0; n < m_noteCount; ++n) { 63 for (int n = 0; n < m_noteCount; ++n) {
61 m_sources[i][n] = (inRange(i, n) ? 1.0 : 0.0); 64 m_sources[i][n] = (inRange(i, n) ? 1.0 : 0.0);
62 } 65 }
63 } 66 }
69 EM::~EM() 72 EM::~EM()
70 { 73 {
71 deallocate(m_q); 74 deallocate(m_q);
72 deallocate(m_estimate); 75 deallocate(m_estimate);
73 deallocate_channels(m_sources, m_sourceCount); 76 deallocate_channels(m_sources, m_sourceCount);
77 deallocate_channels(m_updateSources, m_sourceCount);
74 deallocate_channels(m_shifts, m_shiftCount); 78 deallocate_channels(m_shifts, m_shiftCount);
79 deallocate_channels(m_updateShifts, m_shiftCount);
75 deallocate(m_pitches); 80 deallocate(m_pitches);
81 deallocate(m_updatePitches);
76 } 82 }
77 83
78 void 84 void
79 EM::rangeFor(int instrument, int &minPitch, int &maxPitch) 85 EM::rangeFor(int instrument, int &minPitch, int &maxPitch)
80 { 86 {
158 } 164 }
159 165
160 void 166 void
161 EM::maximisation(const double *column) 167 EM::maximisation(const double *column)
162 { 168 {
163 double *newPitches = allocate<double>(m_noteCount); 169 v_set(m_updatePitches, epsilon, m_noteCount);
164 v_set(newPitches, epsilon, m_noteCount);
165
166 double **newShifts = allocate_channels<double>(m_shiftCount, m_noteCount);
167 for (int i = 0; i < m_shiftCount; ++i) { 170 for (int i = 0; i < m_shiftCount; ++i) {
168 v_set(newShifts[i], epsilon, m_noteCount); 171 v_set(m_updateShifts[i], epsilon, m_noteCount);
169 } 172 }
170
171 double **newSources = allocate_channels<double>(m_sourceCount, m_noteCount);
172 for (int i = 0; i < m_sourceCount; ++i) { 173 for (int i = 0; i < m_sourceCount; ++i) {
173 v_set(newSources[i], epsilon, m_noteCount); 174 v_set(m_updateSources[i], epsilon, m_noteCount);
174 } 175 }
175 176
176 double *contributions = allocate<double>(m_binCount); 177 double *contributions = allocate<double>(m_binCount);
177 178
178 for (int n = 0; n < m_noteCount; ++n) { 179 for (int n = 0; n < m_noteCount; ++n) {
195 196
196 double total = v_sum(contributions, m_binCount); 197 double total = v_sum(contributions, m_binCount);
197 198
198 if (n >= m_lowestPitch && n <= m_highestPitch) { 199 if (n >= m_lowestPitch && n <= m_highestPitch) {
199 200
200 newPitches[n] += total; 201 m_updatePitches[n] += total;
201 202
202 if (inRange(i, n)) { 203 if (inRange(i, n)) {
203 newSources[i][n] += total; 204 m_updateSources[i][n] += total;
204 } 205 }
205 } 206 }
206 207
207 newShifts[f][n] += total; 208 m_updateShifts[f][n] += total;
208 } 209 }
209 } 210 }
210 } 211 }
211 212
212 for (int n = 0; n < m_noteCount; ++n) { 213 for (int n = 0; n < m_noteCount; ++n) {
213 if (m_pitchSparsity != 1.0) { 214 if (m_pitchSparsity != 1.0) {
214 newPitches[n] = pow(newPitches[n], m_pitchSparsity); 215 m_updatePitches[n] = pow(m_updatePitches[n], m_pitchSparsity);
215 } 216 }
216 if (m_sourceSparsity != 1.0) { 217 if (m_sourceSparsity != 1.0) {
217 for (int i = 0; i < m_sourceCount; ++i) { 218 for (int i = 0; i < m_sourceCount; ++i) {
218 newSources[i][n] = pow(newSources[i][n], m_sourceSparsity); 219 m_updateSources[i][n] = pow(m_updateSources[i][n], m_sourceSparsity);
219 } 220 }
220 } 221 }
221 } 222 }
222 223
223 normaliseColumn(newPitches, m_noteCount); 224 normaliseColumn(m_updatePitches, m_noteCount);
224 normaliseGrid(newShifts, m_shiftCount, m_noteCount); 225 normaliseGrid(m_updateShifts, m_shiftCount, m_noteCount);
225 normaliseGrid(newSources, m_sourceCount, m_noteCount); 226 normaliseGrid(m_updateSources, m_sourceCount, m_noteCount);
226 227
227 deallocate(m_pitches); 228 std::swap(m_pitches, m_updatePitches);
228 deallocate_channels(m_shifts, m_shiftCount); 229 std::swap(m_shifts, m_updateShifts);
229 deallocate_channels(m_sources, m_sourceCount); 230 std::swap(m_sources, m_updateSources);
230 231 }
231 m_pitches = newPitches; 232
232 m_shifts = newShifts; 233
233 m_sources = newSources;
234 }
235
236