a tiny addition to acorr

Hi,

I was looking at the autocorrelation of a time series recently and it was useful to scale the x-axis (i.e. multiply lags by the timestep of my actual data). It’s a trivial change, but it might be useful for others. Here’s a standalone version of axes.acorr:

def scaledacorr(x, stepsize=1, normed=False, detrend=mlab.detrend_none,
usevlines=False, maxlags=None, **kwargs):

import numpy as np
x = detrend(np.asarray(x))
Nx = len(x)
y = x

c = np.correlate(x, y, mode=2)

if normed: c/= np.sqrt(np.dot(x,x) * np.dot(y,y))

if maxlags is None: maxlags = Nx - 1

if maxlags >= Nx or maxlags < 1:
    raise ValueError('maglags must be None or strictly '
                     'positive < %d'%Nx)

lags = np.arange(-maxlags,maxlags+1) * stepsize
c = c[Nx-1-maxlags:Nx+maxlags]

if usevlines:
    a = vlines(lags, [0], c, **kwargs)
    b = axhline(**kwargs)
else:

    kwargs.setdefault('marker', 'o')
    kwargs.setdefault('linestyle', 'None')
    a, = plot(lags, c, **kwargs)
    b = None

return lags, c, a, b
···


Michael Lerner, Ph.D.
IRTA Postdoctoral Fellow
Laboratory of Computational Biology NIH/NHLBI
5635 Fishers Lane, Room T909, MSC 9314
Rockville, MD 20852 (UPS/FedEx/Reality)
Bethesda MD 20892-9314 (USPS)

Hi,

I was looking at the autocorrelation of a time series recently and it was
useful to scale the x-axis (i.e. multiply lags by the timestep of my actual
data). It's a trivial change, but it might be useful for others. Here's a
standalone version of axes.acorr:

I'd be happy to consider a patch to acorr/xcorr ro add a new kwarg,
but is should be submitted as an svn patch which also includes
documentation. I don't think we need to add a new function to support
this, just enhance the existing ones. See

  http://matplotlib.sourceforge.net/faq/howto_faq.html#submit-a-patch

Thanks,
JDH

···

On Fri, Jul 10, 2009 at 3:09 PM, Michael Lerner<mglerner@...287...> wrote:

def scaledacorr(x, stepsize=1, normed=False, detrend=mlab.detrend_none,
usevlines=False, maxlags=None, **kwargs):