view signal_processing.py @ 5:ea2387fd1b90 tip

Add CC0 disclaimers
author Chris Cannam
date Thu, 04 Oct 2012 22:12:02 +0100
parents 4be70944d21b
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