view signal_processing.py @ 3:4be70944d21b skeleton_v2

Add some basic signal processing methods we need
author Chris Cannam
date Thu, 04 Oct 2012 17:14:05 +0100
parents
children ea2387fd1b90
line wrap: on
line source

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