Create a long rectangular signal with a function handle

I was wondering if there is a quick way to create a rectangular signal during 20 sec for example, with a function handle. I tried the following command which requires you to do it for every step.
Thanks.
u=@(t) double((0<=t & t<0.5) + ...
(1<=t & t<1.5) + ...
(2<=t & t<2.5)) .* 1;

 채택된 답변

Bruno Luong
Bruno Luong 2022년 4월 20일
편집: Bruno Luong 2022년 4월 20일
myfun = @(t) mod(t,1)<=0.5;
ezplot(myfun, 0, 20)

댓글 수: 4

AEW
AEW 2022년 4월 20일
편집: AEW 2022년 4월 20일
Thanks!
What about you make it bidirectional as follows?
U=@(t) double((0<=t & t<0.25) - (.5<=t & t<.75) + ...
(1<=t & t<1.25) - (1.5<=t & t<1.75) + ...
(2<=t & t<2.25) - (2.5<=t & t<2.75) + ...
(3<=t & t<3.25) - (3.5<=t & t<3.75) + ...
(4<=t & t<4.25) - (4.5<=t & t<4.75)) .* 1;
fun=@(x) interp1(0:0.25:1,[1 0 -1 0 1],mod(x,1),'previous');
ezplot(fun,0,3)
fun=@(x) ppval(mkpp(0:0.25:1,[1; 0; -1; 0]),mod(x,1))
fun = function_handle with value:
@(x)ppval(mkpp(0:0.25:1,[1;0;-1;0]),mod(x,1))
ezplot(fun,0,3)
Thanks a lot!

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

추가 답변 (1개)

Sam Chak
Sam Chak 2022년 4월 20일
Second opinion!
fcn = @(t) (sign(sin(2*pi*t)) + 1)/2;
ezplot(fcn, 0, 5)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

제품

릴리스

R2021b

질문:

AEW
2022년 4월 20일

편집:

AEW
2022년 4월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by