help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: filtering logical array


From: Robert T. Short
Subject: Re: filtering logical array
Date: Wed, 27 Jun 2012 09:24:41 -0700
User-agent: Mozilla/5.0 (X11; Linux i686; rv:13.0) Gecko/20120614 Thunderbird/13.0.1

On 06/27/2012 08:12 AM, seektime wrote:

y = (conv(a,conv([1 1 1],[1 1 1]))>5)(3:end-2)


Hi Bob and Mike,

Thanks for your input. I want to learn more about convolution - obviously
it's a neat and elegant (and probably fast) way of coding and execution.

However, after playing around for some time I still fail to see the
underlying rules to configure the three constants (5, 3, 2) in the above
line of code. The logical array a I provided was only an example. In reality
I'm applying the code on much larger arrays, with considerably longer
strings of 1's. So "y" needs to be calculated for strings of 20-100 1's. The
solution I provided works for me. But it'll be great if you give me a hint
on what rule the constants follow for convolution.
Thanks again,
Michael

Michael,

What you observed was a couple octave geeks trying to create a one-line solution to a problem. Sometimes I wonder about a language in which efficiency relies so much on tricks (think APL) but when it is available you look for such solutions.

First, convolution isn't necessarily fast, but it is easy (at least if you have the conv routine available). For this simple case though

y=conv(x,[1 1 1])

creates a running sum of the elements of the elements of x, i.e. y(n) = x(n) + x(n-1) + x(n-2). The second convolution

z = conv(y,[1 1 1])

creates a running sum of y, and convolution is commutative/associative (etc.) so we can convolve the running sum elements first.

So, to filter a string of L ones, try

f1 = ones(1,L);
f   = conv(f1,f1);
T  = 2*L-1;
y  = (conv(x,f)>T)(L:end-L+1)

or, as a one-liner

y = (conv(x,conv(ones(1,L),ones(1,L)))>=2*L)(L:end-L+1)


I didn't actually try this - it very likely has syntax or other small errors, but I think the idea should work.

Bob


reply via email to

[Prev in Thread] Current Thread [Next in Thread]