comparison dsp/tempotracking/TempoTrackV2.cpp @ 505:930b5b0f707d

Merge branch 'codestyle-and-tidy'
author Chris Cannam <cannam@all-day-breakfast.com>
date Wed, 05 Jun 2019 12:55:15 +0100
parents 162673c8f9de
children
comparison
equal deleted inserted replaced
471:e3335cb213da 505:930b5b0f707d
19 #include <cstdlib> 19 #include <cstdlib>
20 #include <iostream> 20 #include <iostream>
21 21
22 #include "maths/MathUtilities.h" 22 #include "maths/MathUtilities.h"
23 23
24 using std::vector;
25
24 #define EPS 0.0000008 // just some arbitrary small number 26 #define EPS 0.0000008 // just some arbitrary small number
25 27
26 TempoTrackV2::TempoTrackV2(float rate, size_t increment) : 28 TempoTrackV2::TempoTrackV2(float rate, int increment) :
27 m_rate(rate), m_increment(increment) { } 29 m_rate(rate), m_increment(increment) {
30 }
31
28 TempoTrackV2::~TempoTrackV2() { } 32 TempoTrackV2::~TempoTrackV2() { }
29 33
30 void 34 void
31 TempoTrackV2::filter_df(d_vec_t &df) 35 TempoTrackV2::filter_df(d_vec_t &df)
32 { 36 {
37 int df_len = int(df.size());
38
33 d_vec_t a(3); 39 d_vec_t a(3);
34 d_vec_t b(3); 40 d_vec_t b(3);
35 d_vec_t lp_df(df.size()); 41 d_vec_t lp_df(df_len);
36 42
37 //equivalent in matlab to [b,a] = butter(2,0.4); 43 //equivalent in matlab to [b,a] = butter(2,0.4);
38 a[0] = 1.0000; 44 a[0] = 1.0000;
39 a[1] = -0.3695; 45 a[1] = -0.3695;
40 a[2] = 0.1958; 46 a[2] = 0.1958;
47 double out1 = 0.; 53 double out1 = 0.;
48 double out2 = 0.; 54 double out2 = 0.;
49 55
50 56
51 // forwards filtering 57 // forwards filtering
52 for (unsigned int i = 0;i < df.size();i++) 58 for (int i = 0; i < df_len; i++) {
53 {
54 lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2; 59 lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2;
55 inp2 = inp1; 60 inp2 = inp1;
56 inp1 = df[i]; 61 inp1 = df[i];
57 out2 = out1; 62 out2 = out1;
58 out1 = lp_df[i]; 63 out1 = lp_df[i];
59 } 64 }
60 65
61 // copy forwards filtering to df... 66 // copy forwards filtering to df...
62 // but, time-reversed, ready for backwards filtering 67 // but, time-reversed, ready for backwards filtering
63 for (unsigned int i = 0;i < df.size();i++) 68 for (int i = 0; i < df_len; i++) {
64 { 69 df[i] = lp_df[df_len - i - 1];
65 df[i] = lp_df[df.size()-i-1]; 70 }
66 } 71
67 72 for (int i = 0; i < df_len; i++) {
68 for (unsigned int i = 0;i < df.size();i++)
69 {
70 lp_df[i] = 0.; 73 lp_df[i] = 0.;
71 } 74 }
72 75
73 inp1 = 0.; inp2 = 0.; 76 inp1 = 0.; inp2 = 0.;
74 out1 = 0.; out2 = 0.; 77 out1 = 0.; out2 = 0.;
75 78
76 // backwards filetering on time-reversed df 79 // backwards filetering on time-reversed df
77 for (unsigned int i = 0;i < df.size();i++) 80 for (int i = 0; i < df_len; i++) {
78 {
79 lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2; 81 lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2;
80 inp2 = inp1; 82 inp2 = inp1;
81 inp1 = df[i]; 83 inp1 = df[i];
82 out2 = out1; 84 out2 = out1;
83 out1 = lp_df[i]; 85 out1 = lp_df[i];
84 } 86 }
85 87
86 // write the re-reversed (i.e. forward) version back to df 88 // write the re-reversed (i.e. forward) version back to df
87 for (unsigned int i = 0;i < df.size();i++) 89 for (int i = 0; i < df_len; i++) {
88 { 90 df[i] = lp_df[df_len - i - 1];
89 df[i] = lp_df[df.size()-i-1];
90 } 91 }
91 } 92 }
92 93
93 94
94 // MEPD 28/11/12 95 // MEPD 28/11/12
107 // calculate the acf, 108 // calculate the acf,
108 // then the rcf.. and then stick the rcfs as columns of a matrix 109 // then the rcf.. and then stick the rcfs as columns of a matrix
109 // then call viterbi decoding with weight vector and transition matrix 110 // then call viterbi decoding with weight vector and transition matrix
110 // and get best path 111 // and get best path
111 112
112 unsigned int wv_len = 128; 113 int wv_len = 128;
113 114
114 // MEPD 28/11/12 115 // MEPD 28/11/12
115 // the default value of inputtempo in the beat tracking plugin is 120 116 // the default value of inputtempo in the beat tracking plugin is 120
116 // so if the user specifies a different inputtempo, the rayparam will be updated 117 // so if the user specifies a different inputtempo, the rayparam will be updated
117 // accordingly. 118 // accordingly.
118 // note: 60*44100/512 is a magic number 119 // note: 60*44100/512 is a magic number
119 // this might (will?) break if a user specifies a different frame rate for the onset detection function 120 // this might (will?) break if a user specifies a different frame rate for the onset detection function
120 double rayparam = (60*44100/512)/inputtempo; 121 double rayparam = (60*44100/512)/inputtempo;
121 122
122 // these debug statements can be removed.
123 // std::cerr << "inputtempo" << inputtempo << std::endl;
124 // std::cerr << "rayparam" << rayparam << std::endl;
125 // std::cerr << "constraintempo" << constraintempo << std::endl;
126
127 // make rayleigh weighting curve 123 // make rayleigh weighting curve
128 d_vec_t wv(wv_len); 124 d_vec_t wv(wv_len);
129 125
130 // check whether or not to use rayleigh weighting (if constraintempo is false) 126 // check whether or not to use rayleigh weighting (if constraintempo is false)
131 // or use gaussian weighting it (constraintempo is true) 127 // or use gaussian weighting it (constraintempo is true)
132 if (constraintempo) 128 if (constraintempo) {
133 { 129 for (int i = 0; i < wv_len; i++) {
134 for (unsigned int i=0; i<wv.size(); i++)
135 {
136 // MEPD 28/11/12 130 // MEPD 28/11/12
137 // do a gaussian weighting instead of rayleigh 131 // do a gaussian weighting instead of rayleigh
138 wv[i] = exp( (-1.*pow((static_cast<double> (i)-rayparam),2.)) / (2.*pow(rayparam/4.,2.)) ); 132 wv[i] = exp( (-1.*pow((double(i)-rayparam),2.)) / (2.*pow(rayparam/4.,2.)) );
139 } 133 }
140 } 134 } else {
141 else 135 for (int i = 0; i < wv_len; i++) {
142 {
143 for (unsigned int i=0; i<wv.size(); i++)
144 {
145 // MEPD 28/11/12 136 // MEPD 28/11/12
146 // standard rayleigh weighting over periodicities 137 // standard rayleigh weighting over periodicities
147 wv[i] = (static_cast<double> (i) / pow(rayparam,2.)) * exp((-1.*pow(-static_cast<double> (i),2.)) / (2.*pow(rayparam,2.))); 138 wv[i] = (double(i) / pow(rayparam,2.)) * exp((-1.*pow(-double(i),2.)) / (2.*pow(rayparam,2.)));
148 } 139 }
149 } 140 }
150 141
151 // beat tracking frame size (roughly 6 seconds) and hop (1.5 seconds) 142 // beat tracking frame size (roughly 6 seconds) and hop (1.5 seconds)
152 unsigned int winlen = 512; 143 int winlen = 512;
153 unsigned int step = 128; 144 int step = 128;
154 145
155 // matrix to store output of comb filter bank, increment column of matrix at each frame 146 // matrix to store output of comb filter bank, increment column of matrix at each frame
156 d_mat_t rcfmat; 147 d_mat_t rcfmat;
157 int col_counter = -1; 148 int col_counter = -1;
149 int df_len = int(df.size());
158 150
159 // main loop for beat period calculation 151 // main loop for beat period calculation
160 for (unsigned int i=0; i+winlen<df.size(); i+=step) 152 for (int i = 0; i+winlen < df_len; i+=step) {
161 { 153
162 // get dfframe 154 // get dfframe
163 d_vec_t dfframe(winlen); 155 d_vec_t dfframe(winlen);
164 for (unsigned int k=0; k<winlen; k++) 156 for (int k=0; k < winlen; k++) {
165 {
166 dfframe[k] = df[i+k]; 157 dfframe[k] = df[i+k];
167 } 158 }
168 // get rcf vector for current frame 159 // get rcf vector for current frame
169 d_vec_t rcf(wv_len); 160 d_vec_t rcf(wv_len);
170 get_rcf(dfframe,wv,rcf); 161 get_rcf(dfframe,wv,rcf);
171 162
172 rcfmat.push_back( d_vec_t() ); // adds a new column 163 rcfmat.push_back( d_vec_t() ); // adds a new column
173 col_counter++; 164 col_counter++;
174 for (unsigned int j=0; j<rcf.size(); j++) 165 for (int j = 0; j < wv_len; j++) {
175 {
176 rcfmat[col_counter].push_back( rcf[j] ); 166 rcfmat[col_counter].push_back( rcf[j] );
177 } 167 }
178 } 168 }
179 169
180 // now call viterbi decoding function 170 // now call viterbi decoding function
193 183
194 d_vec_t dfframe(dfframe_in); 184 d_vec_t dfframe(dfframe_in);
195 185
196 MathUtilities::adaptiveThreshold(dfframe); 186 MathUtilities::adaptiveThreshold(dfframe);
197 187
198 d_vec_t acf(dfframe.size()); 188 int dfframe_len = int(dfframe.size());
199 189 int rcf_len = int(rcf.size());
200 190
201 for (unsigned int lag=0; lag<dfframe.size(); lag++) 191 d_vec_t acf(dfframe_len);
202 { 192
193 for (int lag = 0; lag < dfframe_len; lag++) {
203 double sum = 0.; 194 double sum = 0.;
204 double tmp = 0.; 195 double tmp = 0.;
205 196
206 for (unsigned int n=0; n<(dfframe.size()-lag); n++) 197 for (int n = 0; n < (dfframe_len - lag); n++) {
207 { 198 tmp = dfframe[n] * dfframe[n + lag];
208 tmp = dfframe[n] * dfframe[n+lag];
209 sum += tmp; 199 sum += tmp;
210 } 200 }
211 acf[lag] = static_cast<double> (sum/ (dfframe.size()-lag)); 201 acf[lag] = double(sum/ (dfframe_len - lag));
212 } 202 }
213 203
214 // now apply comb filtering 204 // now apply comb filtering
215 int numelem = 4; 205 int numelem = 4;
216 206
217 for (unsigned int i = 2;i < rcf.size();i++) // max beat period 207 for (int i = 2; i < rcf_len; i++) { // max beat period
218 { 208 for (int a = 1; a <= numelem; a++) { // number of comb elements
219 for (int a = 1;a <= numelem;a++) // number of comb elements 209 for (int b = 1-a; b <= a-1; b++) { // general state using normalisation of comb elements
220 { 210 rcf[i-1] += ( acf[(a*i+b)-1]*wv[i-1] ) / (2.*a-1.); // calculate value for comb filter row
221 for (int b = 1-a;b <= a-1;b++) // general state using normalisation of comb elements
222 {
223 rcf[i-1] += ( acf[(a*i+b)-1]*wv[i-1] ) / (2.*a-1.); // calculate value for comb filter row
224 } 211 }
225 } 212 }
226 } 213 }
227 214
228 // apply adaptive threshold to rcf 215 // apply adaptive threshold to rcf
229 MathUtilities::adaptiveThreshold(rcf); 216 MathUtilities::adaptiveThreshold(rcf);
230 217
231 double rcfsum =0.; 218 double rcfsum =0.;
232 for (unsigned int i=0; i<rcf.size(); i++) 219 for (int i = 0; i < rcf_len; i++) {
233 {
234 rcf[i] += EPS ; 220 rcf[i] += EPS ;
235 rcfsum += rcf[i]; 221 rcfsum += rcf[i];
236 } 222 }
237 223
238 // normalise rcf to sum to unity 224 // normalise rcf to sum to unity
239 for (unsigned int i=0; i<rcf.size(); i++) 225 for (int i = 0; i < rcf_len; i++) {
240 {
241 rcf[i] /= (rcfsum + EPS); 226 rcf[i] /= (rcfsum + EPS);
242 } 227 }
243 } 228 }
244 229
245 void 230 void
246 TempoTrackV2::viterbi_decode(const d_mat_t &rcfmat, const d_vec_t &wv, d_vec_t &beat_period, d_vec_t &tempi) 231 TempoTrackV2::viterbi_decode(const d_mat_t &rcfmat, const d_vec_t &wv, d_vec_t &beat_period, d_vec_t &tempi)
247 { 232 {
248 // following Kevin Murphy's Viterbi decoding to get best path of 233 // following Kevin Murphy's Viterbi decoding to get best path of
249 // beat periods through rfcmat 234 // beat periods through rfcmat
250 235
236 int wv_len = int(wv.size());
237
251 // make transition matrix 238 // make transition matrix
252 d_mat_t tmat; 239 d_mat_t tmat;
253 for (unsigned int i=0;i<wv.size();i++) 240 for (int i = 0; i < wv_len; i++) {
254 {
255 tmat.push_back ( d_vec_t() ); // adds a new column 241 tmat.push_back ( d_vec_t() ); // adds a new column
256 for (unsigned int j=0; j<wv.size(); j++) 242 for (int j = 0; j < wv_len; j++) {
257 {
258 tmat[i].push_back(0.); // fill with zeros initially 243 tmat[i].push_back(0.); // fill with zeros initially
259 } 244 }
260 } 245 }
261 246
262 // variance of Gaussians in transition matrix 247 // variance of Gaussians in transition matrix
263 // formed of Gaussians on diagonal - implies slow tempo change 248 // formed of Gaussians on diagonal - implies slow tempo change
264 double sigma = 8.; 249 double sigma = 8.;
265 // don't want really short beat periods, or really long ones 250 // don't want really short beat periods, or really long ones
266 for (unsigned int i=20;i <wv.size()-20; i++) 251 for (int i = 20; i < wv_len - 20; i++) {
267 { 252 for (int j = 20; j < wv_len - 20; j++) {
268 for (unsigned int j=20; j<wv.size()-20; j++) 253 double mu = double(i);
269 {
270 double mu = static_cast<double>(i);
271 tmat[i][j] = exp( (-1.*pow((j-mu),2.)) / (2.*pow(sigma,2.)) ); 254 tmat[i][j] = exp( (-1.*pow((j-mu),2.)) / (2.*pow(sigma,2.)) );
272 } 255 }
273 } 256 }
274 257
275 // parameters for Viterbi decoding... this part is taken from 258 // parameters for Viterbi decoding... this part is taken from
276 // Murphy's matlab 259 // Murphy's matlab
277 260
278 d_mat_t delta; 261 d_mat_t delta;
279 i_mat_t psi; 262 i_mat_t psi;
280 for (unsigned int i=0;i <rcfmat.size(); i++) 263 for (int i = 0; i < int(rcfmat.size()); i++) {
281 { 264 delta.push_back(d_vec_t());
282 delta.push_back( d_vec_t()); 265 psi.push_back(i_vec_t());
283 psi.push_back( i_vec_t()); 266 for (int j = 0; j < int(rcfmat[i].size()); j++) {
284 for (unsigned int j=0; j<rcfmat[i].size(); j++)
285 {
286 delta[i].push_back(0.); // fill with zeros initially 267 delta[i].push_back(0.); // fill with zeros initially
287 psi[i].push_back(0); // fill with zeros initially 268 psi[i].push_back(0); // fill with zeros initially
288 } 269 }
289 } 270 }
290 271
291 272 int T = int(delta.size());
292 unsigned int T = delta.size();
293 273
294 if (T < 2) return; // can't do anything at all meaningful 274 if (T < 2) return; // can't do anything at all meaningful
295 275
296 unsigned int Q = delta[0].size(); 276 int Q = int(delta[0].size());
297 277
298 // initialize first column of delta 278 // initialize first column of delta
299 for (unsigned int j=0; j<Q; j++) 279 for (int j = 0; j < Q; j++) {
300 {
301 delta[0][j] = wv[j] * rcfmat[0][j]; 280 delta[0][j] = wv[j] * rcfmat[0][j];
302 psi[0][j] = 0; 281 psi[0][j] = 0;
303 } 282 }
304 283
305 double deltasum = 0.; 284 double deltasum = 0.;
306 for (unsigned int i=0; i<Q; i++) 285 for (int i = 0; i < Q; i++) {
307 {
308 deltasum += delta[0][i]; 286 deltasum += delta[0][i];
309 } 287 }
310 for (unsigned int i=0; i<Q; i++) 288 for (int i = 0; i < Q; i++) {
311 {
312 delta[0][i] /= (deltasum + EPS); 289 delta[0][i] /= (deltasum + EPS);
313 } 290 }
314 291
315 292 for (int t=1; t < T; t++)
316 for (unsigned int t=1; t<T; t++)
317 { 293 {
318 d_vec_t tmp_vec(Q); 294 d_vec_t tmp_vec(Q);
319 295
320 for (unsigned int j=0; j<Q; j++) 296 for (int j = 0; j < Q; j++) {
321 { 297 for (int i = 0; i < Q; i++) {
322 for (unsigned int i=0; i<Q; i++)
323 {
324 tmp_vec[i] = delta[t-1][i] * tmat[j][i]; 298 tmp_vec[i] = delta[t-1][i] * tmat[j][i];
325 } 299 }
326 300
327 delta[t][j] = get_max_val(tmp_vec); 301 delta[t][j] = get_max_val(tmp_vec);
328 302
331 delta[t][j] *= rcfmat[t][j]; 305 delta[t][j] *= rcfmat[t][j];
332 } 306 }
333 307
334 // normalise current delta column 308 // normalise current delta column
335 double deltasum = 0.; 309 double deltasum = 0.;
336 for (unsigned int i=0; i<Q; i++) 310 for (int i = 0; i < Q; i++) {
337 {
338 deltasum += delta[t][i]; 311 deltasum += delta[t][i];
339 } 312 }
340 for (unsigned int i=0; i<Q; i++) 313 for (int i = 0; i < Q; i++) {
341 {
342 delta[t][i] /= (deltasum + EPS); 314 delta[t][i] /= (deltasum + EPS);
343 } 315 }
344 } 316 }
345 317
346 i_vec_t bestpath(T); 318 i_vec_t bestpath(T);
347 d_vec_t tmp_vec(Q); 319 d_vec_t tmp_vec(Q);
348 for (unsigned int i=0; i<Q; i++) 320 for (int i = 0; i < Q; i++) {
349 {
350 tmp_vec[i] = delta[T-1][i]; 321 tmp_vec[i] = delta[T-1][i];
351 } 322 }
352 323
353 // find starting point - best beat period for "last" frame 324 // find starting point - best beat period for "last" frame
354 bestpath[T-1] = get_max_ind(tmp_vec); 325 bestpath[T-1] = get_max_ind(tmp_vec);
355 326
356 // backtrace through index of maximum values in psi 327 // backtrace through index of maximum values in psi
357 for (unsigned int t=T-2; t>0 ;t--) 328 for (int t=T-2; t>0 ;t--) {
358 {
359 bestpath[t] = psi[t+1][bestpath[t+1]]; 329 bestpath[t] = psi[t+1][bestpath[t+1]];
360 } 330 }
361 331
362 // weird but necessary hack -- couldn't get above loop to terminate at t >= 0 332 // weird but necessary hack -- couldn't get above loop to terminate at t >= 0
363 bestpath[0] = psi[1][bestpath[1]]; 333 bestpath[0] = psi[1][bestpath[1]];
364 334
365 unsigned int lastind = 0; 335 int lastind = 0;
366 for (unsigned int i=0; i<T; i++) 336 for (int i = 0; i < T; i++) {
367 { 337 int step = 128;
368 unsigned int step = 128; 338 for (int j = 0; j < step; j++) {
369 for (unsigned int j=0; j<step; j++)
370 {
371 lastind = i*step+j; 339 lastind = i*step+j;
372 beat_period[lastind] = bestpath[i]; 340 beat_period[lastind] = bestpath[i];
373 } 341 }
374 // std::cerr << "bestpath[" << i << "] = " << bestpath[i] << " (used for beat_periods " << i*step << " to " << i*step+step-1 << ")" << std::endl; 342 // std::cerr << "bestpath[" << i << "] = " << bestpath[i] << " (used for beat_periods " << i*step << " to " << i*step+step-1 << ")" << std::endl;
375 } 343 }
376 344
377 //fill in the last values... 345 // fill in the last values...
378 for (unsigned int i=lastind; i<beat_period.size(); i++) 346 for (int i = lastind; i < int(beat_period.size()); i++) {
379 {
380 beat_period[i] = beat_period[lastind]; 347 beat_period[i] = beat_period[lastind];
381 } 348 }
382 349
383 for (unsigned int i = 0; i < beat_period.size(); i++) 350 for (int i = 0; i < int(beat_period.size()); i++) {
384 {
385 tempi.push_back((60. * m_rate / m_increment)/beat_period[i]); 351 tempi.push_back((60. * m_rate / m_increment)/beat_period[i]);
386 } 352 }
387 } 353 }
388 354
389 double 355 double
390 TempoTrackV2::get_max_val(const d_vec_t &df) 356 TempoTrackV2::get_max_val(const d_vec_t &df)
391 { 357 {
392 double maxval = 0.; 358 double maxval = 0.;
393 for (unsigned int i=0; i<df.size(); i++) 359 int df_len = int(df.size());
394 { 360
395 if (maxval < df[i]) 361 for (int i = 0; i < df_len; i++) {
396 { 362 if (maxval < df[i]) {
397 maxval = df[i]; 363 maxval = df[i];
398 } 364 }
399 } 365 }
400 366
401 return maxval; 367 return maxval;
404 int 370 int
405 TempoTrackV2::get_max_ind(const d_vec_t &df) 371 TempoTrackV2::get_max_ind(const d_vec_t &df)
406 { 372 {
407 double maxval = 0.; 373 double maxval = 0.;
408 int ind = 0; 374 int ind = 0;
409 for (unsigned int i=0; i<df.size(); i++) 375 int df_len = int(df.size());
410 { 376
411 if (maxval < df[i]) 377 for (int i = 0; i < df_len; i++) {
412 { 378 if (maxval < df[i]) {
413 maxval = df[i]; 379 maxval = df[i];
414 ind = i; 380 ind = i;
415 } 381 }
416 } 382 }
417 383
420 386
421 void 387 void
422 TempoTrackV2::normalise_vec(d_vec_t &df) 388 TempoTrackV2::normalise_vec(d_vec_t &df)
423 { 389 {
424 double sum = 0.; 390 double sum = 0.;
425 for (unsigned int i=0; i<df.size(); i++) 391 int df_len = int(df.size());
426 { 392
393 for (int i = 0; i < df_len; i++) {
427 sum += df[i]; 394 sum += df[i];
428 } 395 }
429 396
430 for (unsigned int i=0; i<df.size(); i++) 397 for (int i = 0; i < df_len; i++) {
431 {
432 df[i]/= (sum + EPS); 398 df[i]/= (sum + EPS);
433 } 399 }
434 } 400 }
435 401
436 // MEPD 28/11/12 402 // MEPD 28/11/12
442 const vector<double> &beat_period, 408 const vector<double> &beat_period,
443 vector<double> &beats, double alpha, double tightness) 409 vector<double> &beats, double alpha, double tightness)
444 { 410 {
445 if (df.empty() || beat_period.empty()) return; 411 if (df.empty() || beat_period.empty()) return;
446 412
447 d_vec_t cumscore(df.size()); // store cumulative score 413 int df_len = int(df.size());
448 i_vec_t backlink(df.size()); // backlink (stores best beat locations at each time instant) 414
449 d_vec_t localscore(df.size()); // localscore, for now this is the same as the detection function 415 d_vec_t cumscore(df_len); // store cumulative score
450 416 i_vec_t backlink(df_len); // backlink (stores best beat locations at each time instant)
451 for (unsigned int i=0; i<df.size(); i++) 417 d_vec_t localscore(df_len); // localscore, for now this is the same as the detection function
452 { 418
419 for (int i = 0; i < df_len; i++) {
453 localscore[i] = df[i]; 420 localscore[i] = df[i];
454 backlink[i] = -1; 421 backlink[i] = -1;
455 } 422 }
456 423
457 //double tightness = 4.; 424 //double tightness = 4.;
460 // debug statements that can be removed. 427 // debug statements that can be removed.
461 // std::cerr << "alpha" << alpha << std::endl; 428 // std::cerr << "alpha" << alpha << std::endl;
462 // std::cerr << "tightness" << tightness << std::endl; 429 // std::cerr << "tightness" << tightness << std::endl;
463 430
464 // main loop 431 // main loop
465 for (unsigned int i=0; i<localscore.size(); i++) 432 for (int i = 0; i < df_len; i++) {
466 { 433
467 int prange_min = -2*beat_period[i]; 434 int prange_min = -2*beat_period[i];
468 int prange_max = round(-0.5*beat_period[i]); 435 int prange_max = round(-0.5*beat_period[i]);
469 436
470 // transition range 437 // transition range
471 d_vec_t txwt (prange_max - prange_min + 1); 438 int txwt_len = prange_max - prange_min + 1;
472 d_vec_t scorecands (txwt.size()); 439 d_vec_t txwt (txwt_len);
473 440 d_vec_t scorecands (txwt_len);
474 for (unsigned int j=0;j<txwt.size();j++) 441
475 { 442 for (int j = 0; j < txwt_len; j++) {
476 double mu = static_cast<double> (beat_period[i]); 443
444 double mu = double(beat_period[i]);
477 txwt[j] = exp( -0.5*pow(tightness * log((round(2*mu)-j)/mu),2)); 445 txwt[j] = exp( -0.5*pow(tightness * log((round(2*mu)-j)/mu),2));
478 446
479 // IF IN THE ALLOWED RANGE, THEN LOOK AT CUMSCORE[I+PRANGE_MIN+J 447 // IF IN THE ALLOWED RANGE, THEN LOOK AT CUMSCORE[I+PRANGE_MIN+J
480 // ELSE LEAVE AT DEFAULT VALUE FROM INITIALISATION: D_VEC_T SCORECANDS (TXWT.SIZE()); 448 // ELSE LEAVE AT DEFAULT VALUE FROM INITIALISATION: D_VEC_T SCORECANDS (TXWT.SIZE());
481 449
482 int cscore_ind = i+prange_min+j; 450 int cscore_ind = i + prange_min + j;
483 if (cscore_ind >= 0) 451 if (cscore_ind >= 0) {
484 {
485 scorecands[j] = txwt[j] * cumscore[cscore_ind]; 452 scorecands[j] = txwt[j] * cumscore[cscore_ind];
486 } 453 }
487 } 454 }
488 455
489 // find max value and index of maximum value 456 // find max value and index of maximum value
496 // std::cerr << "backlink[" << i << "] <= " << backlink[i] << std::endl; 463 // std::cerr << "backlink[" << i << "] <= " << backlink[i] << std::endl;
497 } 464 }
498 465
499 // STARTING POINT, I.E. LAST BEAT.. PICK A STRONG POINT IN cumscore VECTOR 466 // STARTING POINT, I.E. LAST BEAT.. PICK A STRONG POINT IN cumscore VECTOR
500 d_vec_t tmp_vec; 467 d_vec_t tmp_vec;
501 for (unsigned int i=cumscore.size() - beat_period[beat_period.size()-1] ; i<cumscore.size(); i++) 468 for (int i = df_len - beat_period[beat_period.size()-1] ; i < df_len; i++) {
502 {
503 tmp_vec.push_back(cumscore[i]); 469 tmp_vec.push_back(cumscore[i]);
504 } 470 }
505 471
506 int startpoint = get_max_ind(tmp_vec) + cumscore.size() - beat_period[beat_period.size()-1] ; 472 int startpoint = get_max_ind(tmp_vec) +
473 df_len - beat_period[beat_period.size()-1] ;
507 474
508 // can happen if no results obtained earlier (e.g. input too short) 475 // can happen if no results obtained earlier (e.g. input too short)
509 if (startpoint >= (int)backlink.size()) startpoint = backlink.size()-1; 476 if (startpoint >= int(backlink.size())) {
477 startpoint = int(backlink.size()) - 1;
478 }
510 479
511 // USE BACKLINK TO GET EACH NEW BEAT (TOWARDS THE BEGINNING OF THE FILE) 480 // USE BACKLINK TO GET EACH NEW BEAT (TOWARDS THE BEGINNING OF THE FILE)
512 // BACKTRACKING FROM THE END TO THE BEGINNING.. MAKING SURE NOT TO GO BEFORE SAMPLE 0 481 // BACKTRACKING FROM THE END TO THE BEGINNING.. MAKING SURE NOT TO GO BEFORE SAMPLE 0
513 i_vec_t ibeats; 482 i_vec_t ibeats;
514 ibeats.push_back(startpoint); 483 ibeats.push_back(startpoint);
515 // std::cerr << "startpoint = " << startpoint << std::endl; 484 // std::cerr << "startpoint = " << startpoint << std::endl;
516 while (backlink[ibeats.back()] > 0) 485 while (backlink[ibeats.back()] > 0) {
517 {
518 // std::cerr << "backlink[" << ibeats.back() << "] = " << backlink[ibeats.back()] << std::endl; 486 // std::cerr << "backlink[" << ibeats.back() << "] = " << backlink[ibeats.back()] << std::endl;
519 int b = ibeats.back(); 487 int b = ibeats.back();
520 if (backlink[b] == b) break; // shouldn't happen... haha 488 if (backlink[b] == b) break; // shouldn't happen... haha
521 ibeats.push_back(backlink[b]); 489 ibeats.push_back(backlink[b]);
522 } 490 }
523 491
524 // REVERSE SEQUENCE OF IBEATS AND STORE AS BEATS 492 // REVERSE SEQUENCE OF IBEATS AND STORE AS BEATS
525 for (unsigned int i=0; i<ibeats.size(); i++) 493 for (int i = 0; i < int(ibeats.size()); i++) {
526 { 494 beats.push_back(double(ibeats[ibeats.size() - i - 1]));
527 beats.push_back( static_cast<double>(ibeats[ibeats.size()-i-1]) ); 495 }
528 } 496 }
529 } 497
530 498
531