MATLAB Answers

# Finding indices of first maximum value and last minimum value of a square wave

12 views (last 30 days)
Zuha Yousuf on 17 Feb 2020
Answered: ME on 17 Feb 2020
So I have a square wave (shown in the plot below, as lots of tiny square waves that aren't really visible because of the scale), and I want to find the index where the very first square wave has its upward deflection, and where the very last square wave has its downward deflection (shown by the two arrows below). the whole array has a size of 312960960 by 1. Could someone suggest code that might help finding the indices of the first upward deflection, and the last downward deflection, and cropping the entire array to just give me that little range in the middle? Will really appreciate!!! ##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (1)

ME on 17 Feb 2020
You could use
f = find ( y > threshold )
to find the indices corresponding to all values that exceed some threshold value. Then, the maximum and minimum indices found will be the start and end points you are looking for - so you can then use
y_new = y( min( f ) : max( f ) )
and similar if you have a time array also.
You could also expand that out a little bit if you wanted to plot from just outside this range to see it take it's upturn by using something like
y_new = y( min( f ) - n : max( f ) + n )
where n is a number of time-steps by which the range will be expanded in each direction.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!