Re: Discrete Convolution
- To: mathgroup at smc.vnet.net
- Subject: [mg18982] Re: Discrete Convolution
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Tue, 3 Aug 1999 13:44:31 -0400
- References: <7nrddv$i60@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Alister: The new Version 4.x functions, ListConvolve and ListCorrelate look like meeting your needs. For example ListConvolve[{a0, a1, a2}, {b0, b1, b2}, {1, 3}, 0] {a0 b0, a1 b0 + a0 b1, a2 b0 + a1 b1 + a0 b2, a2 b1 + a1 b2, a2 b2} There is a lot more to these functions - the full HelpBrowser information needs to be consulted, but here is the shorter informatiion: ?ListConvolve "ListConvolve[ker, list] forms the convolution of the kernel ker with list. \ ListConvolve[ker, list, k] forms the cyclic convolution in which the kth \ element of ker is aligned with each element in list. ListConvolve[ker, list, \ {kL, kR}] forms the cyclic convolution whose first element contains list[[1]] \ ker[[kL]] and whose last element contains list[[-1]] ker[[kR]]. \ ListConvolve[ker, list, klist, p] forms the convolution in which list is \ padded at each end with repetitions of the element p. ListConvolve[ker, list, \ klist, {p1, p2, ... }] forms the convolution in which list is padded at each \ end with cyclic repetitions of the pi. ListConvolve[ker, list, klist, \ padding, g, h] forms a generalized convolution in which g is used in place of \ Times and h in place of Plus. ListConvolve[ker, list, klist, padding, g, h, \ lev] forms a convolution using elements at level lev in ker and list." Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 Alister McAlister <alisterq at psy.uwa.edu.au> wrote in message news:7nrddv$i60 at smc.vnet.net... > I want a function that mimics Matlab's "conv" function for doing a discrete > convolution of two lists. > > CONV Convolution and polynomial multiplication. > C = CONV(A, B) convolves vectors A and B. The resulting > vector is length LENGTH(A)+LENGTH(B)-1. > If A and B are vectors of polynomial coefficients, convolving > them is equivalent to multiplying the two polynomials. > > > I wrote the following, but is there a way of either of > (1) speeding up the code by changing the algorithm ... > ignoring simple things like the multiple evaluations > of Length and so forth which I have left > in only for what I hope is clarity; or > (2) Using a built in function (possibly connected with polynomials) to do > the same thing? > > Mark R Diamond > No spam email: markd at psy dot uwa dot edu dot au > -------------------------------------------------------- > > convolve[a_List,b_List]:=Module[ > { > (* reverse one of the lists prior to the convolution *) > ra=Reverse[a], > > (* A variable that collects the indices of lists ra and b, > respectively *) > (* that will be Dot[ ]-ed together. *) > indices > }, > > (* Create the table of indices *) > indices=Table[ > { > { > Max[Length[a]+1-i,1], > Min[Length[a],Length[a]+Length[b]-i] > }, > { > Max[1,i-Length[a]+1],Min[Length[b],i] > } > }, > {i,Length[a]+Length[b]-1} > ]; > > (* Create a list of the appropriate pairs of dot products *) > Map[(Take[ra,#[[1]] ].Take[ b,#[[2]] ])&, indices] > ] /; (VectorQ[a,NumberQ]\[And]VectorQ[b,NumberQ]) > > >