Finding indices of first maximum value and last minimum value of a square wave
4 views (last 30 days)
Show older comments
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!!!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/272078/image.jpeg)
0 Comments
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 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!