# HG changeset patch # User Chris Cannam # Date 1349367245 -3600 # Node ID 4be70944d21b903c0c412095924f7098fd7b17ee # Parent 32bf6cb28341f00b28ace0c1cadedf507c330358 Add some basic signal processing methods we need diff -r 32bf6cb28341 -r 4be70944d21b signal_processing.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/signal_processing.py Thu Oct 04 17:14:05 2012 +0100 @@ -0,0 +1,20 @@ + +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 + diff -r 32bf6cb28341 -r 4be70944d21b test_signal_processing.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test_signal_processing.py Thu Oct 04 17:14:05 2012 +0100 @@ -0,0 +1,27 @@ + +import signal_processing as sp +import numpy as np + +def test_rms(): + assert sp.rms([1,1,1]) == 1.0 + assert sp.rms([0.5,-0.5]) == 0.5 + assert sp.rms([0,0,0,0]) == 0.0 + assert sp.rms([5]) == 5.0 + assert abs(sp.rms([1,2,-2]) - np.sqrt(3.0)) < 0.000001 + +def test_autocorrelation(): + samples = [1,0,0,1,0,0,1,0,0,1,0,0,1,0,0] + acf = sp.autocorrelation(samples) + assert len(acf) == len(samples) + assert np.argmax(acf) == 0 + assert acf[3] > acf[2] + assert acf[3] > acf[4] + +def test_bpm_lag_conversions(): + assert sp.bpm_to_lag(60, 1) == 1 + assert sp.bpm_to_lag(60, 120) == 120 + assert sp.bpm_to_lag(30, 120) == 240 + assert sp.bpm_to_lag(120, 1) == 0 # should be an integer + assert sp.lag_to_bpm(120, 120) == 60.0 + assert sp.lag_to_bpm(60, 120) == 120.0 + assert sp.lag_to_bpm(80, 6) == 4.5