To answer your first question, numpy.correlate(a, v, mode) convolves a with the opposite sign of v and gives the results trimmed by the specified mode. the convolution definition , C (t) = Ξ£ -β <i <β a i v t + i where -β <t <β allows you to get results from -β to β, but you obviously cannot store an infinitely long array. Thus, it must be trimmed, and it is here that the mode is turned on. There are 3 different modes: full, identical and valid:
- "full" mode returns results for each
t , where both a and v have some overlap. - βsameβ mode returns a result with the same length as the shortest vector (
a or v ). - The "real" mode returns results only when
a and v completely overlap. The documentation for numpy.convolve gives more details about the modes.
For your second question, I think numpy.correlate gives you autocorrelation, it just gives you a little more. Autocorrelation is used to determine how much a similar signal or function is for itself with a certain time difference. With a time difference of 0, autocorrelation should be the highest because the signal is identical to itself, so you expected the first element in the array of autocorrelation results to be the largest. However, the correlation does not start with a time difference of 0. It starts with a negative time difference, closes to 0 and then becomes positive. That is, you expected:
autocorrelation (a) = Ξ£ -β <i <β a i v t + i , where 0 <= t <β
But you had:
autocorrelation (a) = Ξ£ -β <i <β a i v t + i where -β <t <β
What you need to do is take the last half of the correlation result, and that should be the autocorrelation you are looking for. A simple python function to do this:
def autocorr(x): result = numpy.correlate(x, x, mode='full') return result[result.size/2:]
Of course, you will need error checking to make sure that x is actually the 1st array. In addition, this explanation is probably not the most mathematically rigorous one. I cheated on infinities because the definition of convolution uses them, but this does not necessarily apply to autocorrelation. Thus, the theoretical part of this explanation may be slightly disgusting, but I hope the practical results are useful. These autocorrelation pages are very useful and can give you a much better theoretical background if you don't mind wading through notation and hard concepts.
A. Levy Mar 24 '09 at 6:09 2009-03-24 06:09
source share