Mercurial > hg > silvet
comparison src/EM.cpp @ 114:b2f0967cb8d1 bqvec-openmp
Pull out shifted templates into separate arrays with proper alignment (trading off memory occupied against getting the alignments right)
author | Chris Cannam |
---|---|
date | Wed, 07 May 2014 09:43:15 +0100 |
parents | c4eae816bdb3 |
children |
comparison
equal
deleted
inserted
replaced
113:c4eae816bdb3 | 114:b2f0967cb8d1 |
---|---|
31 | 31 |
32 using namespace breakfastquay; | 32 using namespace breakfastquay; |
33 | 33 |
34 static double epsilon = 1e-16; | 34 static double epsilon = 1e-16; |
35 | 35 |
36 bool EM::m_initialised = false; | |
37 double ****EM::m_templates = 0; | |
38 | |
36 EM::EM(bool useShifts) : | 39 EM::EM(bool useShifts) : |
37 m_noteCount(SILVET_TEMPLATE_NOTE_COUNT), | 40 m_noteCount(SILVET_TEMPLATE_NOTE_COUNT), |
38 m_shiftCount(useShifts ? SILVET_TEMPLATE_MAX_SHIFT * 2 + 1 : 1), | 41 m_shiftCount(useShifts ? SILVET_TEMPLATE_MAX_SHIFT * 2 + 1 : 1), |
39 m_binCount(SILVET_TEMPLATE_HEIGHT), | 42 m_binCount(SILVET_TEMPLATE_HEIGHT), |
40 m_sourceCount(SILVET_TEMPLATE_COUNT), | 43 m_sourceCount(SILVET_TEMPLATE_COUNT), |
41 m_pitchSparsity(1.1), | 44 m_pitchSparsity(1.1), |
42 m_sourceSparsity(1.3), | 45 m_sourceSparsity(1.3), |
43 m_lowestPitch(silvet_templates_lowest_note), | 46 m_lowestPitch(silvet_templates_lowest_note), |
44 m_highestPitch(silvet_templates_highest_note) | 47 m_highestPitch(silvet_templates_highest_note) |
45 { | 48 { |
49 if (!m_initialised) { | |
50 cerr << "ERROR: You must call EM::initialise() before constructing any EM objects" << endl; | |
51 abort(); | |
52 } | |
53 | |
46 m_pitches = allocate<double>(m_noteCount); | 54 m_pitches = allocate<double>(m_noteCount); |
47 m_updatePitches = allocate<double>(m_noteCount); | 55 m_updatePitches = allocate<double>(m_noteCount); |
48 for (int n = 0; n < m_noteCount; ++n) { | 56 for (int n = 0; n < m_noteCount; ++n) { |
49 m_pitches[n] = drand48(); | 57 m_pitches[n] = drand48(); |
58 } | |
59 | |
60 m_sources = allocate_channels<double>(m_sourceCount, m_noteCount); | |
61 m_updateSources = allocate_channels<double>(m_sourceCount, m_noteCount); | |
62 for (int i = 0; i < m_sourceCount; ++i) { | |
63 for (int n = 0; n < m_noteCount; ++n) { | |
64 m_sources[i][n] = (inRange(i, n) ? 1.0 : 0.0); | |
65 } | |
50 } | 66 } |
51 | 67 |
52 if (useShifts) { | 68 if (useShifts) { |
53 m_shifts = allocate_channels<double>(m_shiftCount, m_noteCount); | 69 m_shifts = allocate_channels<double>(m_shiftCount, m_noteCount); |
54 m_updateShifts = allocate_channels<double>(m_shiftCount, m_noteCount); | 70 m_updateShifts = allocate_channels<double>(m_shiftCount, m_noteCount); |
58 } | 74 } |
59 } | 75 } |
60 } else { | 76 } else { |
61 m_shifts = 0; | 77 m_shifts = 0; |
62 m_updateShifts = 0; | 78 m_updateShifts = 0; |
63 } | |
64 | |
65 m_sources = allocate_channels<double>(m_sourceCount, m_noteCount); | |
66 m_updateSources = allocate_channels<double>(m_sourceCount, m_noteCount); | |
67 for (int i = 0; i < m_sourceCount; ++i) { | |
68 for (int n = 0; n < m_noteCount; ++n) { | |
69 m_sources[i][n] = (inRange(i, n) ? 1.0 : 0.0); | |
70 } | |
71 } | 79 } |
72 | 80 |
73 m_estimate = allocate<double>(m_binCount); | 81 m_estimate = allocate<double>(m_binCount); |
74 m_q = allocate<double>(m_binCount); | 82 m_q = allocate<double>(m_binCount); |
75 } | 83 } |
85 deallocate(m_pitches); | 93 deallocate(m_pitches); |
86 deallocate(m_updatePitches); | 94 deallocate(m_updatePitches); |
87 } | 95 } |
88 | 96 |
89 void | 97 void |
98 EM::initialise() | |
99 { | |
100 //!!! need mutex | |
101 | |
102 if (m_initialised) return; | |
103 m_templates = new double ***[SILVET_TEMPLATE_COUNT]; | |
104 for (int i = 0; i < SILVET_TEMPLATE_COUNT; ++i) { | |
105 m_templates[i] = new double **[SILVET_TEMPLATE_NOTE_COUNT]; | |
106 for (int n = 0; n < SILVET_TEMPLATE_NOTE_COUNT; ++n) { | |
107 m_templates[i][n] = new double *[SILVET_TEMPLATE_MAX_SHIFT * 2 + 1]; | |
108 for (int f = 0; f < SILVET_TEMPLATE_MAX_SHIFT * 2 + 1; ++f) { | |
109 m_templates[i][n][f] = allocate<double>(SILVET_TEMPLATE_HEIGHT); | |
110 const float *t = silvet_templates[i].data[n] + f; | |
111 v_convert(m_templates[i][n][f], t, SILVET_TEMPLATE_HEIGHT); | |
112 } | |
113 } | |
114 } | |
115 m_initialised = true; | |
116 } | |
117 | |
118 void | |
90 EM::rangeFor(int instrument, int &minPitch, int &maxPitch) | 119 EM::rangeFor(int instrument, int &minPitch, int &maxPitch) |
91 { | 120 { |
92 minPitch = silvet_templates[instrument].lowest; | 121 minPitch = silvet_templates[instrument].lowest; |
93 maxPitch = silvet_templates[instrument].highest; | 122 maxPitch = silvet_templates[instrument].highest; |
94 } | 123 } |
139 | 168 |
140 const double * | 169 const double * |
141 EM::templateFor(int instrument, int note, int shift) | 170 EM::templateFor(int instrument, int note, int shift) |
142 { | 171 { |
143 if (m_shifts) { | 172 if (m_shifts) { |
144 return silvet_templates[instrument].data[note] + shift; | 173 return m_templates[instrument][note][shift]; |
145 } else { | 174 } else { |
146 return silvet_templates[instrument].data[note] + | 175 return m_templates[instrument][note][SILVET_TEMPLATE_MAX_SHIFT]; |
147 SILVET_TEMPLATE_MAX_SHIFT; | |
148 } | 176 } |
149 } | 177 } |
150 | 178 |
151 void | 179 void |
152 EM::expectation(const double *column) | 180 EM::expectation(const double *column) |