Is there a built in function to compute the autocorrelation of a signal ?
4 Answers
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 ]
-
1$\begingroup$ This only works because the list in the example is circularly even.
(# == Reverse[#]) &@Rest@Table[Mod[i^2, 17], {i, 100}]
givesTrue
$\endgroup$– RojoFeb 23, 2012 at 14:22
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]
-
$\begingroup$ @OleksandrR Thanks, I missed this in the documentation! (It's also ~100 faster :) $\endgroup$– AjasjaFeb 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$– skratchJun 4, 2013 at 17:09
-
$\begingroup$ @skratch Nope. Look under the help to see exactly what is calculated using CorrelationFunction. $\endgroup$– AjasjaJun 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$– skratchJun 5, 2013 at 19:18
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
If you search the docs for "autocorrelation", ListConvolve
turns up. There's also ListCorrelate
. Does this work for you?
-
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$– aclFeb 23, 2012 at 14:21