Skip to content

Commit f6bcae5

Browse files
committed
Use initializing constructors in viterbi_decode and refactor size variables Q and T
1 parent 0eec5c4 commit f6bcae5

1 file changed

Lines changed: 19 additions & 39 deletions

File tree

lib/qm-dsp/dsp/tempotracking/TempoTrackV2.cpp

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -233,24 +233,19 @@ TempoTrackV2::viterbi_decode(const d_mat_t &rcfmat, const d_vec_t &wv, d_vec_t &
233233
{
234234
// following Kevin Murphy's Viterbi decoding to get best path of
235235
// beat periods through rfcmat
236-
237-
int wv_len = int(wv.size());
238-
239-
// make transition matrix
240-
d_mat_t tmat;
241-
for (int i = 0; i < wv_len; i++) {
242-
tmat.push_back ( d_vec_t() ); // adds a new column
243-
for (int j = 0; j < wv_len; j++) {
244-
tmat[i].push_back(0.); // fill with zeros initially
245-
}
246-
}
247236

237+
if (rcfmat.size() < 2) return; // can't do anything at all meaningful
238+
239+
const std::size_t T = rcfmat.size();
240+
const std::size_t Q = rcfmat[0].size();
241+
242+
auto tmat = d_mat_t(Q, d_vec_t(Q));
248243
// variance of Gaussians in transition matrix
249244
// formed of Gaussians on diagonal - implies slow tempo change
250245
double sigma = 8.;
251246
// don't want really short beat periods, or really long ones
252-
for (int i = 20; i < wv_len - 20; i++) {
253-
for (int j = 20; j < wv_len - 20; j++) {
247+
for (std::size_t i = 20; i < Q - 20; i++) {
248+
for (int j = 20; j < Q - 20; j++) {
254249
double mu = double(i);
255250
tmat[i][j] = exp( (-1.*pow((j-mu),2.)) / (2.*pow(sigma,2.)) );
256251
}
@@ -259,43 +254,28 @@ TempoTrackV2::viterbi_decode(const d_mat_t &rcfmat, const d_vec_t &wv, d_vec_t &
259254
// parameters for Viterbi decoding... this part is taken from
260255
// Murphy's matlab
261256

262-
d_mat_t delta;
263-
i_mat_t psi;
264-
for (int i = 0; i < int(rcfmat.size()); i++) {
265-
delta.push_back(d_vec_t());
266-
psi.push_back(i_vec_t());
267-
for (int j = 0; j < int(rcfmat[i].size()); j++) {
268-
delta[i].push_back(0.); // fill with zeros initially
269-
psi[i].push_back(0); // fill with zeros initially
270-
}
271-
}
272-
273-
int T = int(delta.size());
274-
275-
if (T < 2) return; // can't do anything at all meaningful
276-
277-
int Q = int(delta[0].size());
257+
auto delta = d_mat_t(T, d_vec_t(Q));
258+
auto psi = i_mat_t(T, i_vec_t(Q));
278259

279260
// initialize first column of delta
280-
for (int j = 0; j < Q; j++) {
261+
for (std::size_t j = 0; j < Q; j++) {
281262
delta[0][j] = wv[j] * rcfmat[0][j];
282-
psi[0][j] = 0;
283263
}
284264

285265
double deltasum = 0.;
286-
for (int i = 0; i < Q; i++) {
266+
for (std::size_t i = 0; i < Q; i++) {
287267
deltasum += delta[0][i];
288268
}
289-
for (int i = 0; i < Q; i++) {
269+
for (std::size_t i = 0; i < Q; i++) {
290270
delta[0][i] /= (deltasum + EPS);
291271
}
292272

293-
for (int t=1; t < T; t++)
273+
for (std::size_t t=1; t < T; t++)
294274
{
295275
d_vec_t tmp_vec(Q);
296276

297-
for (int j = 0; j < Q; j++) {
298-
for (int i = 0; i < Q; i++) {
277+
for (std::size_t j = 0; j < Q; j++) {
278+
for (std::size_t i = 0; i < Q; i++) {
299279
tmp_vec[i] = delta[t-1][i] * tmat[j][i];
300280
}
301281

@@ -308,10 +288,10 @@ TempoTrackV2::viterbi_decode(const d_mat_t &rcfmat, const d_vec_t &wv, d_vec_t &
308288

309289
// normalise current delta column
310290
double deltasum = 0.;
311-
for (int i = 0; i < Q; i++) {
291+
for (std::size_t i = 0; i < Q; i++) {
312292
deltasum += delta[t][i];
313293
}
314-
for (int i = 0; i < Q; i++) {
294+
for (std::size_t i = 0; i < Q; i++) {
315295
delta[t][i] /= (deltasum + EPS);
316296
}
317297
}
@@ -329,7 +309,7 @@ TempoTrackV2::viterbi_decode(const d_mat_t &rcfmat, const d_vec_t &wv, d_vec_t &
329309
bestpath[0] = psi[1][bestpath[1]];
330310

331311
for (std::size_t i = 0; i < beat_period.size(); i++) {
332-
beat_period[i] = bestpath[i/128];
312+
beat_period[i] = bestpath[i/Q];
333313
// std::cerr << "bestpath[" << i << "] = " << bestpath[i] << " (used for beat_periods " << i*step << " to " << i*step+step-1 << ")" << std::endl;
334314
}
335315
}

0 commit comments

Comments
 (0)