How to plot a square wave with specific conditions?

1 回表示 (過去 30 日間)
Moshood Kareem
Moshood Kareem 2015 年 4 月 15 日
編集済み: Moshood Kareem 2015 年 4 月 15 日
Hello,
I need to plot a square wave signal with ones and zeros; the wave must be a 0 when y is <5 and must be a 1 when y >5. The wave must be on the same graph as the y signal (shown below) Any help would be appreciated.
I have the following code as the initial conditions.
clear all
S = [6,4,6,5,0,3,1];
A = sum(S);
B = 2.5;
T = mean(S)/1000000;
f = 1/T;
fs = 100*f;
Ts = 1/fs;
phi = sum(S)*180/pi;
t = 0:Ts:5*T;
y = A*sin(((2*pi)/T)*t+ phi)+B;

採用された回答

pfb
pfb 2015 年 4 月 15 日
That should simply be
ys = (y>5);
plot(t,y,'b',t,ys,'r')
  1 件のコメント
Moshood Kareem
Moshood Kareem 2015 年 4 月 15 日
編集済み: Moshood Kareem 2015 年 4 月 15 日
Thanks that works but what if I want the square wave to be 0 when y < 5 but 1 when y >6, how would I modify the command you gave me to fit the purpose?
Edit: I have manged to figure it out using the command you have given me, thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by