Mercurial > hg > python-tutorial-ssw2012-output
view signal_processing.py @ 12:9973b7cd0d31 tip
Implement rest of tempo estimator
author | Chris Cannam |
---|---|
date | Mon, 08 Oct 2012 15:46:54 +0100 |
parents | ea2387fd1b90 |
children |
line wrap: on
line source
# To the extent possible under law, Chris Cannam and QMUL have waived # all copyright and related or neighboring rights to this file # (http://creativecommons.org/about/cc0) import numpy as np def rms(samples): """Return the root mean square of the given samples""" return np.sqrt(np.mean(np.array(samples)**2)) def autocorrelation(samples): """Return the autocorrelation of the given samples, as an array with zero lag at the start""" acf = np.correlate(samples, samples, mode='full') return acf[len(acf)/2:] def bpm_to_lag(bpm, hops_per_sec): """Return the autocorrelation lag corresponding to the duration of a beat at the given tempo in bpm""" return int((60.0 / bpm) * hops_per_sec) def lag_to_bpm(lag, hops_per_sec): """Return the tempo in bpm whose beat duration corresponds to the given autocorrelation lag""" return (60.0 * hops_per_sec) / lag