Hi, I haven't seen in Scipy a function to compute the autocorrelation of a time series. Is there one I missed ? Thanks, David
David Huard wrote:
Hi, I haven't seen in Scipy a function to compute the autocorrelation of a time series. Is there one I missed ?
Thanks,
David ------------------------------------------------------------------------
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
signal.correlate(in1,in1,full) ? Nils
Turns out its quicker to write one, than to find one... def autocorr(x, lag=1): """Returns the autocorrelation x.""" x = squeeze(asarray(x)) mu = x.mean() s = x.std() return ((x[:-lag]-mu)*(x[lag:]-mu)).sum()/(s**2)/(len(x) - lag) David
David Huard wrote:
Hi, I haven't seen in Scipy a function to compute the autocorrelation of a time series. Is there one I missed ?
Isn't numpy.correlate enough ? For autocorrelation you can do something like: r = numpy.correlate(x, x) no ? Pierre
Thanks,
David ------------------------------------------------------------------------
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
On Tue, Aug 15, 2006 at 05:18:43PM +0100, Pierre Barbier de Reuille wrote:
David Huard wrote:
Hi, I haven't seen in Scipy a function to compute the autocorrelation of a time series. Is there one I missed ?
Isn't numpy.correlate enough ? For autocorrelation you can do something like:
r = numpy.correlate(x, x)
These functions are useful when working with short 1-dimensional signals, but for larger images they are agonisingly slow. A way around the problem is to calculate the correlation using the FFT, i.e. def fft_correlate(A,B,*args,**kwargs): return S.signal.fftconvolve(A,B[::-1,::-1,...],*args,**kwargs) On my computer, I benchmarked these methods with different length signals. See the attached graph and script. You'll notice that the FFT execution time jumps around bit -- I know that it can be calculated especially quickly for lengths that are powers of two, maybe this has something to do with that. Can someone on the list enlighten me? Either way, your mileage may vary. Some say that this method is somewhat less accurate, and that it uses more memory. All I know is that it finishes calculating before the end of time. Regards Stéfan
Thanks Stefan for the tip, Fortunately, I got a pretty simple case so I don't need to rely on anything fancy. David 2006/8/15, Stefan van der Walt <stefan@sun.ac.za>:
On Tue, Aug 15, 2006 at 05:18:43PM +0100, Pierre Barbier de Reuille wrote:
David Huard wrote:
Hi, I haven't seen in Scipy a function to compute the autocorrelation of a time series. Is there one I missed ?
Isn't numpy.correlate enough ? For autocorrelation you can do something like:
r = numpy.correlate(x, x)
These functions are useful when working with short 1-dimensional signals, but for larger images they are agonisingly slow. A way around the problem is to calculate the correlation using the FFT, i.e.
def fft_correlate(A,B,*args,**kwargs): return S.signal.fftconvolve(A,B[::-1,::-1,...],*args,**kwargs)
On my computer, I benchmarked these methods with different length signals. See the attached graph and script. You'll notice that the FFT execution time jumps around bit -- I know that it can be calculated especially quickly for lengths that are powers of two, maybe this has something to do with that. Can someone on the list enlighten me?
Either way, your mileage may vary. Some say that this method is somewhat less accurate, and that it uses more memory. All I know is that it finishes calculating before the end of time.
Regards Stéfan
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
participants (4)
-
David Huard -
Nils Wagner -
Pierre Barbier de Reuille -
Stefan van der Walt