Git Product home page Git Product logo

autocorrelation_for_physicist's Introduction

How to correctly calculate an autocorrelation function in python

In statistical physics, an autocorrelation function is widely used to understand a system's nonequilibrium behaivor.
However, the definition of autocorrelation function used in physics (or statistics) is different from the definition of correlation function used in signal processing.
The physicist's definition is, $$\left< x_n x_0 \right>=\frac{1}{m-n} \sum_{i=0}^{m-1} x_{n+i} x_i,$$ where $m$ is the number of elements. Note that $x_i$ becomes $0$ if $i>m-1$. In the mean time, that of signal processing is $$z_n=\sum_{i=0}^{m-1} x_{n+i} x_i.$$

Unfortunately, the physicist's version is not implemented in the popular python packages such as numpy and scipy. Naively using scipy.signal.correlate due to its cosmetic similarity, therefore, often leaves physicists puzzled, forcing them to implement their own algorithms.
Due to a large number of iterations, however, such algorithms are likely to be very slow when directly implemented in python, especially compared to the scipy.signal.correlate function.

Here, we showcase two correct autocorrelation function implementations that use fast algorithms of the scipy library.
The first method is using the fast Fourier transform (FFT) algorithm (scipy.fftpack). Note that the FFT method by default performs a cyclical convolution, but an autocorrelation is defined as an acyclical convolution.
So, blindly implementing the FFT method results in an incorrect autucorrelation function. To remedy this, we insert some zeros in the data, namely zero padding, so that the algorithm runs as if it's acyclical. You can see the difference in the figure.
The second method is to modify scipy.signal.correlate so that it complies with the physicist's definition of autocorrelation function. It involves dividing each entry by the number of data points that are correlated, i.e., we are calculating the average of correlation.

alt text

autocorrelation_for_physicist's People

Contributors

iammmiru avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.