annotate dsp/tempotracking/TempoTrack.cpp @ 410:c5e1b25d5177

Fix some uninitialised values
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 07 Sep 2015 14:00:30 +0100
parents d5014ab8b0e5
children e4a57215ddee
rev   line source
c@410 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@410 2
c@410 3 /*
c@410 4 QM DSP Library
c@410 5
c@410 6 Centre for Digital Music, Queen Mary, University of London.
c@410 7 This file copyright 2005-2006 Christian Landone.and Matthew Davies.
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@410 13 COPYING included with this distribution for more information.
c@410 14 */
c@410 15
c@410 16 #include "TempoTrack.h"
c@410 17
c@410 18 #include "maths/MathAliases.h"
c@410 19 #include "maths/MathUtilities.h"
c@410 20
c@410 21 #include <iostream>
c@410 22
c@410 23 #include <cassert>
c@410 24
c@410 25 //#define DEBUG_TEMPO_TRACK 1
c@410 26
c@410 27
c@410 28 #define RAY43VAL
c@410 29
c@410 30 //////////////////////////////////////////////////////////////////////
c@410 31 // Construction/Destruction
c@410 32 //////////////////////////////////////////////////////////////////////
c@410 33
c@410 34 TempoTrack::TempoTrack( TTParams Params )
c@410 35 {
c@410 36 m_tempoScratch = NULL;
c@410 37 m_rawDFFrame = NULL;
c@410 38 m_smoothDFFrame = NULL;
c@410 39 m_frameACF = NULL;
c@410 40 m_smoothRCF = NULL;
c@410 41
c@410 42 m_dataLength = 0;
c@410 43 m_winLength = 0;
c@410 44 m_lagLength = 0;
c@410 45
c@410 46 m_rayparam = 0;
c@410 47 m_sigma = 0;
c@410 48 m_DFWVNnorm = 0;
c@410 49
c@410 50 initialise( Params );
c@410 51 }
c@410 52
c@410 53 TempoTrack::~TempoTrack()
c@410 54 {
c@410 55 deInitialise();
c@410 56 }
c@410 57
c@410 58 void TempoTrack::initialise( TTParams Params )
c@410 59 {
c@410 60 m_winLength = Params.winLength;
c@410 61 m_lagLength = Params.lagLength;
c@410 62
c@410 63 m_rayparam = 43.0;
c@410 64 m_sigma = sqrt(3.9017);
c@410 65 m_DFWVNnorm = exp( ( log( 2.0 ) / m_rayparam ) * ( m_winLength + 2 ) );
c@410 66
c@410 67 m_rawDFFrame = new double[ m_winLength ];
c@410 68 m_smoothDFFrame = new double[ m_winLength ];
c@410 69 m_frameACF = new double[ m_winLength ];
c@410 70 m_tempoScratch = new double[ m_lagLength ];
c@410 71 m_smoothRCF = new double[ m_lagLength ];
c@410 72
c@410 73
c@410 74 unsigned int winPre = Params.WinT.pre;
c@410 75 unsigned int winPost = Params.WinT.post;
c@410 76
c@410 77 m_DFFramer.configure( m_winLength, m_lagLength );
c@410 78
c@410 79 m_DFPParams.length = m_winLength;
c@410 80 m_DFPParams.AlphaNormParam = Params.alpha;
c@410 81 m_DFPParams.LPOrd = Params.LPOrd;
c@410 82 m_DFPParams.LPACoeffs = Params.LPACoeffs;
c@410 83 m_DFPParams.LPBCoeffs = Params.LPBCoeffs;
c@410 84 m_DFPParams.winPre = Params.WinT.pre;
c@410 85 m_DFPParams.winPost = Params.WinT.post;
c@410 86 m_DFPParams.isMedianPositive = true;
c@410 87
c@410 88 m_DFConditioning = new DFProcess( m_DFPParams );
c@410 89
c@410 90
c@410 91 // these are parameters for smoothing m_tempoScratch
c@410 92 m_RCFPParams.length = m_lagLength;
c@410 93 m_RCFPParams.AlphaNormParam = Params.alpha;
c@410 94 m_RCFPParams.LPOrd = Params.LPOrd;
c@410 95 m_RCFPParams.LPACoeffs = Params.LPACoeffs;
c@410 96 m_RCFPParams.LPBCoeffs = Params.LPBCoeffs;
c@410 97 m_RCFPParams.winPre = Params.WinT.pre;
c@410 98 m_RCFPParams.winPost = Params.WinT.post;
c@410 99 m_RCFPParams.isMedianPositive = true;
c@410 100
c@410 101 m_RCFConditioning = new DFProcess( m_RCFPParams );
c@410 102
c@410 103 }
c@410 104
c@410 105 void TempoTrack::deInitialise()
c@410 106 {
c@410 107 delete [] m_rawDFFrame;
c@410 108
c@410 109 delete [] m_smoothDFFrame;
c@410 110
c@410 111 delete [] m_smoothRCF;
c@410 112
c@410 113 delete [] m_frameACF;
c@410 114
c@410 115 delete [] m_tempoScratch;
c@410 116
c@410 117 delete m_DFConditioning;
c@410 118
c@410 119 delete m_RCFConditioning;
c@410 120
c@410 121 }
c@410 122
c@410 123 void TempoTrack::createCombFilter(double* Filter, unsigned int winLength, unsigned int TSig, double beatLag)
c@410 124 {
c@410 125 unsigned int i;
c@410 126
c@410 127 if( beatLag == 0 )
c@410 128 {
c@410 129 for( i = 0; i < winLength; i++ )
c@410 130 {
c@410 131 Filter[ i ] = ( ( i + 1 ) / pow( m_rayparam, 2.0) ) * exp( ( -pow(( i + 1 ),2.0 ) / ( 2.0 * pow( m_rayparam, 2.0))));
c@410 132 }
c@410 133 }
c@410 134 else
c@410 135 {
c@410 136 m_sigma = beatLag/4;
c@410 137 for( i = 0; i < winLength; i++ )
c@410 138 {
c@410 139 double dlag = (double)(i+1) - beatLag;
c@410 140 Filter[ i ] = exp(-0.5 * pow(( dlag / m_sigma), 2.0) ) / (sqrt( 2 * PI) * m_sigma);
c@410 141 }
c@410 142 }
c@410 143 }
c@410 144
c@410 145 double TempoTrack::tempoMM(double* ACF, double* weight, int tsig)
c@410 146 {
c@410 147
c@410 148 double period = 0;
c@410 149 double maxValRCF = 0.0;
c@410 150 unsigned int maxIndexRCF = 0;
c@410 151
c@410 152 double* pdPeaks;
c@410 153
c@410 154 unsigned int maxIndexTemp;
c@410 155 double maxValTemp;
c@410 156 unsigned int count;
c@410 157
c@410 158 unsigned int numelem,i,j;
c@410 159 int a, b;
c@410 160
c@410 161 for( i = 0; i < m_lagLength; i++ )
c@410 162 m_tempoScratch[ i ] = 0.0;
c@410 163
c@410 164 if( tsig == 0 )
c@410 165 {
c@410 166 //if time sig is unknown, use metrically unbiased version of Filterbank
c@410 167 numelem = 4;
c@410 168 }
c@410 169 else
c@410 170 {
c@410 171 numelem = tsig;
c@410 172 }
c@410 173
c@410 174 #ifdef DEBUG_TEMPO_TRACK
c@410 175 std::cerr << "tempoMM: m_winLength = " << m_winLength << ", m_lagLength = " << m_lagLength << ", numelem = " << numelem << std::endl;
c@410 176 #endif
c@410 177
c@410 178 for(i=1;i<m_lagLength-1;i++)
c@410 179 {
c@410 180 //first and last output values are left intentionally as zero
c@410 181 for (a=1;a<=numelem;a++)
c@410 182 {
c@410 183 for(b=(1-a);b<a;b++)
c@410 184 {
c@410 185 if( tsig == 0 )
c@410 186 {
c@410 187 m_tempoScratch[i] += ACF[a*(i+1)+b-1] * (1.0 / (2.0 * (double)a-1)) * weight[i];
c@410 188 }
c@410 189 else
c@410 190 {
c@410 191 m_tempoScratch[i] += ACF[a*(i+1)+b-1] * 1 * weight[i];
c@410 192 }
c@410 193 }
c@410 194 }
c@410 195 }
c@410 196
c@410 197
c@410 198 //////////////////////////////////////////////////
c@410 199 // MODIFIED BEAT PERIOD EXTRACTION //////////////
c@410 200 /////////////////////////////////////////////////
c@410 201
c@410 202 // find smoothed version of RCF ( as applied to Detection Function)
c@410 203 m_RCFConditioning->process( m_tempoScratch, m_smoothRCF);
c@410 204
c@410 205 if (tsig != 0) // i.e. in context dependent state
c@410 206 {
c@410 207 // NOW FIND MAX INDEX OF ACFOUT
c@410 208 for( i = 0; i < m_lagLength; i++)
c@410 209 {
c@410 210 if( m_tempoScratch[ i ] > maxValRCF)
c@410 211 {
c@410 212 maxValRCF = m_tempoScratch[ i ];
c@410 213 maxIndexRCF = i;
c@410 214 }
c@410 215 }
c@410 216 }
c@410 217 else // using rayleigh weighting
c@410 218 {
c@410 219 vector <vector<double> > rcfMat;
c@410 220
c@410 221 double sumRcf = 0.;
c@410 222
c@410 223 double maxVal = 0.;
c@410 224 // now find the two values which minimise rcfMat
c@410 225 double minVal = 0.;
c@410 226 int p_i = 1; // periodicity for row i;
c@410 227 int p_j = 1; //periodicity for column j;
c@410 228
c@410 229
c@410 230 for ( i=0; i<m_lagLength; i++)
c@410 231 {
c@410 232 m_tempoScratch[i] =m_smoothRCF[i];
c@410 233 }
c@410 234
c@410 235 // normalise m_tempoScratch so that it sums to zero.
c@410 236 for ( i=0; i<m_lagLength; i++)
c@410 237 {
c@410 238 sumRcf += m_tempoScratch[i];
c@410 239 }
c@410 240
c@410 241 for( i=0; i<m_lagLength; i++)
c@410 242 {
c@410 243 m_tempoScratch[i] /= sumRcf;
c@410 244 }
c@410 245
c@410 246 // create a matrix to store m_tempoScratchValues modified by log2 ratio
c@410 247 for ( i=0; i<m_lagLength; i++)
c@410 248 {
c@410 249 rcfMat.push_back ( vector<double>() ); // adds a new row...
c@410 250 }
c@410 251
c@410 252 for (i=0; i<m_lagLength; i++)
c@410 253 {
c@410 254 for (j=0; j<m_lagLength; j++)
c@410 255 {
c@410 256 rcfMat[i].push_back (0.);
c@410 257 }
c@410 258 }
c@410 259
c@410 260 // the 'i' and 'j' indices deliberately start from '1' and not '0'
c@410 261 for ( i=1; i<m_lagLength; i++)
c@410 262 {
c@410 263 for (j=1; j<m_lagLength; j++)
c@410 264 {
c@410 265 double log2PeriodRatio = log( static_cast<double>(i)/static_cast<double>(j) ) / log(2.0);
c@410 266 rcfMat[i][j] = ( abs(1.0-abs(log2PeriodRatio)) );
c@410 267 rcfMat[i][j] += ( 0.01*( 1./(m_tempoScratch[i]+m_tempoScratch[j]) ) );
c@410 268 }
c@410 269 }
c@410 270
c@410 271 // set diagonal equal to maximum value in rcfMat
c@410 272 // we don't want to pick one strong middle peak - we need a combination of two peaks.
c@410 273
c@410 274 for ( i=1; i<m_lagLength; i++)
c@410 275 {
c@410 276 for (j=1; j<m_lagLength; j++)
c@410 277 {
c@410 278 if (rcfMat[i][j] > maxVal)
c@410 279 {
c@410 280 maxVal = rcfMat[i][j];
c@410 281 }
c@410 282 }
c@410 283 }
c@410 284
c@410 285 for ( i=1; i<m_lagLength; i++)
c@410 286 {
c@410 287 rcfMat[i][i] = maxVal;
c@410 288 }
c@410 289
c@410 290 // now find the row and column number which minimise rcfMat
c@410 291 minVal = maxVal;
c@410 292
c@410 293 for ( i=1; i<m_lagLength; i++)
c@410 294 {
c@410 295 for ( j=1; j<m_lagLength; j++)
c@410 296 {
c@410 297 if (rcfMat[i][j] < minVal)
c@410 298 {
c@410 299 minVal = rcfMat[i][j];
c@410 300 p_i = i;
c@410 301 p_j = j;
c@410 302 }
c@410 303 }
c@410 304 }
c@410 305
c@410 306
c@410 307 // initially choose p_j (arbitrary) - saves on an else statement
c@410 308 int beatPeriod = p_j;
c@410 309 if (m_tempoScratch[p_i] > m_tempoScratch[p_j])
c@410 310 {
c@410 311 beatPeriod = p_i;
c@410 312 }
c@410 313
c@410 314 // now write the output
c@410 315 maxIndexRCF = static_cast<int>(beatPeriod);
c@410 316 }
c@410 317
c@410 318
c@410 319 double locked = 5168.f / maxIndexRCF;
c@410 320 if (locked >= 30 && locked <= 180) {
c@410 321 m_lockedTempo = locked;
c@410 322 }
c@410 323
c@410 324 #ifdef DEBUG_TEMPO_TRACK
c@410 325 std::cerr << "tempoMM: locked tempo = " << m_lockedTempo << std::endl;
c@410 326 #endif
c@410 327
c@410 328 if( tsig == 0 )
c@410 329 tsig = 4;
c@410 330
c@410 331
c@410 332 #ifdef DEBUG_TEMPO_TRACK
c@410 333 std::cerr << "tempoMM: maxIndexRCF = " << maxIndexRCF << std::endl;
c@410 334 #endif
c@410 335
c@410 336 if( tsig == 4 )
c@410 337 {
c@410 338 #ifdef DEBUG_TEMPO_TRACK
c@410 339 std::cerr << "tsig == 4" << std::endl;
c@410 340 #endif
c@410 341
c@410 342 pdPeaks = new double[ 4 ];
c@410 343 for( i = 0; i < 4; i++ ){ pdPeaks[ i ] = 0.0;}
c@410 344
c@410 345 pdPeaks[ 0 ] = ( double )maxIndexRCF + 1;
c@410 346
c@410 347 maxIndexTemp = 0;
c@410 348 maxValTemp = 0.0;
c@410 349 count = 0;
c@410 350
c@410 351 for( i = (2 * maxIndexRCF + 1) - 1; i < (2 * maxIndexRCF + 1) + 2; i++ )
c@410 352 {
c@410 353 if( ACF[ i ] > maxValTemp )
c@410 354 {
c@410 355 maxValTemp = ACF[ i ];
c@410 356 maxIndexTemp = count;
c@410 357 }
c@410 358 count++;
c@410 359 }
c@410 360 pdPeaks[ 1 ] = (double)( maxIndexTemp + 1 + ( (2 * maxIndexRCF + 1 ) - 2 ) + 1 )/2;
c@410 361
c@410 362 maxIndexTemp = 0;
c@410 363 maxValTemp = 0.0;
c@410 364 count = 0;
c@410 365
c@410 366 for( i = (3 * maxIndexRCF + 2 ) - 2; i < (3 * maxIndexRCF + 2 ) + 3; i++ )
c@410 367 {
c@410 368 if( ACF[ i ] > maxValTemp )
c@410 369 {
c@410 370 maxValTemp = ACF[ i ];
c@410 371 maxIndexTemp = count;
c@410 372 }
c@410 373 count++;
c@410 374 }
c@410 375 pdPeaks[ 2 ] = (double)( maxIndexTemp + 1 + ( (3 * maxIndexRCF + 2) - 4 ) + 1 )/3;
c@410 376
c@410 377 maxIndexTemp = 0;
c@410 378 maxValTemp = 0.0;
c@410 379 count = 0;
c@410 380
c@410 381 for( i = ( 4 * maxIndexRCF + 3) - 3; i < ( 4 * maxIndexRCF + 3) + 4; i++ )
c@410 382 {
c@410 383 if( ACF[ i ] > maxValTemp )
c@410 384 {
c@410 385 maxValTemp = ACF[ i ];
c@410 386 maxIndexTemp = count;
c@410 387 }
c@410 388 count++;
c@410 389 }
c@410 390 pdPeaks[ 3 ] = (double)( maxIndexTemp + 1 + ( (4 * maxIndexRCF + 3) - 9 ) + 1 )/4 ;
c@410 391
c@410 392
c@410 393 period = MathUtilities::mean( pdPeaks, 4 );
c@410 394 }
c@410 395 else
c@410 396 {
c@410 397 #ifdef DEBUG_TEMPO_TRACK
c@410 398 std::cerr << "tsig != 4" << std::endl;
c@410 399 #endif
c@410 400
c@410 401 pdPeaks = new double[ 3 ];
c@410 402 for( i = 0; i < 3; i++ ){ pdPeaks[ i ] = 0.0;}
c@410 403
c@410 404 pdPeaks[ 0 ] = ( double )maxIndexRCF + 1;
c@410 405
c@410 406 maxIndexTemp = 0;
c@410 407 maxValTemp = 0.0;
c@410 408 count = 0;
c@410 409
c@410 410 for( i = (2 * maxIndexRCF + 1) - 1; i < (2 * maxIndexRCF + 1) + 2; i++ )
c@410 411 {
c@410 412 if( ACF[ i ] > maxValTemp )
c@410 413 {
c@410 414 maxValTemp = ACF[ i ];
c@410 415 maxIndexTemp = count;
c@410 416 }
c@410 417 count++;
c@410 418 }
c@410 419 pdPeaks[ 1 ] = (double)( maxIndexTemp + 1 + ( (2 * maxIndexRCF + 1 ) - 2 ) + 1 )/2;
c@410 420
c@410 421 maxIndexTemp = 0;
c@410 422 maxValTemp = 0.0;
c@410 423 count = 0;
c@410 424
c@410 425 for( i = (3 * maxIndexRCF + 2 ) - 2; i < (3 * maxIndexRCF + 2 ) + 3; i++ )
c@410 426 {
c@410 427 if( ACF[ i ] > maxValTemp )
c@410 428 {
c@410 429 maxValTemp = ACF[ i ];
c@410 430 maxIndexTemp = count;
c@410 431 }
c@410 432 count++;
c@410 433 }
c@410 434 pdPeaks[ 2 ] = (double)( maxIndexTemp + 1 + ( (3 * maxIndexRCF + 2) - 4 ) + 1 )/3;
c@410 435
c@410 436
c@410 437 period = MathUtilities::mean( pdPeaks, 3 );
c@410 438 }
c@410 439
c@410 440 delete [] pdPeaks;
c@410 441
c@410 442 return period;
c@410 443 }
c@410 444
c@410 445 void TempoTrack::stepDetect( double* periodP, double* periodG, int currentIdx, int* flag )
c@410 446 {
c@410 447 double stepthresh = 1 * 3.9017;
c@410 448
c@410 449 if( *flag )
c@410 450 {
c@410 451 if(abs(periodG[ currentIdx ] - periodP[ currentIdx ]) > stepthresh)
c@410 452 {
c@410 453 // do nuffin'
c@410 454 }
c@410 455 }
c@410 456 else
c@410 457 {
c@410 458 if(fabs(periodG[ currentIdx ]-periodP[ currentIdx ]) > stepthresh)
c@410 459 {
c@410 460 *flag = 3;
c@410 461 }
c@410 462 }
c@410 463 }
c@410 464
c@410 465 void TempoTrack::constDetect( double* periodP, int currentIdx, int* flag )
c@410 466 {
c@410 467 double constthresh = 2 * 3.9017;
c@410 468
c@410 469 if( fabs( 2 * periodP[ currentIdx ] - periodP[ currentIdx - 1] - periodP[ currentIdx - 2] ) < constthresh)
c@410 470 {
c@410 471 *flag = 1;
c@410 472 }
c@410 473 else
c@410 474 {
c@410 475 *flag = 0;
c@410 476 }
c@410 477 }
c@410 478
c@410 479 int TempoTrack::findMeter(double *ACF, unsigned int len, double period)
c@410 480 {
c@410 481 int i;
c@410 482 int p = (int)MathUtilities::round( period );
c@410 483 int tsig;
c@410 484
c@410 485 double Energy_3 = 0.0;
c@410 486 double Energy_4 = 0.0;
c@410 487
c@410 488 double temp3A = 0.0;
c@410 489 double temp3B = 0.0;
c@410 490 double temp4A = 0.0;
c@410 491 double temp4B = 0.0;
c@410 492
c@410 493 double* dbf = new double[ len ]; int t = 0;
c@410 494 for( unsigned int u = 0; u < len; u++ ){ dbf[ u ] = 0.0; }
c@410 495
c@410 496 if( (double)len < 6 * p + 2 )
c@410 497 {
c@410 498 for( i = ( 3 * p - 2 ); i < ( 3 * p + 2 ) + 1; i++ )
c@410 499 {
c@410 500 temp3A += ACF[ i ];
c@410 501 dbf[ t++ ] = ACF[ i ];
c@410 502 }
c@410 503
c@410 504 for( i = ( 4 * p - 2 ); i < ( 4 * p + 2 ) + 1; i++ )
c@410 505 {
c@410 506 temp4A += ACF[ i ];
c@410 507 }
c@410 508
c@410 509 Energy_3 = temp3A;
c@410 510 Energy_4 = temp4A;
c@410 511 }
c@410 512 else
c@410 513 {
c@410 514 for( i = ( 3 * p - 2 ); i < ( 3 * p + 2 ) + 1; i++ )
c@410 515 {
c@410 516 temp3A += ACF[ i ];
c@410 517 }
c@410 518
c@410 519 for( i = ( 4 * p - 2 ); i < ( 4 * p + 2 ) + 1; i++ )
c@410 520 {
c@410 521 temp4A += ACF[ i ];
c@410 522 }
c@410 523
c@410 524 for( i = ( 6 * p - 2 ); i < ( 6 * p + 2 ) + 1; i++ )
c@410 525 {
c@410 526 temp3B += ACF[ i ];
c@410 527 }
c@410 528
c@410 529 for( i = ( 2 * p - 2 ); i < ( 2 * p + 2 ) + 1; i++ )
c@410 530 {
c@410 531 temp4B += ACF[ i ];
c@410 532 }
c@410 533
c@410 534 Energy_3 = temp3A + temp3B;
c@410 535 Energy_4 = temp4A + temp4B;
c@410 536 }
c@410 537
c@410 538 if (Energy_3 > Energy_4)
c@410 539 {
c@410 540 tsig = 3;
c@410 541 }
c@410 542 else
c@410 543 {
c@410 544 tsig = 4;
c@410 545 }
c@410 546
c@410 547
c@410 548 return tsig;
c@410 549 }
c@410 550
c@410 551 void TempoTrack::createPhaseExtractor(double *Filter, unsigned int winLength, double period, unsigned int fsp, unsigned int lastBeat)
c@410 552 {
c@410 553 int p = (int)MathUtilities::round( period );
c@410 554 int predictedOffset = 0;
c@410 555
c@410 556 #ifdef DEBUG_TEMPO_TRACK
c@410 557 std::cerr << "TempoTrack::createPhaseExtractor: period = " << period << ", p = " << p << std::endl;
c@410 558 #endif
c@410 559
c@410 560 if (p > 10000) {
c@410 561 std::cerr << "TempoTrack::createPhaseExtractor: WARNING! Highly implausible period value " << p << "!" << std::endl;
c@410 562 period = 5168 / 120;
c@410 563 }
c@410 564
c@410 565 double* phaseScratch = new double[ p*2 + 2 ];
c@410 566 for (int i = 0; i < p*2 + 2; ++i) phaseScratch[i] = 0.0;
c@410 567
c@410 568
c@410 569 if( lastBeat != 0 )
c@410 570 {
c@410 571 lastBeat = (int)MathUtilities::round((double)lastBeat );///(double)winLength);
c@410 572
c@410 573 predictedOffset = lastBeat + p - fsp;
c@410 574
c@410 575 if (predictedOffset < 0)
c@410 576 {
c@410 577 lastBeat = 0;
c@410 578 }
c@410 579 }
c@410 580
c@410 581 if( lastBeat != 0 )
c@410 582 {
c@410 583 int mu = p;
c@410 584 double sigma = (double)p/8;
c@410 585 double PhaseMin = 0.0;
c@410 586 double PhaseMax = 0.0;
c@410 587 unsigned int scratchLength = p*2;
c@410 588 double temp = 0.0;
c@410 589
c@410 590 for( int i = 0; i < scratchLength; i++ )
c@410 591 {
c@410 592 phaseScratch[ i ] = exp( -0.5 * pow( ( i - mu ) / sigma, 2 ) ) / ( sqrt( 2*PI ) *sigma );
c@410 593 }
c@410 594
c@410 595 MathUtilities::getFrameMinMax( phaseScratch, scratchLength, &PhaseMin, &PhaseMax );
c@410 596
c@410 597 for(int i = 0; i < scratchLength; i ++)
c@410 598 {
c@410 599 temp = phaseScratch[ i ];
c@410 600 phaseScratch[ i ] = (temp - PhaseMin)/PhaseMax;
c@410 601 }
c@410 602
c@410 603 #ifdef DEBUG_TEMPO_TRACK
c@410 604 std::cerr << "predictedOffset = " << predictedOffset << std::endl;
c@410 605 #endif
c@410 606
c@410 607 unsigned int index = 0;
c@410 608 for (int i = p - ( predictedOffset - 1); i < p + ( p - predictedOffset) + 1; i++)
c@410 609 {
c@410 610 #ifdef DEBUG_TEMPO_TRACK
c@410 611 std::cerr << "assigning to filter index " << index << " (size = " << p*2 << ")" << " value " << phaseScratch[i] << " from scratch index " << i << std::endl;
c@410 612 #endif
c@410 613 Filter[ index++ ] = phaseScratch[ i ];
c@410 614 }
c@410 615 }
c@410 616 else
c@410 617 {
c@410 618 for( int i = 0; i < p; i ++)
c@410 619 {
c@410 620 Filter[ i ] = 1;
c@410 621 }
c@410 622 }
c@410 623
c@410 624 delete [] phaseScratch;
c@410 625 }
c@410 626
c@410 627 int TempoTrack::phaseMM(double *DF, double *weighting, unsigned int winLength, double period)
c@410 628 {
c@410 629 int alignment = 0;
c@410 630 int p = (int)MathUtilities::round( period );
c@410 631
c@410 632 double temp = 0.0;
c@410 633
c@410 634 double* y = new double[ winLength ];
c@410 635 double* align = new double[ p ];
c@410 636
c@410 637 for( int i = 0; i < winLength; i++ )
c@410 638 {
c@410 639 y[ i ] = (double)( -i + winLength )/(double)winLength;
c@410 640 y[ i ] = pow(y [i ],2.0); // raise to power 2.
c@410 641 }
c@410 642
c@410 643 for( int o = 0; o < p; o++ )
c@410 644 {
c@410 645 temp = 0.0;
c@410 646 for(int i = 1 + (o - 1); i< winLength; i += (p + 1))
c@410 647 {
c@410 648 temp = temp + DF[ i ] * y[ i ];
c@410 649 }
c@410 650 align[ o ] = temp * weighting[ o ];
c@410 651 }
c@410 652
c@410 653
c@410 654 double valTemp = 0.0;
c@410 655 for(int i = 0; i < p; i++)
c@410 656 {
c@410 657 if( align[ i ] > valTemp )
c@410 658 {
c@410 659 valTemp = align[ i ];
c@410 660 alignment = i;
c@410 661 }
c@410 662 }
c@410 663
c@410 664 delete [] y;
c@410 665 delete [] align;
c@410 666
c@410 667 return alignment;
c@410 668 }
c@410 669
c@410 670 int TempoTrack::beatPredict(unsigned int FSP0, double alignment, double period, unsigned int step )
c@410 671 {
c@410 672 int beat = 0;
c@410 673
c@410 674 int p = (int)MathUtilities::round( period );
c@410 675 int align = (int)MathUtilities::round( alignment );
c@410 676 int FSP = (int)MathUtilities::round( FSP0 );
c@410 677
c@410 678 int FEP = FSP + ( step );
c@410 679
c@410 680 beat = FSP + align;
c@410 681
c@410 682 m_beats.push_back( beat );
c@410 683
c@410 684 while( beat + p < FEP )
c@410 685 {
c@410 686 beat += p;
c@410 687
c@410 688 m_beats.push_back( beat );
c@410 689 }
c@410 690
c@410 691 return beat;
c@410 692 }
c@410 693
c@410 694
c@410 695
c@410 696 vector<int> TempoTrack::process( vector <double> DF,
c@410 697 vector <double> *tempoReturn )
c@410 698 {
c@410 699 m_dataLength = DF.size();
c@410 700
c@410 701 m_lockedTempo = 0.0;
c@410 702
c@410 703 double period = 0.0;
c@410 704 int stepFlag = 0;
c@410 705 int constFlag = 0;
c@410 706 int FSP = 0;
c@410 707 int tsig = 0;
c@410 708 int lastBeat = 0;
c@410 709
c@410 710 vector <double> causalDF;
c@410 711
c@410 712 causalDF = DF;
c@410 713
c@410 714 //Prepare Causal Extension DFData
c@410 715 unsigned int DFCLength = m_dataLength + m_winLength;
c@410 716
c@410 717 for( unsigned int j = 0; j < m_winLength; j++ )
c@410 718 {
c@410 719 causalDF.push_back( 0 );
c@410 720 }
c@410 721
c@410 722
c@410 723 double* RW = new double[ m_lagLength ];
c@410 724 for( unsigned int clear = 0; clear < m_lagLength; clear++){ RW[ clear ] = 0.0;}
c@410 725
c@410 726 double* GW = new double[ m_lagLength ];
c@410 727 for(unsigned int clear = 0; clear < m_lagLength; clear++){ GW[ clear ] = 0.0;}
c@410 728
c@410 729 double* PW = new double[ m_lagLength ];
c@410 730 for(unsigned clear = 0; clear < m_lagLength; clear++){ PW[ clear ] = 0.0;}
c@410 731
c@410 732 m_DFFramer.setSource( &causalDF[0], m_dataLength );
c@410 733
c@410 734 unsigned int TTFrames = m_DFFramer.getMaxNoFrames();
c@410 735
c@410 736 #ifdef DEBUG_TEMPO_TRACK
c@410 737 std::cerr << "TTFrames = " << TTFrames << std::endl;
c@410 738 #endif
c@410 739
c@410 740 double* periodP = new double[ TTFrames ];
c@410 741 for(unsigned clear = 0; clear < TTFrames; clear++){ periodP[ clear ] = 0.0;}
c@410 742
c@410 743 double* periodG = new double[ TTFrames ];
c@410 744 for(unsigned clear = 0; clear < TTFrames; clear++){ periodG[ clear ] = 0.0;}
c@410 745
c@410 746 double* alignment = new double[ TTFrames ];
c@410 747 for(unsigned clear = 0; clear < TTFrames; clear++){ alignment[ clear ] = 0.0;}
c@410 748
c@410 749 m_beats.clear();
c@410 750
c@410 751 createCombFilter( RW, m_lagLength, 0, 0 );
c@410 752
c@410 753 int TTLoopIndex = 0;
c@410 754
c@410 755 for( unsigned int i = 0; i < TTFrames; i++ )
c@410 756 {
c@410 757 m_DFFramer.getFrame( m_rawDFFrame );
c@410 758
c@410 759 m_DFConditioning->process( m_rawDFFrame, m_smoothDFFrame );
c@410 760
c@410 761 m_correlator.doAutoUnBiased( m_smoothDFFrame, m_frameACF, m_winLength );
c@410 762
c@410 763 periodP[ TTLoopIndex ] = tempoMM( m_frameACF, RW, 0 );
c@410 764
c@410 765 if( GW[ 0 ] != 0 )
c@410 766 {
c@410 767 periodG[ TTLoopIndex ] = tempoMM( m_frameACF, GW, tsig );
c@410 768 }
c@410 769 else
c@410 770 {
c@410 771 periodG[ TTLoopIndex ] = 0.0;
c@410 772 }
c@410 773
c@410 774 stepDetect( periodP, periodG, TTLoopIndex, &stepFlag );
c@410 775
c@410 776 if( stepFlag == 1)
c@410 777 {
c@410 778 constDetect( periodP, TTLoopIndex, &constFlag );
c@410 779 stepFlag = 0;
c@410 780 }
c@410 781 else
c@410 782 {
c@410 783 stepFlag -= 1;
c@410 784 }
c@410 785
c@410 786 if( stepFlag < 0 )
c@410 787 {
c@410 788 stepFlag = 0;
c@410 789 }
c@410 790
c@410 791 if( constFlag != 0)
c@410 792 {
c@410 793 tsig = findMeter( m_frameACF, m_winLength, periodP[ TTLoopIndex ] );
c@410 794
c@410 795 createCombFilter( GW, m_lagLength, tsig, periodP[ TTLoopIndex ] );
c@410 796
c@410 797 periodG[ TTLoopIndex ] = tempoMM( m_frameACF, GW, tsig );
c@410 798
c@410 799 period = periodG[ TTLoopIndex ];
c@410 800
c@410 801 #ifdef DEBUG_TEMPO_TRACK
c@410 802 std::cerr << "TempoTrack::process: constFlag == " << constFlag << ", TTLoopIndex = " << TTLoopIndex << ", period from periodG = " << period << std::endl;
c@410 803 #endif
c@410 804
c@410 805 createPhaseExtractor( PW, m_winLength, period, FSP, 0 );
c@410 806
c@410 807 constFlag = 0;
c@410 808
c@410 809 }
c@410 810 else
c@410 811 {
c@410 812 if( GW[ 0 ] != 0 )
c@410 813 {
c@410 814 period = periodG[ TTLoopIndex ];
c@410 815
c@410 816 #ifdef DEBUG_TEMPO_TRACK
c@410 817 std::cerr << "TempoTrack::process: GW[0] == " << GW[0] << ", TTLoopIndex = " << TTLoopIndex << ", period from periodG = " << period << std::endl;
c@410 818 #endif
c@410 819
c@410 820 if (period > 10000) {
c@410 821 std::cerr << "TempoTrack::process: WARNING! Highly implausible period value " << period << "!" << std::endl;
c@410 822 std::cerr << "periodG contains (of " << TTFrames << " frames): " << std::endl;
c@410 823 for (int i = 0; i < TTLoopIndex + 3 && i < TTFrames; ++i) {
c@410 824 std::cerr << i << " -> " << periodG[i] << std::endl;
c@410 825 }
c@410 826 std::cerr << "periodP contains (of " << TTFrames << " frames): " << std::endl;
c@410 827 for (int i = 0; i < TTLoopIndex + 3 && i < TTFrames; ++i) {
c@410 828 std::cerr << i << " -> " << periodP[i] << std::endl;
c@410 829 }
c@410 830 period = 5168 / 120;
c@410 831 }
c@410 832
c@410 833 createPhaseExtractor( PW, m_winLength, period, FSP, lastBeat );
c@410 834
c@410 835 }
c@410 836 else
c@410 837 {
c@410 838 period = periodP[ TTLoopIndex ];
c@410 839
c@410 840 #ifdef DEBUG_TEMPO_TRACK
c@410 841 std::cerr << "TempoTrack::process: GW[0] == " << GW[0] << ", TTLoopIndex = " << TTLoopIndex << ", period from periodP = " << period << std::endl;
c@410 842 #endif
c@410 843
c@410 844 createPhaseExtractor( PW, m_winLength, period, FSP, 0 );
c@410 845 }
c@410 846 }
c@410 847
c@410 848 alignment[ TTLoopIndex ] = phaseMM( m_rawDFFrame, PW, m_winLength, period );
c@410 849
c@410 850 lastBeat = beatPredict(FSP, alignment[ TTLoopIndex ], period, m_lagLength );
c@410 851
c@410 852 FSP += (m_lagLength);
c@410 853
c@410 854 if (tempoReturn) tempoReturn->push_back(m_lockedTempo);
c@410 855
c@410 856 TTLoopIndex++;
c@410 857 }
c@410 858
c@410 859
c@410 860 delete [] periodP;
c@410 861 delete [] periodG;
c@410 862 delete [] alignment;
c@410 863
c@410 864 delete [] RW;
c@410 865 delete [] GW;
c@410 866 delete [] PW;
c@410 867
c@410 868 return m_beats;
c@410 869 }
c@410 870