annotate dsp/tempotracking/TempoTrackV2.cpp @ 515:08bcc06c38ec tip master

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