3.8.5 Convolutions and Correlations

Convolution and correlation are central to many kinds of operations on lists of data. They are used in such areas as signal and image processing, statistical data analysis, approximations to partial differential equations, as well as operations on digit sequences and power series.

In both convolution and correlation the basic idea is to combine a kernel list with successive sublists of a list of data. The convolution of a kernel with a list has the general form , while the correlation has the general form .

 ListConvolve[kernel, list] form the convolution of kernel with list ListCorrelate[kernel, list] form the correlation of kernel with list

Convolution and correlation of lists.
This forms the convolution of the kernel {x, y} with a list of data.
 In[1]:=  ListConvolve[{x,y}, {a,b,c,d,e}]
 Out[1]=
This forms the correlation.
 In[2]:=  ListCorrelate[{x,y}, {a,b,c,d,e}]
 Out[2]=
In this case reversing the kernel gives exactly the same result as ListConvolve.
 In[3]:=  ListCorrelate[{y, x}, {a,b,c,d,e}]
 Out[3]=
This forms successive differences of the data.
 In[4]:=  ListCorrelate[{-1,1}, {a,b,c,d,e}]
 Out[4]=

In forming sublists to combine with a kernel, there is always an issue of what to do at the ends of the list of data. By default, ListConvolve and ListCorrelate never form sublists which would "overhang" the ends of the list of data. This means that the output you get is normally shorter than the original list of data.

With an input list of length 6, the output is in this case of length 4.
 In[5]:=  ListCorrelate[{1,1,1}, Range[6]]
 Out[5]=

In practice one often wants to get output that is as long as the original list of data. To do this requires including sublists that overhang one or both ends of the list of data. The additional elements needed to form these sublists must be filled in with some kind of "padding". By default, Mathematica takes copies of the original list to provide the padding, thus effectively treating the list as being cyclic.

 ListCorrelate[kernel, list] do not allow overhangs on either side (result shorter than list) ListCorrelate[kernel, list, 1] allow an overhang on the right (result same length as list) ListCorrelate[kernel, list, -1] allow an overhang on the left (result same length as list) ListCorrelate[kernel, list, {-1, 1}] allow overhangs on both sides (result longer than list) ListCorrelate[kernel, list, {, }] allow particular overhangs on left and right

Controlling how the ends of the list of data are treated.
The default involves no overhangs.
 In[6]:=  ListCorrelate[{x, y}, {a, b, c, d}]
 Out[6]=
The last term in the last element now comes from the beginning of the list.
 In[7]:=  ListCorrelate[{x, y}, {a, b, c, d}, 1]
 Out[7]=
Now the first term of the first element and the last term of the last element both involve wraparound.
 In[8]:=  ListCorrelate[{x, y}, {a, b, c, d}, {-1, 1}]
 Out[8]=

In the general case ListCorrelate[kernel, list, {, }] is set up so that in the first element of the result, the first element of list appears multiplied by the element at position in kernel, and in the last element of the result, the last element of list appears multiplied by the element at position in kernel. The default case in which no overhang is allowed on either side thus corresponds to ListCorrelate[kernel, list, {1, -1}].

With a kernel of length 3, alignments {-1, 2} always make the first and last elements of the result the same.
 In[9]:=  ListCorrelate[{x, y, z}, {a, b, c, d}, {-1, 2}]
 Out[9]=

For many kinds of data, it is convenient to assume not that the data is cyclic, but rather that it is padded at either end by some fixed element, often 0, or by some sequence of elements.

 ListCorrelate[kernel, list, klist, p] pad with element p ListCorrelate[kernel, list, klist, {, , ... }] pad with cyclic repetitions of the ListCorrelate[kernel, list, klist, list] pad with cyclic repetitions of the original data ListCorrelate[kernel, list, klist, {}] include no padding

Controlling the padding for a list of data.
 In[10]:=  ListCorrelate[{x, y}, {a, b, c, d}, {-1, 1}, p]
 Out[10]=
A common case is to pad with zero.
 In[11]:=  ListCorrelate[{x, y}, {a, b, c, d}, {-1, 1}, 0]
 Out[11]=
In this case q appears at one end, and p at the other.
 In[12]:=  ListCorrelate[{x, y}, {a, b, c, d}, {-1, 1}, {p, q}]
 Out[12]=

Different choices of kernel allow ListConvolve and ListCorrelate to be used for different kinds of computations.

This finds a moving average of data.
 In[13]:=  ListCorrelate[{1,1,1}/3, {a,b,c,d,e}, {-1,1}]
 Out[13]=
Here is a Gaussian kernel.
 In[14]:=  kern = Table[Exp[-n^2/100]/Sqrt[2. Pi], {n, -10, 10}] ;
This generates some "data".
 In[15]:=  data = Table[BesselJ[1, x] + 0.2 Random[ ], {x, 0, 10, .1}] ;
Here is a plot of the data.
 In[16]:=  ListPlot[data]

 Out[16]=
This convolves the kernel with the data.
 In[17]:=  ListConvolve[kern, data, {-1, 1}] ;
The result is a smoothed version of the data.
 In[18]:=  ListPlot[%]

 Out[18]=

You can use ListConvolve and ListCorrelate to handle symbolic as well as numerical data.

This forms the convolution of two symbolic lists.
 In[19]:=  ListConvolve[{a,b,c}, {u,v,w}, {1, -1}, 0]
 Out[19]=
The result corresponds exactly with the coefficients in the expanded form of this product of polynomials.
 In[20]:=  Expand[(a + b x + c x^2)(u + v x + w x^2)]
 Out[20]=

ListConvolve and ListCorrelate work on data in any number of dimensions.

This imports image data from a file.
 In[21]:=  g = ReadList["fish.data", Number, RecordLists->True];
Here is the image.
 In[22]:=  Show[Graphics[Raster[g], AspectRatio->Automatic]]

 Out[22]=
This convolves the data with a two-dimensional kernel.
 In[23]:=  ListConvolve[{{1,1,1},{1,-8,1},{1,1,1}}, g] ;
This shows the image corresponding to the data.
 In[24]:=  Show[Graphics[Raster[%], AspectRatio->Automatic]]

 Out[24]=

 RotateLeft[list, {, , ... }], RotateRight[list, {, , ... }] rotate elements cyclically by positions at level i PadLeft[list, {, , ... }], PadRight[list, {, , ... }] pad with zeros to create an array Take[list, , , ... ], Drop[list, , , ... ] take or drop elements at level i

Other functions for manipulating multidimensional data.

THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.