c@277: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@277: c@277: /* c@277: QM DSP Library c@277: c@277: Centre for Digital Music, Queen Mary, University of London. c@277: This file copyright 2008-2009 Matthew Davies and QMUL. c@277: All rights reserved. c@277: */ c@277: c@277: #include "TempoTrackV2.h" c@277: c@277: #include c@277: #include c@278: #include c@277: c@277: c@277: //#define FRAMESIZE 512 c@277: //#define BIGFRAMESIZE 1024 c@277: #define TWOPI 6.283185307179586232 c@277: #define EPS 0.0000008 // just some arbitrary small number c@277: c@277: TempoTrackV2::TempoTrackV2() { } c@277: TempoTrackV2::~TempoTrackV2() { } c@277: c@277: void c@277: TempoTrackV2::adapt_thresh(d_vec_t &df) c@277: { c@278: d_vec_t smoothed(df.size()); c@278: c@278: int p_post = 7; c@278: int p_pre = 8; c@277: c@278: int t = std::min(static_cast(df.size()),p_post); // what is smaller, p_post of df size. This is to avoid accessing outside of arrays c@277: c@278: // find threshold for first 't' samples, where a full average cannot be computed yet c@278: for (int i = 0;i <= t;i++) c@278: { c@278: int k = std::min((i+p_pre),static_cast(df.size())); c@278: smoothed[i] = mean_array(df,1,k); c@278: } c@278: // find threshold for bulk of samples across a moving average from [i-p_pre,i+p_post] c@278: for (uint i = t+1;i < df.size()-p_post;i++) c@278: { c@278: smoothed[i] = mean_array(df,i-p_pre,i+p_post); c@278: } c@278: // for last few samples calculate threshold, again, not enough samples to do as above c@278: for (uint i = df.size()-p_post;i < df.size();i++) c@278: { c@278: int k = std::max((static_cast (i) -p_post),1); c@278: smoothed[i] = mean_array(df,k,df.size()); c@278: } c@277: c@278: // subtract the threshold from the detection function and check that it is not less than 0 c@278: for (uint i = 0;i < df.size();i++) c@278: { c@278: df[i] -= smoothed[i]; c@278: if (df[i] < 0) c@278: { c@278: df[i] = 0; c@278: } c@278: } c@277: } c@277: c@277: double c@277: TempoTrackV2::mean_array(const d_vec_t &dfin,int start,int end) c@277: { c@278: double sum = 0.; c@278: c@278: // find sum c@278: for (int i = start;i < end;i++) c@278: { c@278: sum += dfin[i]; c@278: } c@277: c@278: return static_cast (sum / (end - start + 1) ); // average and return c@277: } c@277: c@277: void c@277: TempoTrackV2::filter_df(d_vec_t &df) c@277: { c@278: d_vec_t a(3); c@278: d_vec_t b(3); c@278: d_vec_t lp_df(df.size()); c@277: c@278: //equivalent in matlab to [b,a] = butter(2,0.4); c@278: a[0] = 1.0000; c@278: a[1] = -0.3695; c@278: a[2] = 0.1958; c@278: b[0] = 0.2066; c@278: b[1] = 0.4131; c@278: b[2] = 0.2066; c@278: c@278: double inp1 = 0.; c@278: double inp2 = 0.; c@278: double out1 = 0.; c@278: double out2 = 0.; c@277: c@277: c@278: // forwards filtering c@278: for (uint i = 0;i < df.size();i++) c@278: { c@278: lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2; c@278: inp2 = inp1; c@278: inp1 = df[i]; c@278: out2 = out1; c@278: out1 = lp_df[i]; c@278: } c@277: c@278: // copy forwards filtering to df... c@278: // but, time-reversed, ready for backwards filtering c@278: for (uint i = 0;i < df.size();i++) c@278: { c@278: df[i] = lp_df[df.size()-i-1]; c@278: } c@277: c@278: for (uint i = 0;i < df.size();i++) c@278: { c@278: lp_df[i] = 0.; c@278: } c@277: c@278: inp1 = 0.; inp2 = 0.; c@278: out1 = 0.; out2 = 0.; c@277: c@277: // backwards filetering on time-reversed df c@278: for (uint i = 0;i < df.size();i++) c@278: { c@278: lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2; c@278: inp2 = inp1; c@278: inp1 = df[i]; c@278: out2 = out1; c@278: out1 = lp_df[i]; c@278: } c@277: c@277: // write the re-reversed (i.e. forward) version back to df c@278: for (uint i = 0;i < df.size();i++) c@278: { c@278: df[i] = lp_df[df.size()-i-1]; c@278: } c@277: } c@277: c@277: c@277: void c@278: TempoTrackV2::calculateBeatPeriod(const d_vec_t &df, d_vec_t &beat_period, c@278: d_vec_t &tempi) c@277: { c@278: // to follow matlab.. split into 512 sample frames with a 128 hop size c@278: // calculate the acf, c@278: // then the rcf.. and then stick the rcfs as columns of a matrix c@278: // then call viterbi decoding with weight vector and transition matrix c@278: // and get best path c@277: c@278: uint wv_len = 128; c@278: double rayparam = 43.; c@277: c@278: // make rayleigh weighting curve c@278: d_vec_t wv(wv_len); c@278: for (uint i=0; i (i) / pow(rayparam,2.)) * exp((-1.*pow(-static_cast (i),2.)) / (2.*pow(rayparam,2.))); c@277: } c@277: c@278: // beat tracking frame size (roughly 6 seconds) and hop (1.5 seconds) c@278: uint winlen = 512; c@278: uint step = 128; c@278: c@278: // matrix to store output of comb filter bank, increment column of matrix at each frame c@278: d_mat_t rcfmat; c@278: int col_counter = -1; c@278: c@278: // main loop for beat period calculation c@278: for (uint i=0; i<(df.size()-winlen); i+=step) c@278: { c@278: // get dfframe c@278: d_vec_t dfframe(winlen); c@278: for (uint k=0; k (sum/ (dfframe.size()-lag)); c@278: } c@277: c@278: // now apply comb filtering c@278: int numelem = 4; c@278: c@278: for (uint i = 2;i < rcf.size();i++) // max beat period c@278: { c@278: for (int a = 1;a <= numelem;a++) // number of comb elements c@278: { c@278: for (int b = 1-a;b <= a-1;b++) // general state using normalisation of comb elements c@278: { c@278: rcf[i-1] += ( acf[(a*i+b)-1]*wv[i-1] ) / (2.*a-1.); // calculate value for comb filter row c@278: } c@278: } c@278: } c@278: c@278: // apply adaptive threshold to rcf c@278: adapt_thresh(rcf); c@278: c@278: double rcfsum =0.; c@278: for (uint i=0; i(i); c@278: tmat[i][j] = exp( (-1.*pow((j-mu),2.)) / (2.*pow(sigma,2.)) ); c@278: } c@278: } c@277: c@278: // parameters for Viterbi decoding... this part is taken from c@278: // Murphy's matlab c@277: c@278: d_mat_t delta; c@278: i_mat_t psi; c@278: for (uint i=0;i 0 ;t--) c@278: { c@278: bestpath[t] = psi[t+1][bestpath[t+1]]; c@278: } c@277: c@278: // weird but necessary hack -- couldn't get above loop to terminate at t >= 0 c@278: bestpath[0] = psi[1][bestpath[1]]; c@277: c@278: uint lastind = 0; c@278: for (uint i=0; i (beat_period[i]); c@278: txwt[j] = exp( -0.5*pow(tightness * log((round(2*mu)-j)/mu),2)); c@277: c@278: // IF IN THE ALLOWED RANGE, THEN LOOK AT CUMSCORE[I+PRANGE_MIN+J c@278: // ELSE LEAVE AT DEFAULT VALUE FROM INITIALISATION: D_VEC_T SCORECANDS (TXWT.SIZE()); c@277: c@278: int cscore_ind = i+prange_min+j; c@278: if (cscore_ind >= 0) c@278: { c@278: scorecands[j] = txwt[j] * cumscore[cscore_ind]; c@278: } c@278: } c@277: c@278: // find max value and index of maximum value c@278: double vv = get_max_val(scorecands); c@278: int xx = get_max_ind(scorecands); c@277: c@278: cumscore[i] = alpha*vv + (1.-alpha)*localscore[i]; c@278: backlink[i] = i+prange_min+xx; c@278: } c@278: c@278: // STARTING POINT, I.E. LAST BEAT.. PICK A STRONG POINT IN cumscore VECTOR c@278: d_vec_t tmp_vec; c@278: for (uint i=cumscore.size() - beat_period[beat_period.size()-1] ; i 0) c@278: { c@278: ibeats.push_back(backlink[ibeats.back()]); c@278: } c@277: c@278: // REVERSE SEQUENCE OF IBEATS AND STORE AS BEATS c@278: for (uint i=0; i(ibeats[ibeats.size()-i-1]) ); c@278: } c@277: } c@277: c@277: