Chris@3: Chris@3: import numpy as np Chris@3: Chris@3: def rms(samples): Chris@3: """Return the root mean square of the given samples""" Chris@3: return np.sqrt(np.mean(np.array(samples)**2)) Chris@3: Chris@3: def autocorrelation(samples): Chris@3: """Return the autocorrelation of the given samples, as an array with zero lag at the start""" Chris@3: acf = np.correlate(samples, samples, mode='full') Chris@3: return acf[len(acf)/2:] Chris@3: Chris@3: def bpm_to_lag(bpm, hops_per_sec): Chris@3: """Return the autocorrelation lag corresponding to the duration of a beat at the given tempo in bpm""" Chris@3: return int((60.0 / bpm) * hops_per_sec) Chris@3: Chris@3: def lag_to_bpm(lag, hops_per_sec): Chris@3: """Return the tempo in bpm whose beat duration corresponds to the given autocorrelation lag""" Chris@3: return (60.0 * hops_per_sec) / lag Chris@3: