Re: Multiple application of LinearFilter

• To: mathgroup at smc.vnet.net
• Subject: [mg64850] Re: Multiple application of LinearFilter
• Date: Sun, 5 Mar 2006 03:19:23 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```On 3/4/06 at 2:35 AM, lrebanks at netvigator.com (Lea Rebanks) wrote:

>I have been using LinearFilter on my data with great success &
>achieve better results the more times I pass my data through the
>correct indexing with PadLeft - but this is not my question here.)

>Below is how I am applying the multiple LinearFilter.

>EG

>originaldata

>Data1=LinearFilter[originaldata, {1/2,1/2}];
>Data2=LinearFilter[Data1, {1/2,1/2}];
>Data3=LinearFilter[Data2, {1/2,1/2}];

>My question is - Is there a shorter way of writing multiple passes
>of the above.

Yes. But rather than using LinearFilter, I think the built-in function ListConvolve is more efficient and flexible. For your specific application, ListConvolve differs from LinearFilter only in the order of the arguments, i.e.,

In[13]:=
LinearFilter[{a,b,c,d},{1/2,1/2}]==ListConvolve[{1/2,1/2},{a,b,c,d}]

Out[13]=
True

Repeated passes with a given kernel are equivalent to a single pass with a longer kernel. That is 3 passes with the kernel (1/2,1/2) give the same result as a single pass with the kernel {1/8,3/8,3/8,1/8}

Here is three passes using ListConvolve and the kernel {1/2,1/2}

In[14]:=
ListConvolve[{1/2, 1/2},
ListConvolve[{1/2, 1/2}, ListConvolve[{1/2, 1/2},
{a, b, c, d, e, f}]]]//Simplify

Out[14]=
{(1/8)*(a + 3*b + 3*c + d), (1/8)*(b + 3*c + 3*d + e),
(1/8)*(c + 3*d + 3*e + f)}

and here is the result using a single pass

In[15]:=
ListConvolve[{1/8, 3/8, 3/8, 1/8}, {a, b, c, d, e, f}]//Simplify

Out[15]=
{(1/8)*(a + 3*b + 3*c + d), (1/8)*(b + 3*c + 3*d + e),
(1/8)*(c + 3*d + 3*e + f)}

As you can see the results are the same.

The real advantage of ListConvolve over LinearFilter is the additional flexibility ListConvolve gives, For example, to get an output the same length as the input you can do

In[18]:=
ListConvolve[{1/2, 1/2},  {a, b, c}, {1, 1}]

Out[18]=
{a/2 + c/2, a/2 + b/2, b/2 + c/2}

or if you wanted the equivalent of