6
$\begingroup$

Is there a built in function to compute the autocorrelation of a signal ?

$\endgroup$
1
  • 1
    $\begingroup$ You can get ListCorrelate to do this. $\endgroup$
    – Szabolcs
    Feb 23, 2012 at 14:17

4 Answers 4

9
$\begingroup$

From the help file for ListConvolve, to find the autocorrelation of a list:

data = Table[Mod[i^2, 17], {i, 100}];
autocorrelation = ListConvolve[data, data, {1, 1}];
ListLinePlot[autocorrelation ]

Mathematica graphics

$\endgroup$
1
  • 1
    $\begingroup$ This only works because the list in the example is circularly even. (# == Reverse[#]) &@Rest@Table[Mod[i^2, 17], {i, 100}] gives True $\endgroup$
    – Rojo
    Feb 23, 2012 at 14:22
14
$\begingroup$

Yes, in version 9 you have CorrelationFunction built in.

data = Table[Mod[i^2, 17], {i, 100}];
autocorrelation = CorrelationFunction[data, {0, Length[data] - 1}];

ListLinePlot[autocorrelation]

autocorrelation

$\endgroup$
4
  • $\begingroup$ @OleksandrR Thanks, I missed this in the documentation! (It's also ~100 faster :) $\endgroup$
    – Ajasja
    Feb 21, 2013 at 14:16
  • $\begingroup$ This does not seem to match the output of the ListConvolve method (just look at the y-scale). Using some test functions like a square pulse and a triangular pulse, I also don't get the expected output using CorrelationFunction. Any idea what the discrepancy is? I'm working with some applications of autocorrelation functions and I'm struggling to find the discrepancy between the two methods. $\endgroup$
    – skratch
    Jun 4, 2013 at 17:09
  • $\begingroup$ @skratch Nope. Look under the help to see exactly what is calculated using CorrelationFunction. $\endgroup$
    – Ajasja
    Jun 4, 2013 at 17:13
  • 3
    $\begingroup$ In response to myself and anyone else who may be interested: The "CorrelationFunction" approach seems to indeed be the "correct" one if you're looking for an autocorrelation function (ACF). I'm not so sure about ListConvolve and ListCorrelate... In v9, CorrelationFunction is scaled to vary between 1 and -1 and takes its maximum value at the first data point, which is a property of autocorrelation functions. $\endgroup$
    – skratch
    Jun 5, 2013 at 19:18
2
$\begingroup$

Perhaps this satisfies you?

autocorrelate[exp_, varFrom_, varTo_]:=Convolve[exp, exp/.varFrom->-varFrom, varFrom, varTo]

If your signal is discrete but symbolic, change Convolve to DiscreteConvolve. If they are lists, check out ListCorrelate

$\endgroup$
1
$\begingroup$

If you search the docs for "autocorrelation", ListConvolve turns up. There's also ListCorrelate. Does this work for you?

$\endgroup$
1
  • 1
    $\begingroup$ @Rojo the point I was sort of making is that typing "autocorrelate" in the help viewer takes you to ListConvolve which has an example. $\endgroup$
    – acl
    Feb 23, 2012 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.