24 #define EPS 0.0000008 // just some arbitrary small number 27 m_rate(rate), m_increment(increment) { }
52 for (
unsigned int i = 0;i < df.size();i++)
54 lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2;
63 for (
unsigned int i = 0;i < df.size();i++)
65 df[i] = lp_df[df.size()-i-1];
68 for (
unsigned int i = 0;i < df.size();i++)
77 for (
unsigned int i = 0;i < df.size();i++)
79 lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2;
87 for (
unsigned int i = 0;i < df.size();i++)
89 df[i] = lp_df[df.size()-i-1];
102 vector<double> &beat_period,
103 vector<double> &tempi,
104 double inputtempo,
bool constraintempo)
112 unsigned int wv_len = 128;
120 double rayparam = (60*44100/512)/inputtempo;
134 for (
unsigned int i=0; i<wv.size(); i++)
138 wv[i] = exp( (-1.*pow((static_cast<double> (i)-rayparam),2.)) / (2.*pow(rayparam/4.,2.)) );
143 for (
unsigned int i=0; i<wv.size(); i++)
147 wv[i] = (
static_cast<double> (i) / pow(rayparam,2.)) * exp((-1.*pow(-static_cast<double> (i),2.)) / (2.*pow(rayparam,2.)));
152 unsigned int winlen = 512;
153 unsigned int step = 128;
157 int col_counter = -1;
160 for (
unsigned int i=0; i+winlen<df.size(); i+=step)
164 for (
unsigned int k=0; k<winlen; k++)
166 dfframe[k] = df[i+k];
174 for (
unsigned int j=0; j<rcf.size(); j++)
176 rcfmat[col_counter].push_back( rcf[j] );
201 for (
unsigned int lag=0; lag<dfframe.size(); lag++)
206 for (
unsigned int n=0; n<(dfframe.size()-lag); n++)
208 tmp = dfframe[n] * dfframe[n+lag];
211 acf[lag] =
static_cast<double> (sum/ (dfframe.size()-lag));
217 for (
unsigned int i = 2;i < rcf.size();i++)
219 for (
int a = 1;a <= numelem;a++)
221 for (
int b = 1-a;b <= a-1;b++)
223 rcf[i-1] += ( acf[(a*i+b)-1]*wv[i-1] ) / (2.*a-1.);
232 for (
unsigned int i=0; i<rcf.size(); i++)
239 for (
unsigned int i=0; i<rcf.size(); i++)
241 rcf[i] /= (rcfsum +
EPS);
253 for (
unsigned int i=0;i<wv.size();i++)
256 for (
unsigned int j=0; j<wv.size(); j++)
258 tmat[i].push_back(0.);
266 for (
unsigned int i=20;i <wv.size()-20; i++)
268 for (
unsigned int j=20; j<wv.size()-20; j++)
270 double mu =
static_cast<double>(i);
271 tmat[i][j] = exp( (-1.*pow((j-mu),2.)) / (2.*pow(sigma,2.)) );
280 for (
unsigned int i=0;i <rcfmat.size(); i++)
284 for (
unsigned int j=0; j<rcfmat[i].size(); j++)
286 delta[i].push_back(0.);
292 unsigned int T = delta.size();
296 unsigned int Q = delta[0].size();
299 for (
unsigned int j=0; j<Q; j++)
301 delta[0][j] = wv[j] * rcfmat[0][j];
305 double deltasum = 0.;
306 for (
unsigned int i=0; i<Q; i++)
308 deltasum += delta[0][i];
310 for (
unsigned int i=0; i<Q; i++)
312 delta[0][i] /= (deltasum +
EPS);
316 for (
unsigned int t=1; t<T; t++)
320 for (
unsigned int j=0; j<Q; j++)
322 for (
unsigned int i=0; i<Q; i++)
324 tmp_vec[i] = delta[t-1][i] * tmat[j][i];
331 delta[t][j] *= rcfmat[t][j];
335 double deltasum = 0.;
336 for (
unsigned int i=0; i<Q; i++)
338 deltasum += delta[t][i];
340 for (
unsigned int i=0; i<Q; i++)
342 delta[t][i] /= (deltasum +
EPS);
348 for (
unsigned int i=0; i<Q; i++)
350 tmp_vec[i] = delta[T-1][i];
357 for (
unsigned int t=T-2; t>0 ;t--)
359 bestpath[t] = psi[t+1][bestpath[t+1]];
363 bestpath[0] = psi[1][bestpath[1]];
365 unsigned int lastind = 0;
366 for (
unsigned int i=0; i<T; i++)
368 unsigned int step = 128;
369 for (
unsigned int j=0; j<step; j++)
372 beat_period[lastind] = bestpath[i];
378 for (
unsigned int i=lastind; i<beat_period.size(); i++)
380 beat_period[i] = beat_period[lastind];
383 for (
unsigned int i = 0; i < beat_period.size(); i++)
393 for (
unsigned int i=0; i<df.size(); i++)
409 for (
unsigned int i=0; i<df.size(); i++)
425 for (
unsigned int i=0; i<df.size(); i++)
430 for (
unsigned int i=0; i<df.size(); i++)
442 const vector<double> &beat_period,
443 vector<double> &beats,
double alpha,
double tightness)
445 if (df.empty() || beat_period.empty())
return;
451 for (
unsigned int i=0; i<df.size(); i++)
453 localscore[i] = df[i];
465 for (
unsigned int i=0; i<localscore.size(); i++)
467 int prange_min = -2*beat_period[i];
468 int prange_max = round(-0.5*beat_period[i]);
471 d_vec_t txwt (prange_max - prange_min + 1);
472 d_vec_t scorecands (txwt.size());
474 for (
unsigned int j=0;j<txwt.size();j++)
476 double mu =
static_cast<double> (beat_period[i]);
477 txwt[j] = exp( -0.5*pow(tightness * log((round(2*mu)-j)/mu),2));
482 int cscore_ind = i+prange_min+j;
485 scorecands[j] = txwt[j] * cumscore[cscore_ind];
493 cumscore[i] = alpha*vv + (1.-alpha)*localscore[i];
494 backlink[i] = i+prange_min+xx;
501 for (
unsigned int i=cumscore.size() - beat_period[beat_period.size()-1] ; i<cumscore.size(); i++)
503 tmp_vec.push_back(cumscore[i]);
506 int startpoint =
get_max_ind(tmp_vec) + cumscore.size() - beat_period[beat_period.size()-1] ;
509 if (startpoint >= (
int)backlink.size()) startpoint = backlink.size()-1;
514 ibeats.push_back(startpoint);
516 while (backlink[ibeats.back()] > 0)
519 int b = ibeats.back();
520 if (backlink[b] == b)
break;
521 ibeats.push_back(backlink[b]);
525 for (
unsigned int i=0; i<ibeats.size(); i++)
527 beats.push_back( static_cast<double>(ibeats[ibeats.size()-i-1]) );
static void adaptiveThreshold(std::vector< double > &data)
Threshold the input/output vector data against a moving-mean average filter.
void normalise_vec(d_vec_t &df)
void viterbi_decode(const d_mat_t &rcfmat, const d_vec_t &wv, d_vec_t &bp, d_vec_t &tempi)
double get_max_val(const d_vec_t &df)
int get_max_ind(const d_vec_t &df)
vector< vector< int > > i_mat_t
void calculateBeats(const vector< double > &df, const vector< double > &beatPeriod, vector< double > &beats)
void get_rcf(const d_vec_t &dfframe, const d_vec_t &wv, d_vec_t &rcf)
TempoTrackV2(float sampleRate, size_t dfIncrement)
Construct a tempo tracker that will operate on beat detection function data calculated from audio at ...
vector< vector< double > > d_mat_t
void calculateBeatPeriod(const vector< double > &df, vector< double > &beatPeriod, vector< double > &tempi)
void filter_df(d_vec_t &df)