Manipulate amplitude without using linspace

조회 수: 1 (최근 30일)
Aaron8298
Aaron8298 2021년 1월 23일
편집: Adam Danz 2021년 1월 23일
I have a signal which I converted to a square wave however, the amplitude needs to be set in a range of -2 to 1 without using linspace. Here is the code I have so far
t=0:0.1:2*pi
x_t1=sin(2*pi*t)+3*cos(6*pi*t);
x_t=square((x_t1/0.5),25)-1;%creates square wave with a period of 0.5 and duty cycle of 25 percent, subtracting 1 makes amplitude -2 to 0
plot(x_t)
I know the square function automatically makes the amplitude range -1 to 1 but I am stuck on how else to manipulate the code. Thank you for the help

채택된 답변

Adam Danz
Adam Danz 2021년 1월 23일
편집: Adam Danz 2021년 1월 23일
Since square returns values in the range of [-1,1], rescaling to [-2,1] is as easy as multiplying by 1.5 and subtracting 0.5 which is ~4x faster than rescale().
t=0:0.1:2*pi;
x_t1=sin(2*pi*t)+3*cos(6*pi*t);
x_t=square((x_t1/0.5),25)*1.5-.5;
% ^^^^^^^
plot(x_t)

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 1월 23일
Use rescale():
x_t = rescale(x_t, -2, 1);
  댓글 수: 1
Aaron8298
Aaron8298 2021년 1월 23일
Thank you for your assistance, it works

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by