annotate dsp/tempotracking/TempoTrackV2.cpp @ 493:bb78ca3fe7de

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