comparison src/EM.cpp @ 91:2b0818a1c058 bqvec

First bit of bqvec adaptation
author Chris Cannam
date Tue, 06 May 2014 13:49:52 +0100
parents a6e136aaa202
children 81eaba98985b
comparison
equal deleted inserted replaced
90:f1116eb464f9 91:2b0818a1c058
20 #include <cstdlib> 20 #include <cstdlib>
21 #include <cmath> 21 #include <cmath>
22 22
23 #include <iostream> 23 #include <iostream>
24 24
25 #include <vector> 25 #include "VectorOps.h"
26 #include "Allocators.h"
26 27
27 using std::vector; 28 using std::vector;
28 using std::cerr; 29 using std::cerr;
29 using std::endl; 30 using std::endl;
31
32 using namespace breakfastquay;
30 33
31 static double epsilon = 1e-16; 34 static double epsilon = 1e-16;
32 35
33 EM::EM() : 36 EM::EM() :
34 m_noteCount(SILVET_TEMPLATE_NOTE_COUNT), 37 m_noteCount(SILVET_TEMPLATE_NOTE_COUNT),
35 m_shiftCount(SILVET_TEMPLATE_MAX_SHIFT * 2 + 1), 38 m_shiftCount(SILVET_TEMPLATE_MAX_SHIFT * 2 + 1),
36 m_binCount(SILVET_TEMPLATE_HEIGHT), 39 m_binCount(SILVET_TEMPLATE_HEIGHT),
37 m_instrumentCount(SILVET_TEMPLATE_COUNT), 40 m_sourceCount(SILVET_TEMPLATE_COUNT),
38 m_pitchSparsity(1.1), 41 m_pitchSparsity(1.1),
39 m_sourceSparsity(1.3), 42 m_sourceSparsity(1.3),
40 m_lowestPitch(silvet_templates_lowest_note), 43 m_lowestPitch(silvet_templates_lowest_note),
41 m_highestPitch(silvet_templates_highest_note) 44 m_highestPitch(silvet_templates_highest_note)
42 { 45 {
43 m_pitches = V(m_noteCount); 46 m_pitches = allocate<double>(m_noteCount);
44 for (int n = 0; n < m_noteCount; ++n) { 47 for (int n = 0; n < m_noteCount; ++n) {
45 m_pitches[n] = drand48(); 48 m_pitches[n] = drand48();
46 } 49 }
47 50
48 m_shifts = Grid(m_shiftCount); 51 m_shifts = allocate_channels<double>(m_shiftCount, m_noteCount);
49 for (int f = 0; f < m_shiftCount; ++f) { 52 for (int f = 0; f < m_shiftCount; ++f) {
50 m_shifts[f] = V(m_noteCount);
51 for (int n = 0; n < m_noteCount; ++n) { 53 for (int n = 0; n < m_noteCount; ++n) {
52 m_shifts[f][n] = drand48(); 54 m_shifts[f][n] = drand48();
53 } 55 }
54 } 56 }
55 57
56 m_sources = Grid(m_instrumentCount); 58 m_sources = allocate_channels<double>(m_sourceCount, m_noteCount);
57 for (int i = 0; i < m_instrumentCount; ++i) { 59 for (int i = 0; i < m_sourceCount; ++i) {
58 m_sources[i] = V(m_noteCount);
59 for (int n = 0; n < m_noteCount; ++n) { 60 for (int n = 0; n < m_noteCount; ++n) {
60 m_sources[i][n] = (inRange(i, n) ? 1.0 : 0.0); 61 m_sources[i][n] = (inRange(i, n) ? 1.0 : 0.0);
61 } 62 }
62 } 63 }
63 64
64 m_estimate = V(m_binCount); 65 m_estimate = allocate<double>(m_binCount);
65 m_q = V(m_binCount); 66 m_q = allocate<double>(m_binCount);
66 } 67 }
67 68
68 EM::~EM() 69 EM::~EM()
69 { 70 {
70 } 71 }
135 136
136 for (int i = 0; i < m_binCount; ++i) { 137 for (int i = 0; i < m_binCount; ++i) {
137 m_estimate[i] = epsilon; 138 m_estimate[i] = epsilon;
138 } 139 }
139 140
140 for (int i = 0; i < m_instrumentCount; ++i) { 141 for (int i = 0; i < m_sourceCount; ++i) {
141 for (int n = 0; n < m_noteCount; ++n) { 142 for (int n = 0; n < m_noteCount; ++n) {
142 const double pitch = m_pitches[n]; 143 const double pitch = m_pitches[n];
143 const double source = m_sources[i][n]; 144 const double source = m_sources[i][n];
144 for (int f = 0; f < m_shiftCount; ++f) { 145 for (int f = 0; f < m_shiftCount; ++f) {
145 const double *w = templateFor(i, n, f); 146 const double *w = templateFor(i, n, f);
160 void 161 void
161 EM::maximisation(const V &column) 162 EM::maximisation(const V &column)
162 { 163 {
163 V newPitches(m_noteCount, epsilon); 164 V newPitches(m_noteCount, epsilon);
164 Grid newShifts(m_shiftCount, V(m_noteCount, epsilon)); 165 Grid newShifts(m_shiftCount, V(m_noteCount, epsilon));
165 Grid newSources(m_instrumentCount, V(m_noteCount, epsilon)); 166 Grid newSources(m_sourceCount, V(m_noteCount, epsilon));
166 167
167 for (int n = 0; n < m_noteCount; ++n) { 168 for (int n = 0; n < m_noteCount; ++n) {
168 169
169 const double pitch = m_pitches[n]; 170 const double pitch = m_pitches[n];
170 171
171 for (int f = 0; f < m_shiftCount; ++f) { 172 for (int f = 0; f < m_shiftCount; ++f) {
172 173
173 const double shift = m_shifts[f][n]; 174 const double shift = m_shifts[f][n];
174 175
175 for (int i = 0; i < m_instrumentCount; ++i) { 176 for (int i = 0; i < m_sourceCount; ++i) {
176 177
177 const double source = m_sources[i][n]; 178 const double source = m_sources[i][n];
178 const double factor = pitch * source * shift; 179 const double factor = pitch * source * shift;
179 const double *w = templateFor(i, n, f); 180 const double *w = templateFor(i, n, f);
180 181
201 for (int n = 0; n < m_noteCount; ++n) { 202 for (int n = 0; n < m_noteCount; ++n) {
202 if (m_pitchSparsity != 1.0) { 203 if (m_pitchSparsity != 1.0) {
203 newPitches[n] = pow(newPitches[n], m_pitchSparsity); 204 newPitches[n] = pow(newPitches[n], m_pitchSparsity);
204 } 205 }
205 if (m_sourceSparsity != 1.0) { 206 if (m_sourceSparsity != 1.0) {
206 for (int i = 0; i < m_instrumentCount; ++i) { 207 for (int i = 0; i < m_sourceCount; ++i) {
207 newSources[i][n] = pow(newSources[i][n], m_sourceSparsity); 208 newSources[i][n] = pow(newSources[i][n], m_sourceSparsity);
208 } 209 }
209 } 210 }
210 } 211 }
211 212