Mercurial > hg > qm-dsp
comparison dsp/tempotracking/TempoTrackV2.cpp @ 501:12b5a9244bb0
Style fixes: avoid unsigned, fix formatting
author | Chris Cannam <cannam@all-day-breakfast.com> |
---|---|
date | Wed, 05 Jun 2019 10:21:48 +0100 |
parents | bb78ca3fe7de |
children | 162673c8f9de |
comparison
equal
deleted
inserted
replaced
500:8a8693f38b91 | 501:12b5a9244bb0 |
---|---|
32 TempoTrackV2::~TempoTrackV2() { } | 32 TempoTrackV2::~TempoTrackV2() { } |
33 | 33 |
34 void | 34 void |
35 TempoTrackV2::filter_df(d_vec_t &df) | 35 TempoTrackV2::filter_df(d_vec_t &df) |
36 { | 36 { |
37 int df_len = int(df.size()); | |
38 | |
37 d_vec_t a(3); | 39 d_vec_t a(3); |
38 d_vec_t b(3); | 40 d_vec_t b(3); |
39 d_vec_t lp_df(df.size()); | 41 d_vec_t lp_df(df_len); |
40 | 42 |
41 //equivalent in matlab to [b,a] = butter(2,0.4); | 43 //equivalent in matlab to [b,a] = butter(2,0.4); |
42 a[0] = 1.0000; | 44 a[0] = 1.0000; |
43 a[1] = -0.3695; | 45 a[1] = -0.3695; |
44 a[2] = 0.1958; | 46 a[2] = 0.1958; |
51 double out1 = 0.; | 53 double out1 = 0.; |
52 double out2 = 0.; | 54 double out2 = 0.; |
53 | 55 |
54 | 56 |
55 // forwards filtering | 57 // forwards filtering |
56 for (unsigned int i = 0;i < df.size();i++) { | 58 for (int i = 0; i < df_len; i++) { |
57 lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2; | 59 lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2; |
58 inp2 = inp1; | 60 inp2 = inp1; |
59 inp1 = df[i]; | 61 inp1 = df[i]; |
60 out2 = out1; | 62 out2 = out1; |
61 out1 = lp_df[i]; | 63 out1 = lp_df[i]; |
62 } | 64 } |
63 | 65 |
64 // copy forwards filtering to df... | 66 // copy forwards filtering to df... |
65 // but, time-reversed, ready for backwards filtering | 67 // but, time-reversed, ready for backwards filtering |
66 for (unsigned int i = 0;i < df.size();i++) { | 68 for (int i = 0; i < df_len; i++) { |
67 df[i] = lp_df[df.size()-i-1]; | 69 df[i] = lp_df[df_len - i - 1]; |
68 } | 70 } |
69 | 71 |
70 for (unsigned int i = 0;i < df.size();i++) { | 72 for (int i = 0; i < df_len; i++) { |
71 lp_df[i] = 0.; | 73 lp_df[i] = 0.; |
72 } | 74 } |
73 | 75 |
74 inp1 = 0.; inp2 = 0.; | 76 inp1 = 0.; inp2 = 0.; |
75 out1 = 0.; out2 = 0.; | 77 out1 = 0.; out2 = 0.; |
76 | 78 |
77 // backwards filetering on time-reversed df | 79 // backwards filetering on time-reversed df |
78 for (unsigned int i = 0;i < df.size();i++) { | 80 for (int i = 0; i < df_len; i++) { |
79 lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2; | 81 lp_df[i] = b[0]*df[i] + b[1]*inp1 + b[2]*inp2 - a[1]*out1 - a[2]*out2; |
80 inp2 = inp1; | 82 inp2 = inp1; |
81 inp1 = df[i]; | 83 inp1 = df[i]; |
82 out2 = out1; | 84 out2 = out1; |
83 out1 = lp_df[i]; | 85 out1 = lp_df[i]; |
84 } | 86 } |
85 | 87 |
86 // write the re-reversed (i.e. forward) version back to df | 88 // write the re-reversed (i.e. forward) version back to df |
87 for (unsigned int i = 0;i < df.size();i++) { | 89 for (int i = 0; i < df_len; i++) { |
88 df[i] = lp_df[df.size()-i-1]; | 90 df[i] = lp_df[df_len - i - 1]; |
89 } | 91 } |
90 } | 92 } |
91 | 93 |
92 | 94 |
93 // MEPD 28/11/12 | 95 // MEPD 28/11/12 |
106 // calculate the acf, | 108 // calculate the acf, |
107 // then the rcf.. and then stick the rcfs as columns of a matrix | 109 // then the rcf.. and then stick the rcfs as columns of a matrix |
108 // then call viterbi decoding with weight vector and transition matrix | 110 // then call viterbi decoding with weight vector and transition matrix |
109 // and get best path | 111 // and get best path |
110 | 112 |
111 unsigned int wv_len = 128; | 113 int wv_len = 128; |
112 | 114 |
113 // MEPD 28/11/12 | 115 // MEPD 28/11/12 |
114 // the default value of inputtempo in the beat tracking plugin is 120 | 116 // the default value of inputtempo in the beat tracking plugin is 120 |
115 // so if the user specifies a different inputtempo, the rayparam will be updated | 117 // so if the user specifies a different inputtempo, the rayparam will be updated |
116 // accordingly. | 118 // accordingly. |
122 d_vec_t wv(wv_len); | 124 d_vec_t wv(wv_len); |
123 | 125 |
124 // check whether or not to use rayleigh weighting (if constraintempo is false) | 126 // check whether or not to use rayleigh weighting (if constraintempo is false) |
125 // or use gaussian weighting it (constraintempo is true) | 127 // or use gaussian weighting it (constraintempo is true) |
126 if (constraintempo) { | 128 if (constraintempo) { |
127 for (unsigned int i=0; i<wv.size(); i++) { | 129 for (int i = 0; i < wv_len; i++) { |
128 // MEPD 28/11/12 | 130 // MEPD 28/11/12 |
129 // do a gaussian weighting instead of rayleigh | 131 // do a gaussian weighting instead of rayleigh |
130 wv[i] = exp( (-1.*pow((static_cast<double> (i)-rayparam),2.)) / (2.*pow(rayparam/4.,2.)) ); | 132 wv[i] = exp( (-1.*pow((double(i)-rayparam),2.)) / (2.*pow(rayparam/4.,2.)) ); |
131 } | 133 } |
132 } else { | 134 } else { |
133 for (unsigned int i=0; i<wv.size(); i++) { | 135 for (int i = 0; i < wv_len; i++) { |
134 // MEPD 28/11/12 | 136 // MEPD 28/11/12 |
135 // standard rayleigh weighting over periodicities | 137 // standard rayleigh weighting over periodicities |
136 wv[i] = (static_cast<double> (i) / pow(rayparam,2.)) * exp((-1.*pow(-static_cast<double> (i),2.)) / (2.*pow(rayparam,2.))); | 138 wv[i] = (double(i) / pow(rayparam,2.)) * exp((-1.*pow(-double(i),2.)) / (2.*pow(rayparam,2.))); |
137 } | 139 } |
138 } | 140 } |
139 | 141 |
140 // beat tracking frame size (roughly 6 seconds) and hop (1.5 seconds) | 142 // beat tracking frame size (roughly 6 seconds) and hop (1.5 seconds) |
141 unsigned int winlen = 512; | 143 int winlen = 512; |
142 unsigned int step = 128; | 144 int step = 128; |
143 | 145 |
144 // matrix to store output of comb filter bank, increment column of matrix at each frame | 146 // matrix to store output of comb filter bank, increment column of matrix at each frame |
145 d_mat_t rcfmat; | 147 d_mat_t rcfmat; |
146 int col_counter = -1; | 148 int col_counter = -1; |
149 int df_len = int(df.size()); | |
147 | 150 |
148 // main loop for beat period calculation | 151 // main loop for beat period calculation |
149 for (unsigned int i=0; i+winlen<df.size(); i+=step) { | 152 for (int i = 0; i+winlen < df_len; i+=step) { |
150 | 153 |
151 // get dfframe | 154 // get dfframe |
152 d_vec_t dfframe(winlen); | 155 d_vec_t dfframe(winlen); |
153 for (unsigned int k=0; k<winlen; k++) { | 156 for (int k=0; k < winlen; k++) { |
154 dfframe[k] = df[i+k]; | 157 dfframe[k] = df[i+k]; |
155 } | 158 } |
156 // get rcf vector for current frame | 159 // get rcf vector for current frame |
157 d_vec_t rcf(wv_len); | 160 d_vec_t rcf(wv_len); |
158 get_rcf(dfframe,wv,rcf); | 161 get_rcf(dfframe,wv,rcf); |
159 | 162 |
160 rcfmat.push_back( d_vec_t() ); // adds a new column | 163 rcfmat.push_back( d_vec_t() ); // adds a new column |
161 col_counter++; | 164 col_counter++; |
162 for (unsigned int j=0; j<rcf.size(); j++) { | 165 for (int j = 0; j < wv_len; j++) { |
163 rcfmat[col_counter].push_back( rcf[j] ); | 166 rcfmat[col_counter].push_back( rcf[j] ); |
164 } | 167 } |
165 } | 168 } |
166 | 169 |
167 // now call viterbi decoding function | 170 // now call viterbi decoding function |
180 | 183 |
181 d_vec_t dfframe(dfframe_in); | 184 d_vec_t dfframe(dfframe_in); |
182 | 185 |
183 MathUtilities::adaptiveThreshold(dfframe); | 186 MathUtilities::adaptiveThreshold(dfframe); |
184 | 187 |
185 d_vec_t acf(dfframe.size()); | 188 int dfframe_len = int(dfframe.size()); |
186 | 189 int rcf_len = int(rcf.size()); |
187 for (unsigned int lag=0; lag<dfframe.size(); lag++) { | 190 |
191 d_vec_t acf(dfframe_len); | |
192 | |
193 for (int lag = 0; lag < dfframe_len; lag++) { | |
188 double sum = 0.; | 194 double sum = 0.; |
189 double tmp = 0.; | 195 double tmp = 0.; |
190 | 196 |
191 for (unsigned int n=0; n<(dfframe.size()-lag); n++) { | 197 for (int n = 0; n < (dfframe_len - lag); n++) { |
192 tmp = dfframe[n] * dfframe[n+lag]; | 198 tmp = dfframe[n] * dfframe[n + lag]; |
193 sum += tmp; | 199 sum += tmp; |
194 } | 200 } |
195 acf[lag] = static_cast<double> (sum/ (dfframe.size()-lag)); | 201 acf[lag] = double(sum/ (dfframe_len - lag)); |
196 } | 202 } |
197 | 203 |
198 // now apply comb filtering | 204 // now apply comb filtering |
199 int numelem = 4; | 205 int numelem = 4; |
200 | 206 |
201 for (unsigned int i = 2;i < rcf.size();i++) { // max beat period | 207 for (int i = 2; i < rcf_len; i++) { // max beat period |
202 for (int a = 1;a <= numelem;a++) { // number of comb elements | 208 for (int a = 1; a <= numelem; a++) { // number of comb elements |
203 for (int b = 1-a;b <= a-1;b++) { // general state using normalisation of comb elements | 209 for (int b = 1-a; b <= a-1; b++) { // general state using normalisation of comb elements |
204 rcf[i-1] += ( acf[(a*i+b)-1]*wv[i-1] ) / (2.*a-1.); // calculate value for comb filter row | 210 rcf[i-1] += ( acf[(a*i+b)-1]*wv[i-1] ) / (2.*a-1.); // calculate value for comb filter row |
205 } | 211 } |
206 } | 212 } |
207 } | 213 } |
208 | 214 |
209 // apply adaptive threshold to rcf | 215 // apply adaptive threshold to rcf |
210 MathUtilities::adaptiveThreshold(rcf); | 216 MathUtilities::adaptiveThreshold(rcf); |
211 | 217 |
212 double rcfsum =0.; | 218 double rcfsum =0.; |
213 for (unsigned int i=0; i<rcf.size(); i++) { | 219 for (int i = 0; i < rcf_len; i++) { |
214 rcf[i] += EPS ; | 220 rcf[i] += EPS ; |
215 rcfsum += rcf[i]; | 221 rcfsum += rcf[i]; |
216 } | 222 } |
217 | 223 |
218 // normalise rcf to sum to unity | 224 // normalise rcf to sum to unity |
219 for (unsigned int i=0; i<rcf.size(); i++) { | 225 for (int i = 0; i < rcf_len; i++) { |
220 rcf[i] /= (rcfsum + EPS); | 226 rcf[i] /= (rcfsum + EPS); |
221 } | 227 } |
222 } | 228 } |
223 | 229 |
224 void | 230 void |
225 TempoTrackV2::viterbi_decode(const d_mat_t &rcfmat, const d_vec_t &wv, d_vec_t &beat_period, d_vec_t &tempi) | 231 TempoTrackV2::viterbi_decode(const d_mat_t &rcfmat, const d_vec_t &wv, d_vec_t &beat_period, d_vec_t &tempi) |
226 { | 232 { |
227 // following Kevin Murphy's Viterbi decoding to get best path of | 233 // following Kevin Murphy's Viterbi decoding to get best path of |
228 // beat periods through rfcmat | 234 // beat periods through rfcmat |
229 | 235 |
236 int wv_len = int(wv.size()); | |
237 | |
230 // make transition matrix | 238 // make transition matrix |
231 d_mat_t tmat; | 239 d_mat_t tmat; |
232 for (unsigned int i=0;i<wv.size();i++) { | 240 for (int i = 0; i < wv_len; i++) { |
233 tmat.push_back ( d_vec_t() ); // adds a new column | 241 tmat.push_back ( d_vec_t() ); // adds a new column |
234 for (unsigned int j=0; j<wv.size(); j++) { | 242 for (int j = 0; j < wv_len; j++) { |
235 tmat[i].push_back(0.); // fill with zeros initially | 243 tmat[i].push_back(0.); // fill with zeros initially |
236 } | 244 } |
237 } | 245 } |
238 | 246 |
239 // variance of Gaussians in transition matrix | 247 // variance of Gaussians in transition matrix |
240 // formed of Gaussians on diagonal - implies slow tempo change | 248 // formed of Gaussians on diagonal - implies slow tempo change |
241 double sigma = 8.; | 249 double sigma = 8.; |
242 // don't want really short beat periods, or really long ones | 250 // don't want really short beat periods, or really long ones |
243 for (unsigned int i=20;i <wv.size()-20; i++) { | 251 for (int i = 20; i < wv_len - 20; i++) { |
244 for (unsigned int j=20; j<wv.size()-20; j++) { | 252 for (int j = 20; j < wv_len - 20; j++) { |
245 double mu = static_cast<double>(i); | 253 double mu = double(i); |
246 tmat[i][j] = exp( (-1.*pow((j-mu),2.)) / (2.*pow(sigma,2.)) ); | 254 tmat[i][j] = exp( (-1.*pow((j-mu),2.)) / (2.*pow(sigma,2.)) ); |
247 } | 255 } |
248 } | 256 } |
249 | 257 |
250 // parameters for Viterbi decoding... this part is taken from | 258 // parameters for Viterbi decoding... this part is taken from |
251 // Murphy's matlab | 259 // Murphy's matlab |
252 | 260 |
253 d_mat_t delta; | 261 d_mat_t delta; |
254 i_mat_t psi; | 262 i_mat_t psi; |
255 for (unsigned int i=0;i <rcfmat.size(); i++) { | 263 for (int i = 0; i < int(rcfmat.size()); i++) { |
256 delta.push_back( d_vec_t()); | 264 delta.push_back(d_vec_t()); |
257 psi.push_back( i_vec_t()); | 265 psi.push_back(i_vec_t()); |
258 for (unsigned int j=0; j<rcfmat[i].size(); j++) { | 266 for (int j = 0; j < int(rcfmat[i].size()); j++) { |
259 delta[i].push_back(0.); // fill with zeros initially | 267 delta[i].push_back(0.); // fill with zeros initially |
260 psi[i].push_back(0); // fill with zeros initially | 268 psi[i].push_back(0); // fill with zeros initially |
261 } | 269 } |
262 } | 270 } |
263 | 271 |
264 unsigned int T = delta.size(); | 272 int T = delta.size(); |
265 | 273 |
266 if (T < 2) return; // can't do anything at all meaningful | 274 if (T < 2) return; // can't do anything at all meaningful |
267 | 275 |
268 unsigned int Q = delta[0].size(); | 276 int Q = delta[0].size(); |
269 | 277 |
270 // initialize first column of delta | 278 // initialize first column of delta |
271 for (unsigned int j=0; j<Q; j++) { | 279 for (int j = 0; j < Q; j++) { |
272 delta[0][j] = wv[j] * rcfmat[0][j]; | 280 delta[0][j] = wv[j] * rcfmat[0][j]; |
273 psi[0][j] = 0; | 281 psi[0][j] = 0; |
274 } | 282 } |
275 | 283 |
276 double deltasum = 0.; | 284 double deltasum = 0.; |
277 for (unsigned int i=0; i<Q; i++) { | 285 for (int i = 0; i < Q; i++) { |
278 deltasum += delta[0][i]; | 286 deltasum += delta[0][i]; |
279 } | 287 } |
280 for (unsigned int i=0; i<Q; i++) { | 288 for (int i = 0; i < Q; i++) { |
281 delta[0][i] /= (deltasum + EPS); | 289 delta[0][i] /= (deltasum + EPS); |
282 } | 290 } |
283 | 291 |
284 for (unsigned int t=1; t<T; t++) | 292 for (int t=1; t < T; t++) |
285 { | 293 { |
286 d_vec_t tmp_vec(Q); | 294 d_vec_t tmp_vec(Q); |
287 | 295 |
288 for (unsigned int j=0; j<Q; j++) { | 296 for (int j = 0; j < Q; j++) { |
289 for (unsigned int i=0; i<Q; i++) { | 297 for (int i = 0; i < Q; i++) { |
290 tmp_vec[i] = delta[t-1][i] * tmat[j][i]; | 298 tmp_vec[i] = delta[t-1][i] * tmat[j][i]; |
291 } | 299 } |
292 | 300 |
293 delta[t][j] = get_max_val(tmp_vec); | 301 delta[t][j] = get_max_val(tmp_vec); |
294 | 302 |
297 delta[t][j] *= rcfmat[t][j]; | 305 delta[t][j] *= rcfmat[t][j]; |
298 } | 306 } |
299 | 307 |
300 // normalise current delta column | 308 // normalise current delta column |
301 double deltasum = 0.; | 309 double deltasum = 0.; |
302 for (unsigned int i=0; i<Q; i++) { | 310 for (int i = 0; i < Q; i++) { |
303 deltasum += delta[t][i]; | 311 deltasum += delta[t][i]; |
304 } | 312 } |
305 for (unsigned int i=0; i<Q; i++) { | 313 for (int i = 0; i < Q; i++) { |
306 delta[t][i] /= (deltasum + EPS); | 314 delta[t][i] /= (deltasum + EPS); |
307 } | 315 } |
308 } | 316 } |
309 | 317 |
310 i_vec_t bestpath(T); | 318 i_vec_t bestpath(T); |
311 d_vec_t tmp_vec(Q); | 319 d_vec_t tmp_vec(Q); |
312 for (unsigned int i=0; i<Q; i++) { | 320 for (int i = 0; i < Q; i++) { |
313 tmp_vec[i] = delta[T-1][i]; | 321 tmp_vec[i] = delta[T-1][i]; |
314 } | 322 } |
315 | 323 |
316 // find starting point - best beat period for "last" frame | 324 // find starting point - best beat period for "last" frame |
317 bestpath[T-1] = get_max_ind(tmp_vec); | 325 bestpath[T-1] = get_max_ind(tmp_vec); |
318 | 326 |
319 // backtrace through index of maximum values in psi | 327 // backtrace through index of maximum values in psi |
320 for (unsigned int t=T-2; t>0 ;t--) { | 328 for (int t=T-2; t>0 ;t--) { |
321 bestpath[t] = psi[t+1][bestpath[t+1]]; | 329 bestpath[t] = psi[t+1][bestpath[t+1]]; |
322 } | 330 } |
323 | 331 |
324 // weird but necessary hack -- couldn't get above loop to terminate at t >= 0 | 332 // weird but necessary hack -- couldn't get above loop to terminate at t >= 0 |
325 bestpath[0] = psi[1][bestpath[1]]; | 333 bestpath[0] = psi[1][bestpath[1]]; |
326 | 334 |
327 unsigned int lastind = 0; | 335 int lastind = 0; |
328 for (unsigned int i=0; i<T; i++) { | 336 for (int i = 0; i < T; i++) { |
329 unsigned int step = 128; | 337 int step = 128; |
330 for (unsigned int j=0; j<step; j++) { | 338 for (int j = 0; j < step; j++) { |
331 lastind = i*step+j; | 339 lastind = i*step+j; |
332 beat_period[lastind] = bestpath[i]; | 340 beat_period[lastind] = bestpath[i]; |
333 } | 341 } |
334 // std::cerr << "bestpath[" << i << "] = " << bestpath[i] << " (used for beat_periods " << i*step << " to " << i*step+step-1 << ")" << std::endl; | 342 // std::cerr << "bestpath[" << i << "] = " << bestpath[i] << " (used for beat_periods " << i*step << " to " << i*step+step-1 << ")" << std::endl; |
335 } | 343 } |
336 | 344 |
337 //fill in the last values... | 345 // fill in the last values... |
338 for (unsigned int i=lastind; i<beat_period.size(); i++) { | 346 for (int i = lastind; i < int(beat_period.size()); i++) { |
339 beat_period[i] = beat_period[lastind]; | 347 beat_period[i] = beat_period[lastind]; |
340 } | 348 } |
341 | 349 |
342 for (unsigned int i = 0; i < beat_period.size(); i++) { | 350 for (int i = 0; i < int(beat_period.size()); i++) { |
343 tempi.push_back((60. * m_rate / m_increment)/beat_period[i]); | 351 tempi.push_back((60. * m_rate / m_increment)/beat_period[i]); |
344 } | 352 } |
345 } | 353 } |
346 | 354 |
347 double | 355 double |
348 TempoTrackV2::get_max_val(const d_vec_t &df) | 356 TempoTrackV2::get_max_val(const d_vec_t &df) |
349 { | 357 { |
350 double maxval = 0.; | 358 double maxval = 0.; |
351 for (unsigned int i=0; i<df.size(); i++) { | 359 int df_len = int(df.size()); |
360 | |
361 for (int i = 0; i < df_len; i++) { | |
352 if (maxval < df[i]) { | 362 if (maxval < df[i]) { |
353 maxval = df[i]; | 363 maxval = df[i]; |
354 } | 364 } |
355 } | 365 } |
356 | 366 |
360 int | 370 int |
361 TempoTrackV2::get_max_ind(const d_vec_t &df) | 371 TempoTrackV2::get_max_ind(const d_vec_t &df) |
362 { | 372 { |
363 double maxval = 0.; | 373 double maxval = 0.; |
364 int ind = 0; | 374 int ind = 0; |
365 for (unsigned int i=0; i<df.size(); i++) { | 375 int df_len = int(df.size()); |
376 | |
377 for (int i = 0; i < df_len; i++) { | |
366 if (maxval < df[i]) { | 378 if (maxval < df[i]) { |
367 maxval = df[i]; | 379 maxval = df[i]; |
368 ind = i; | 380 ind = i; |
369 } | 381 } |
370 } | 382 } |
374 | 386 |
375 void | 387 void |
376 TempoTrackV2::normalise_vec(d_vec_t &df) | 388 TempoTrackV2::normalise_vec(d_vec_t &df) |
377 { | 389 { |
378 double sum = 0.; | 390 double sum = 0.; |
379 for (unsigned int i=0; i<df.size(); i++) { | 391 int df_len = int(df.size()); |
392 | |
393 for (int i = 0; i < df_len; i++) { | |
380 sum += df[i]; | 394 sum += df[i]; |
381 } | 395 } |
382 | 396 |
383 for (unsigned int i=0; i<df.size(); i++) { | 397 for (int i = 0; i < df_len; i++) { |
384 df[i]/= (sum + EPS); | 398 df[i]/= (sum + EPS); |
385 } | 399 } |
386 } | 400 } |
387 | 401 |
388 // MEPD 28/11/12 | 402 // MEPD 28/11/12 |
394 const vector<double> &beat_period, | 408 const vector<double> &beat_period, |
395 vector<double> &beats, double alpha, double tightness) | 409 vector<double> &beats, double alpha, double tightness) |
396 { | 410 { |
397 if (df.empty() || beat_period.empty()) return; | 411 if (df.empty() || beat_period.empty()) return; |
398 | 412 |
399 d_vec_t cumscore(df.size()); // store cumulative score | 413 int df_len = int(df.size()); |
400 i_vec_t backlink(df.size()); // backlink (stores best beat locations at each time instant) | 414 |
401 d_vec_t localscore(df.size()); // localscore, for now this is the same as the detection function | 415 d_vec_t cumscore(df_len); // store cumulative score |
402 | 416 i_vec_t backlink(df_len); // backlink (stores best beat locations at each time instant) |
403 for (unsigned int i=0; i<df.size(); i++) { | 417 d_vec_t localscore(df_len); // localscore, for now this is the same as the detection function |
418 | |
419 for (int i = 0; i < df_len; i++) { | |
404 localscore[i] = df[i]; | 420 localscore[i] = df[i]; |
405 backlink[i] = -1; | 421 backlink[i] = -1; |
406 } | 422 } |
407 | 423 |
408 //double tightness = 4.; | 424 //double tightness = 4.; |
411 // debug statements that can be removed. | 427 // debug statements that can be removed. |
412 // std::cerr << "alpha" << alpha << std::endl; | 428 // std::cerr << "alpha" << alpha << std::endl; |
413 // std::cerr << "tightness" << tightness << std::endl; | 429 // std::cerr << "tightness" << tightness << std::endl; |
414 | 430 |
415 // main loop | 431 // main loop |
416 for (unsigned int i=0; i<localscore.size(); i++) { | 432 for (int i = 0; i < df_len; i++) { |
417 | 433 |
418 int prange_min = -2*beat_period[i]; | 434 int prange_min = -2*beat_period[i]; |
419 int prange_max = round(-0.5*beat_period[i]); | 435 int prange_max = round(-0.5*beat_period[i]); |
420 | 436 |
421 // transition range | 437 // transition range |
422 d_vec_t txwt (prange_max - prange_min + 1); | 438 int txwt_len = prange_max - prange_min + 1; |
423 d_vec_t scorecands (txwt.size()); | 439 d_vec_t txwt (txwt_len); |
424 | 440 d_vec_t scorecands (txwt_len); |
425 for (unsigned int j=0;j<txwt.size();j++) { | 441 |
442 for (int j = 0; j < txwt_len; j++) { | |
426 | 443 |
427 double mu = static_cast<double> (beat_period[i]); | 444 double mu = double(beat_period[i]); |
428 txwt[j] = exp( -0.5*pow(tightness * log((round(2*mu)-j)/mu),2)); | 445 txwt[j] = exp( -0.5*pow(tightness * log((round(2*mu)-j)/mu),2)); |
429 | 446 |
430 // IF IN THE ALLOWED RANGE, THEN LOOK AT CUMSCORE[I+PRANGE_MIN+J | 447 // IF IN THE ALLOWED RANGE, THEN LOOK AT CUMSCORE[I+PRANGE_MIN+J |
431 // ELSE LEAVE AT DEFAULT VALUE FROM INITIALISATION: D_VEC_T SCORECANDS (TXWT.SIZE()); | 448 // ELSE LEAVE AT DEFAULT VALUE FROM INITIALISATION: D_VEC_T SCORECANDS (TXWT.SIZE()); |
432 | 449 |
433 int cscore_ind = i+prange_min+j; | 450 int cscore_ind = i + prange_min + j; |
434 if (cscore_ind >= 0) { | 451 if (cscore_ind >= 0) { |
435 scorecands[j] = txwt[j] * cumscore[cscore_ind]; | 452 scorecands[j] = txwt[j] * cumscore[cscore_ind]; |
436 } | 453 } |
437 } | 454 } |
438 | 455 |
446 // std::cerr << "backlink[" << i << "] <= " << backlink[i] << std::endl; | 463 // std::cerr << "backlink[" << i << "] <= " << backlink[i] << std::endl; |
447 } | 464 } |
448 | 465 |
449 // STARTING POINT, I.E. LAST BEAT.. PICK A STRONG POINT IN cumscore VECTOR | 466 // STARTING POINT, I.E. LAST BEAT.. PICK A STRONG POINT IN cumscore VECTOR |
450 d_vec_t tmp_vec; | 467 d_vec_t tmp_vec; |
451 for (unsigned int i=cumscore.size() - beat_period[beat_period.size()-1] ; i<cumscore.size(); i++) { | 468 for (int i = df_len - beat_period[beat_period.size()-1] ; i < df_len; i++) { |
452 tmp_vec.push_back(cumscore[i]); | 469 tmp_vec.push_back(cumscore[i]); |
453 } | 470 } |
454 | 471 |
455 int startpoint = get_max_ind(tmp_vec) + | 472 int startpoint = get_max_ind(tmp_vec) + |
456 cumscore.size() - beat_period[beat_period.size()-1] ; | 473 df_len - beat_period[beat_period.size()-1] ; |
457 | 474 |
458 // can happen if no results obtained earlier (e.g. input too short) | 475 // can happen if no results obtained earlier (e.g. input too short) |
459 if (startpoint >= (int)backlink.size()) { | 476 if (startpoint >= int(backlink.size())) { |
460 startpoint = backlink.size()-1; | 477 startpoint = int(backlink.size()) - 1; |
461 } | 478 } |
462 | 479 |
463 // USE BACKLINK TO GET EACH NEW BEAT (TOWARDS THE BEGINNING OF THE FILE) | 480 // USE BACKLINK TO GET EACH NEW BEAT (TOWARDS THE BEGINNING OF THE FILE) |
464 // BACKTRACKING FROM THE END TO THE BEGINNING.. MAKING SURE NOT TO GO BEFORE SAMPLE 0 | 481 // BACKTRACKING FROM THE END TO THE BEGINNING.. MAKING SURE NOT TO GO BEFORE SAMPLE 0 |
465 i_vec_t ibeats; | 482 i_vec_t ibeats; |
471 if (backlink[b] == b) break; // shouldn't happen... haha | 488 if (backlink[b] == b) break; // shouldn't happen... haha |
472 ibeats.push_back(backlink[b]); | 489 ibeats.push_back(backlink[b]); |
473 } | 490 } |
474 | 491 |
475 // REVERSE SEQUENCE OF IBEATS AND STORE AS BEATS | 492 // REVERSE SEQUENCE OF IBEATS AND STORE AS BEATS |
476 for (unsigned int i=0; i<ibeats.size(); i++) { | 493 for (int i = 0; i < int(ibeats.size()); i++) { |
477 beats.push_back( static_cast<double>(ibeats[ibeats.size()-i-1]) ); | 494 beats.push_back(double(ibeats[ibeats.size() - i - 1])); |
478 } | 495 } |
479 } | 496 } |
480 | 497 |
481 | 498 |