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@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@277: c@277: d_vec_t smoothed(df.size()); c@277: c@277: int p_post = 7; c@277: int p_pre = 8; c@277: c@277: 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@277: // find threshold for first 't' samples, where a full average cannot be computed yet c@277: for (int i = 0;i <= t;i++) c@277: { c@277: int k = std::min((i+p_pre),static_cast(df.size())); c@277: smoothed[i] = mean_array(df,1,k); c@277: } c@277: // find threshold for bulk of samples across a moving average from [i-p_pre,i+p_post] c@277: for (uint i = t+1;i < df.size()-p_post;i++) c@277: { c@277: smoothed[i] = mean_array(df,i-p_pre,i+p_post); c@277: } c@277: // for last few samples calculate threshold, again, not enough samples to do as above c@277: for (uint i = df.size()-p_post;i < df.size();i++) c@277: { c@277: int k = std::max((static_cast (i) -p_post),1); c@277: smoothed[i] = mean_array(df,k,df.size()); c@277: } c@277: c@277: // subtract the threshold from the detection function and check that it is not less than 0 c@277: for (uint i = 0;i < df.size();i++) c@277: { c@277: df[i] -= smoothed[i]; c@277: if (df[i] < 0) c@277: { c@277: df[i] = 0; c@277: } c@277: } c@277: } c@277: c@277: double c@277: TempoTrackV2::mean_array(const d_vec_t &dfin,int start,int end) c@277: { c@277: c@277: double sum = 0.; c@277: c@277: // find sum c@277: for (int i = start;i < end+1;i++) c@277: { c@277: sum += dfin[i]; c@277: } c@277: c@277: 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@277: c@277: c@277: d_vec_t a(3); c@277: d_vec_t b(3); c@277: d_vec_t lp_df(df.size()); c@277: c@277: //equivalent in matlab to [b,a] = butter(2,0.4); c@277: a[0] = 1.0000; c@277: a[1] = -0.3695; c@277: a[2] = 0.1958; c@277: b[0] = 0.2066; c@277: b[1] = 0.4131; c@277: b[2] = 0.2066; c@277: c@277: double inp1 = 0.; c@277: double inp2 = 0.; c@277: double out1 = 0.; c@277: double out2 = 0.; c@277: c@277: c@277: // forwards filtering c@277: for (uint i = 0;i < df.size();i++) c@277: { c@277: lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2; c@277: inp2 = inp1; c@277: inp1 = df[i]; c@277: out2 = out1; c@277: out1 = lp_df[i]; c@277: } c@277: c@277: c@277: // copy forwards filtering to df... c@277: // but, time-reversed, ready for backwards filtering c@277: for (uint i = 0;i < df.size();i++) c@277: { c@277: df[i] = lp_df[df.size()-i]; c@277: } c@277: c@277: for (uint i = 0;i < df.size();i++) c@277: { c@277: lp_df[i] = 0.; c@277: } c@277: c@277: inp1 = 0.; inp2 = 0.; c@277: out1 = 0.; out2 = 0.; c@277: c@277: // backwards filetering on time-reversed df c@277: for (uint i = 0;i < df.size();i++) c@277: { c@277: lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2; c@277: inp2 = inp1; c@277: inp1 = df[i]; c@277: out2 = out1; c@277: out1 = lp_df[i]; c@277: } c@277: c@277: // write the re-reversed (i.e. forward) version back to df c@277: for (uint i = 0;i < df.size();i++) c@277: { c@277: df[i] = lp_df[df.size()-i]; c@277: } c@277: c@277: c@277: } c@277: c@277: c@277: void c@277: TempoTrackV2::calculateBeatPeriod(const d_vec_t &df, d_vec_t &beat_period) c@277: { c@277: c@277: // to follow matlab.. split into 512 sample frames with a 128 hop size c@277: // calculate the acf, c@277: // then the rcf.. and then stick the rcfs as columns of a matrix c@277: // then call viterbi decoding with weight vector and transition matrix c@277: // and get best path c@277: c@277: uint wv_len = 128; c@277: double rayparam = 43.; c@277: c@277: // make rayleigh weighting curve c@277: d_vec_t wv(wv_len); c@277: 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@277: c@277: uint winlen = 512; c@277: uint step = 128; c@277: c@277: d_mat_t rcfmat; c@277: int col_counter = -1; c@277: // main loop for beat period calculation c@277: for (uint i=0; i<(df.size()-winlen); i+=step) c@277: { c@277: // get dfframe c@277: d_vec_t dfframe(winlen); c@277: for (uint k=0; k (sum/ (dfframe.size()-lag)); c@277: } c@277: c@277: c@277: // for (uint i=0; i(i); c@277: tmat[i][j] = exp( (-1.*pow((j-mu),2.)) / (2.*pow(sigma,2.)) ); c@277: } c@277: } c@277: c@277: d_mat_t delta; c@277: i_mat_t psi; c@277: for (uint i=0;i 0 ;t--) c@277: { c@277: bestpath[t] = psi[t+1][bestpath[t+1]]; c@277: } c@277: // very weird hack! c@277: bestpath[0] = psi[1][bestpath[1]]; c@277: c@277: // for (uint i=0; i (beat_period[i]); c@277: txwt[j] = exp( -0.5*pow(tightness * log((round(2*mu)-j)/mu),2)); c@277: c@277: scorecands[j] = txwt[j] * cumscore[i+prange_min+j]; c@277: } c@277: c@277: double vv = get_max_val(scorecands); c@277: int xx = get_max_ind(scorecands); c@277: c@277: cumscore[i] = alpha*vv + (1.-alpha)*localscore[i]; c@277: c@277: backlink[i] = i+prange_min+xx; c@277: c@277: } c@277: c@277: c@277: d_vec_t tmp_vec; c@277: for (uint i=cumscore.size() - beat_period[beat_period.size()-1] ; i 3*beat_period[0]) c@277: { c@277: ibeats.push_back(backlink[ibeats.back()]); c@277: } c@277: c@277: c@277: for (uint i=0; i(ibeats[i]) ); c@277: c@277: // cout << ibeats[i] << " " << beats[i] <