Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Thresholding - vectorization help

13 views
Skip to first unread message

Bill Stones

unread,
Mar 13, 2002, 7:23:02 AM3/13/02
to
Hello,

I'm trying to do thresholding on a signal about 50000 samples long.
I want to get the samples (index) closest to the threshold.
What I've done so far basicaly is

x_above = x>threshold

x_above is a logical vector that looks like:

x_above = [ 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 . . . 0 0 1 1 1 0]

What I want to get is all the indeces at which the signal crosses the
threshold up and then down. That is the index of the first and the last 1 of
each group of 1's. I am testing all the values of x_above in a loop, but it
takes very long to compute. I am looking for help in vectorizing this.

The loop is something like this:
------------------------------------------
LeftBracket =[]; % Start index of each group of 1's
RightBracket = [];% End index of each group of 1's
TZswitch = 0; %This switch indicates if we're in a sequence of 1's or out
for k = 1:length(x_above)
if x_above(k) == 1
if TZswitch==0
% Put current index in LeftBracket and set the switch to ON
LeftBracket = [LeftBracket k];
TZswitch = 1;
end

elseif TZswitch ==1
% Put current index in RightBracket and set the swith to OFF
RightBracket = [RightBracket k];
LZswitch = 0;
end
end
----------------------------------------------------------------------------
-

All help greatly appreciated.


--
The main thing is to keep the main thing the main thing.
H. Beckenridge
-----------------------------------------------------------
N.B. I only check the listed e-mail
from time to time to clean the spam.
-----------------------------------------------------------


us

unread,
Mar 13, 2002, 7:52:20 AM3/13/02
to
why not consider this

1) convert ..010.. double sequence to a STRING
s=sprintf('%s',x_above+'0');
2) now
xup=strfind(s,'01');
xdown=strfind(s,'10');
3) and from here on

us

"Bill Stones" <mail2...@yahoo.com> wrote in message
news:a6ngdm$fqbgg$1...@ID-112827.news.dfncis.de...

us

unread,
Mar 13, 2002, 7:55:49 AM3/13/02
to
... and as we're getting used to look at things this way

xup=strfind(x_above,[char(0) char(1)]);
xdown=strfind(x_above,[char(1) char(0)]);

... just a thought
us

"us" <u...@neurol.unizh.ch> wrote in message
news:3c8f4b84$1...@zinews.unizh.ch...


> why not consider this
> 1) convert ..010.. double sequence to a STRING
> s=sprintf('%s',x_above+'0');

<SNIP>

us

unread,
Mar 13, 2002, 8:01:30 AM3/13/02
to
... which, however, will finally allow us to see why this should work

xup=strfind(x_above,[0 1]);
xdown=strfind(x_above,[1 0]);

us


"us" <u...@neurol.unizh.ch> wrote in message

news:3c8f4c55$1...@zinews.unizh.ch...

Sam Boulton

unread,
Mar 13, 2002, 9:06:23 AM3/13/02
to
Try doing a diff on your x_above vector.

Sam

"us" <u...@neurol.unizh.ch> wrote in message

news:3c8f4daa$1...@zinews.unizh.ch...

John D'Errico

unread,
Mar 13, 2002, 8:57:26 AM3/13/02
to
In article <a6ngdm$fqbgg$1...@ID-112827.news.dfncis.de>, "Bill Stones"
<mail2...@yahoo.com> wrote:

> x_above is a logical vector that looks like:
>
> x_above = [ 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 . . . 0 0 1 1 1 0]
>
> What I want to get is all the indeces at which the signal crosses the
> threshold up and then down. That is the index of the first and the last 1

Why not just do:

find(diff(x_above))

This will give you a string of crossing locations,
alternately up, then down.

John

--

Peter Boettcher

unread,
Mar 13, 2002, 9:45:56 AM3/13/02
to
"Bill Stones" <mail2...@yahoo.com> writes:

How about something like (untested):

LeftBracket = find(diff(x_above) == 1);
RightBracket = find(diff(x_above) == -1);


--
Peter Boettcher <boet...@ll.mit.edu>
MIT Lincoln Laboratory
MATLAB FAQ: http://www.mit.edu/~pwb/cssm/

us

unread,
Mar 13, 2002, 9:55:49 AM3/13/02
to
of course.
i just wanted to alert NGers to the <strfind> stuff because

1) it works with DOUBLES (just another trail of bytes)
- and - more importantly

2) it allows to find more complex patterns, eg
strfind(x_above,[0 1 1 1 0])

that often is much faster than any of the <set member> functions.

us

"Sam Boulton" <sbou...@mathworks.co.uk> wrote in message
news:a6nmbm$3ms$1...@sabina.mathworks.co.uk...

Ken Davis

unread,
Mar 13, 2002, 2:29:22 PM3/13/02
to
Very good point. Thanks! - Ken

"us" <u...@neurol.unizh.ch> wrote in message news:3c8f...@zinews.unizh.ch...

george poonen

unread,
Mar 13, 2002, 9:22:22 AM3/13/02
to
How about


find(diff(x) ~=0)


where x is the vector of 0s and 1s
You may wish to add one to the index depending on
where you want to catch the rising or falling edge

Bill Stones

unread,
Mar 13, 2002, 7:53:28 PM3/13/02
to
Wow! What a long thread for just one night!
Thanks, everyone, for all the help.
Actually, on my way home last night, while waiting at a traffic light to
change, and while nervously tapping on the wheel, it suddenly came upon me
that using DIFF may do the trick. But I would never have thought of using
string comparison to do the job. Thanks for that idea, us.
Thank you all. It's really nice to have such a responsive and helpful NG as
CSSM.
Have a nice day!

--
The main thing is to keep the main thing the main thing.
H. Beckenridge
-----------------------------------------------------------
N.B. I only check the listed e-mail
from time to time to clean the spam.
-----------------------------------------------------------

"Bill Stones" <mail2...@yahoo.com> wrote in message
news:a6ngdm$fqbgg$1...@ID-112827.news.dfncis.de...

0 new messages