# HG changeset patch # User cannam # Date 1232463661 0 # Node ID 4d1f32efcafd8b29516d4a4c75f9b6d071c79894 # Parent 114e833c07acaf16e2319e122ab6f83622f83f27 * Add Matthew's newer beat tracking implementation diff -r 114e833c07ac -r 4d1f32efcafd dsp/tempotracking/TempoTrackV2.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dsp/tempotracking/TempoTrackV2.cpp Tue Jan 20 15:01:01 2009 +0000 @@ -0,0 +1,567 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ + +/* + QM DSP Library + + Centre for Digital Music, Queen Mary, University of London. + This file copyright 2008-2009 Matthew Davies and QMUL. + All rights reserved. +*/ + +#include "TempoTrackV2.h" + +#include +#include + + +//#define FRAMESIZE 512 +//#define BIGFRAMESIZE 1024 +#define TWOPI 6.283185307179586232 +#define EPS 0.0000008 // just some arbitrary small number + +TempoTrackV2::TempoTrackV2() { } +TempoTrackV2::~TempoTrackV2() { } + +void +TempoTrackV2::adapt_thresh(d_vec_t &df) +{ + + d_vec_t smoothed(df.size()); + + int p_post = 7; + int p_pre = 8; + + 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 + + // find threshold for first 't' samples, where a full average cannot be computed yet + for (int i = 0;i <= t;i++) + { + int k = std::min((i+p_pre),static_cast(df.size())); + smoothed[i] = mean_array(df,1,k); + } + // find threshold for bulk of samples across a moving average from [i-p_pre,i+p_post] + for (uint i = t+1;i < df.size()-p_post;i++) + { + smoothed[i] = mean_array(df,i-p_pre,i+p_post); + } + // for last few samples calculate threshold, again, not enough samples to do as above + for (uint i = df.size()-p_post;i < df.size();i++) + { + int k = std::max((static_cast (i) -p_post),1); + smoothed[i] = mean_array(df,k,df.size()); + } + + // subtract the threshold from the detection function and check that it is not less than 0 + for (uint i = 0;i < df.size();i++) + { + df[i] -= smoothed[i]; + if (df[i] < 0) + { + df[i] = 0; + } + } +} + +double +TempoTrackV2::mean_array(const d_vec_t &dfin,int start,int end) +{ + + double sum = 0.; + + // find sum + for (int i = start;i < end+1;i++) + { + sum += dfin[i]; + } + + return static_cast (sum / (end - start + 1) ); // average and return +} + +void +TempoTrackV2::filter_df(d_vec_t &df) +{ + + + d_vec_t a(3); + d_vec_t b(3); + d_vec_t lp_df(df.size()); + + //equivalent in matlab to [b,a] = butter(2,0.4); + a[0] = 1.0000; + a[1] = -0.3695; + a[2] = 0.1958; + b[0] = 0.2066; + b[1] = 0.4131; + b[2] = 0.2066; + + double inp1 = 0.; + double inp2 = 0.; + double out1 = 0.; + double out2 = 0.; + + + // forwards filtering + for (uint i = 0;i < df.size();i++) + { + lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2; + inp2 = inp1; + inp1 = df[i]; + out2 = out1; + out1 = lp_df[i]; + } + + + // copy forwards filtering to df... + // but, time-reversed, ready for backwards filtering + for (uint i = 0;i < df.size();i++) + { + df[i] = lp_df[df.size()-i]; + } + + for (uint i = 0;i < df.size();i++) + { + lp_df[i] = 0.; + } + + inp1 = 0.; inp2 = 0.; + out1 = 0.; out2 = 0.; + + // backwards filetering on time-reversed df + for (uint i = 0;i < df.size();i++) + { + lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2; + inp2 = inp1; + inp1 = df[i]; + out2 = out1; + out1 = lp_df[i]; + } + + // write the re-reversed (i.e. forward) version back to df + for (uint i = 0;i < df.size();i++) + { + df[i] = lp_df[df.size()-i]; + } + + +} + + +void +TempoTrackV2::calculateBeatPeriod(const d_vec_t &df, d_vec_t &beat_period) +{ + +// to follow matlab.. split into 512 sample frames with a 128 hop size +// calculate the acf, +// then the rcf.. and then stick the rcfs as columns of a matrix +// then call viterbi decoding with weight vector and transition matrix +// and get best path + + uint wv_len = 128; + double rayparam = 43.; + + // make rayleigh weighting curve + d_vec_t wv(wv_len); + for (uint i=0; i (i) / pow(rayparam,2.)) * exp((-1.*pow(-static_cast (i),2.)) / (2.*pow(rayparam,2.))); + } + + + uint winlen = 512; + uint step = 128; + + d_mat_t rcfmat; + int col_counter = -1; + // main loop for beat period calculation + for (uint i=0; i<(df.size()-winlen); i+=step) + { + // get dfframe + d_vec_t dfframe(winlen); + for (uint k=0; k (sum/ (dfframe.size()-lag)); + } + + +// for (uint i=0; i(i); + tmat[i][j] = exp( (-1.*pow((j-mu),2.)) / (2.*pow(sigma,2.)) ); + } + } + + d_mat_t delta; + i_mat_t psi; + for (uint i=0;i 0 ;t--) + { + bestpath[t] = psi[t+1][bestpath[t+1]]; + } + // very weird hack! + bestpath[0] = psi[1][bestpath[1]]; + +// for (uint i=0; i (beat_period[i]); + txwt[j] = exp( -0.5*pow(tightness * log((round(2*mu)-j)/mu),2)); + + scorecands[j] = txwt[j] * cumscore[i+prange_min+j]; + } + + double vv = get_max_val(scorecands); + int xx = get_max_ind(scorecands); + + cumscore[i] = alpha*vv + (1.-alpha)*localscore[i]; + + backlink[i] = i+prange_min+xx; + + } + + + d_vec_t tmp_vec; + for (uint i=cumscore.size() - beat_period[beat_period.size()-1] ; i 3*beat_period[0]) + { + ibeats.push_back(backlink[ibeats.back()]); + } + + + for (uint i=0; i(ibeats[i]) ); + + // cout << ibeats[i] << " " << beats[i] < + +using std::vector; + +class TempoTrackV2 +{ +public: + TempoTrackV2(); + ~TempoTrackV2(); + + void calculateBeatPeriod(const vector &df, + vector &beatPeriod); + + void calculateBeats(const vector &df, + const vector &beatPeriod, + vector &beats); + +private: + typedef vector i_vec_t; + typedef vector > i_mat_t; + typedef vector d_vec_t; + typedef vector > d_mat_t; + + void adapt_thresh(d_vec_t &df); + double mean_array(const d_vec_t &dfin, int start, int end); + void filter_df(d_vec_t &df); + void get_rcf(const d_vec_t &dfframe, const d_vec_t &wv, d_vec_t &rcf); + void viterbi_decode(const d_mat_t &rcfmat, const d_vec_t &wv, d_vec_t &bp); + double get_max_val(const d_vec_t &df); + int get_max_ind(const d_vec_t &df); + void normalise_vec(d_vec_t &df); +}; + +#endif diff -r 114e833c07ac -r 4d1f32efcafd qm-dsp.pro --- a/qm-dsp.pro Thu Dec 04 11:59:29 2008 +0000 +++ b/qm-dsp.pro Tue Jan 20 15:01:01 2009 +0000 @@ -4,7 +4,9 @@ OBJECTS_DIR = tmp_obj MOC_DIR = tmp_moc -linux-g++:QMAKE_CXXFLAGS_RELEASE += -DNDEBUG -O3 -fno-exceptions -fPIC -march=pentium3 -mfpmath=sse -msse -ffast-math +linux-g++-64:QMAKE_CXXFLAGS_RELEASE += -DNDEBUG -O3 -fno-exceptions -fPIC -ffast-math + +linux-g++-64:INCLUDEPATH += ../qm-vamp-plugins/build/linux/amd64 macx-g++:QMAKE_CXXFLAGS_RELEASE += -fvisibility=hidden @@ -47,6 +49,7 @@ dsp/signalconditioning/FiltFilt.h \ dsp/signalconditioning/Framer.h \ dsp/tempotracking/TempoTrack.h \ + dsp/tempotracking/TempoTrackV2.h \ dsp/tonal/ChangeDetectionFunction.h \ dsp/tonal/TCSgram.h \ dsp/tonal/TonalEstimator.h \ @@ -81,6 +84,7 @@ dsp/signalconditioning/FiltFilt.cpp \ dsp/signalconditioning/Framer.cpp \ dsp/tempotracking/TempoTrack.cpp \ + dsp/tempotracking/TempoTrackV2.cpp \ dsp/tonal/ChangeDetectionFunction.cpp \ dsp/tonal/TCSgram.cpp \ dsp/tonal/TonalEstimator.cpp \