필터 지우기
필터 지우기

Find Middle of square wave

조회 수: 7 (최근 30일)
Russell Senior
Russell Senior 2020년 4월 2일
댓글: Star Strider 2020년 4월 2일
I can find the rising edge and falling edge of a square wave, any thoughts on how to find the middle of an arbitrary square wave? I created a simple code using a logical array that finds the rising edges and falling edges, but I can't think of a good way to find the middle of the square. Ideas?
Edit: I'd like to avoid using a for loop. Speed is important since this will be used to process a few GB of data.
clear all
close all
t = 0:100;
x = false(size(t));
x(2:10) = true;
x(25:30) = true;
x(50:75) = true;
x(90:end) = true;
rising = x > [x(1) x(1:end-1)];
falling = x < [x(1) x(1:end-1)];
subplot(3,1,1)
plot(t,x)
subplot(3,1,2)
plot(t,rising,'r')
subplot(3,1,3)
plot(t,falling,'k')

채택된 답변

Star Strider
Star Strider 2020년 4월 2일
Use the islocalmax function (R2017b and later):
This code plots green upward-pointing triangles at the centre of each pulse:
t = 0:100;
x = false(size(t));
x(2:10) = true;
x(25:30) = true;
x(50:75) = true;
x(90:end) = true;
rising = x > [x(1) x(1:end-1)];
falling = x < [x(1) x(1:end-1)];
ctr = islocalmax(x, 'FlatSelection','center'); % Added
subplot(3,1,1)
plot(t,x)
hold on
plot(t(ctr), x(ctr), 'g^') % Added
hold off
subplot(3,1,2)
plot(t,rising,'r')
subplot(3,1,3)
plot(t,falling,'k')
I did not add them to the last two subplot axes. Copy the hold and plot calls to them if you want to plot them there as well.
  댓글 수: 2
Russell Senior
Russell Senior 2020년 4월 2일
That is exactly what I was looking for. Thanks for that.
Star Strider
Star Strider 2020년 4월 2일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by