cannam@52: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ cannam@52: cannam@52: /* cannam@52: QM DSP Library cannam@52: cannam@52: Centre for Digital Music, Queen Mary, University of London. cannam@52: This file copyright 2008-2009 Matthew Davies and QMUL. cannam@52: All rights reserved. cannam@52: */ cannam@52: cannam@52: #include "TempoTrackV2.h" cannam@52: cannam@52: #include cannam@52: #include cannam@53: #include cannam@52: cannam@54: #include "maths/MathUtilities.h" cannam@52: cannam@52: #define EPS 0.0000008 // just some arbitrary small number cannam@52: cannam@54: TempoTrackV2::TempoTrackV2(float rate, size_t increment) : cannam@54: m_rate(rate), m_increment(increment) { } cannam@52: TempoTrackV2::~TempoTrackV2() { } cannam@52: cannam@52: void cannam@52: TempoTrackV2::filter_df(d_vec_t &df) cannam@52: { cannam@53: d_vec_t a(3); cannam@53: d_vec_t b(3); cannam@53: d_vec_t lp_df(df.size()); cannam@52: cannam@53: //equivalent in matlab to [b,a] = butter(2,0.4); cannam@53: a[0] = 1.0000; cannam@53: a[1] = -0.3695; cannam@53: a[2] = 0.1958; cannam@53: b[0] = 0.2066; cannam@53: b[1] = 0.4131; cannam@53: b[2] = 0.2066; cannam@53: cannam@53: double inp1 = 0.; cannam@53: double inp2 = 0.; cannam@53: double out1 = 0.; cannam@53: double out2 = 0.; cannam@52: cannam@52: cannam@53: // forwards filtering cannam@53: for (uint i = 0;i < df.size();i++) cannam@53: { cannam@53: lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2; cannam@53: inp2 = inp1; cannam@53: inp1 = df[i]; cannam@53: out2 = out1; cannam@53: out1 = lp_df[i]; cannam@53: } cannam@52: cannam@53: // copy forwards filtering to df... cannam@53: // but, time-reversed, ready for backwards filtering cannam@53: for (uint i = 0;i < df.size();i++) cannam@53: { cannam@53: df[i] = lp_df[df.size()-i-1]; cannam@53: } cannam@52: cannam@53: for (uint i = 0;i < df.size();i++) cannam@53: { cannam@53: lp_df[i] = 0.; cannam@53: } cannam@52: cannam@53: inp1 = 0.; inp2 = 0.; cannam@53: out1 = 0.; out2 = 0.; cannam@52: cannam@52: // backwards filetering on time-reversed df cannam@53: for (uint i = 0;i < df.size();i++) cannam@53: { cannam@53: lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2; cannam@53: inp2 = inp1; cannam@53: inp1 = df[i]; cannam@53: out2 = out1; cannam@53: out1 = lp_df[i]; cannam@53: } cannam@52: cannam@52: // write the re-reversed (i.e. forward) version back to df cannam@53: for (uint i = 0;i < df.size();i++) cannam@53: { cannam@53: df[i] = lp_df[df.size()-i-1]; cannam@53: } cannam@52: } cannam@52: cannam@52: cannam@52: void cannam@53: TempoTrackV2::calculateBeatPeriod(const d_vec_t &df, d_vec_t &beat_period, cannam@53: d_vec_t &tempi) cannam@52: { cannam@53: // to follow matlab.. split into 512 sample frames with a 128 hop size cannam@53: // calculate the acf, cannam@53: // then the rcf.. and then stick the rcfs as columns of a matrix cannam@53: // then call viterbi decoding with weight vector and transition matrix cannam@53: // and get best path cannam@52: cannam@53: uint wv_len = 128; cannam@53: double rayparam = 43.; cannam@52: cannam@53: // make rayleigh weighting curve cannam@53: d_vec_t wv(wv_len); cannam@53: for (uint i=0; i (i) / pow(rayparam,2.)) * exp((-1.*pow(-static_cast (i),2.)) / (2.*pow(rayparam,2.))); cannam@52: } cannam@52: cannam@53: // beat tracking frame size (roughly 6 seconds) and hop (1.5 seconds) cannam@53: uint winlen = 512; cannam@53: uint step = 128; cannam@53: cannam@53: // matrix to store output of comb filter bank, increment column of matrix at each frame cannam@53: d_mat_t rcfmat; cannam@53: int col_counter = -1; cannam@53: cannam@53: // main loop for beat period calculation cannam@53: for (uint i=0; i<(df.size()-winlen); i+=step) cannam@53: { cannam@53: // get dfframe cannam@53: d_vec_t dfframe(winlen); cannam@53: for (uint k=0; k (sum/ (dfframe.size()-lag)); cannam@53: } cannam@52: cannam@53: // now apply comb filtering cannam@53: int numelem = 4; cannam@53: cannam@53: for (uint i = 2;i < rcf.size();i++) // max beat period cannam@53: { cannam@53: for (int a = 1;a <= numelem;a++) // number of comb elements cannam@53: { cannam@53: for (int b = 1-a;b <= a-1;b++) // general state using normalisation of comb elements cannam@53: { cannam@53: rcf[i-1] += ( acf[(a*i+b)-1]*wv[i-1] ) / (2.*a-1.); // calculate value for comb filter row cannam@53: } cannam@53: } cannam@53: } cannam@53: cannam@53: // apply adaptive threshold to rcf cannam@54: MathUtilities::adaptiveThreshold(rcf); cannam@53: cannam@53: double rcfsum =0.; cannam@53: for (uint i=0; i(i); cannam@53: tmat[i][j] = exp( (-1.*pow((j-mu),2.)) / (2.*pow(sigma,2.)) ); cannam@53: } cannam@53: } cannam@52: cannam@53: // parameters for Viterbi decoding... this part is taken from cannam@53: // Murphy's matlab cannam@52: cannam@53: d_mat_t delta; cannam@53: i_mat_t psi; cannam@53: for (uint i=0;i 0 ;t--) cannam@53: { cannam@53: bestpath[t] = psi[t+1][bestpath[t+1]]; cannam@53: } cannam@52: cannam@53: // weird but necessary hack -- couldn't get above loop to terminate at t >= 0 cannam@53: bestpath[0] = psi[1][bestpath[1]]; cannam@52: cannam@53: uint lastind = 0; cannam@53: for (uint i=0; i (beat_period[i]); cannam@53: txwt[j] = exp( -0.5*pow(tightness * log((round(2*mu)-j)/mu),2)); cannam@52: cannam@53: // IF IN THE ALLOWED RANGE, THEN LOOK AT CUMSCORE[I+PRANGE_MIN+J cannam@53: // ELSE LEAVE AT DEFAULT VALUE FROM INITIALISATION: D_VEC_T SCORECANDS (TXWT.SIZE()); cannam@52: cannam@53: int cscore_ind = i+prange_min+j; cannam@53: if (cscore_ind >= 0) cannam@53: { cannam@53: scorecands[j] = txwt[j] * cumscore[cscore_ind]; cannam@53: } cannam@53: } cannam@52: cannam@53: // find max value and index of maximum value cannam@53: double vv = get_max_val(scorecands); cannam@53: int xx = get_max_ind(scorecands); cannam@52: cannam@53: cumscore[i] = alpha*vv + (1.-alpha)*localscore[i]; cannam@53: backlink[i] = i+prange_min+xx; cannam@55: cannam@55: std::cerr << "backlink[" << i << "] <= " << backlink[i] << std::endl; cannam@53: } cannam@53: cannam@53: // STARTING POINT, I.E. LAST BEAT.. PICK A STRONG POINT IN cumscore VECTOR cannam@53: d_vec_t tmp_vec; cannam@53: for (uint i=cumscore.size() - beat_period[beat_period.size()-1] ; i