Mercurial > hg > python-tutorial-skeleton
annotate signal_processing.py @ 5:ea2387fd1b90 tip
Add CC0 disclaimers
author | Chris Cannam |
---|---|
date | Thu, 04 Oct 2012 22:12:02 +0100 |
parents | 4be70944d21b |
children |
rev | line source |
---|---|
Chris@5 | 1 |
Chris@5 | 2 # To the extent possible under law, Chris Cannam and QMUL have waived |
Chris@5 | 3 # all copyright and related or neighboring rights to this file |
Chris@5 | 4 # (http://creativecommons.org/about/cc0) |
Chris@3 | 5 |
Chris@3 | 6 import numpy as np |
Chris@3 | 7 |
Chris@3 | 8 def rms(samples): |
Chris@3 | 9 """Return the root mean square of the given samples""" |
Chris@3 | 10 return np.sqrt(np.mean(np.array(samples)**2)) |
Chris@3 | 11 |
Chris@3 | 12 def autocorrelation(samples): |
Chris@3 | 13 """Return the autocorrelation of the given samples, as an array with zero lag at the start""" |
Chris@3 | 14 acf = np.correlate(samples, samples, mode='full') |
Chris@3 | 15 return acf[len(acf)/2:] |
Chris@3 | 16 |
Chris@3 | 17 def bpm_to_lag(bpm, hops_per_sec): |
Chris@3 | 18 """Return the autocorrelation lag corresponding to the duration of a beat at the given tempo in bpm""" |
Chris@3 | 19 return int((60.0 / bpm) * hops_per_sec) |
Chris@3 | 20 |
Chris@3 | 21 def lag_to_bpm(lag, hops_per_sec): |
Chris@3 | 22 """Return the tempo in bpm whose beat duration corresponds to the given autocorrelation lag""" |
Chris@3 | 23 return (60.0 * hops_per_sec) / lag |
Chris@3 | 24 |