How do I plot a periodic square-wave for 10 cycles, 5 cycles in advance of t=0 and 5 cycles to the right of t=0 using a unit step function?

5 ビュー (過去 30 日間)
I am trying to plot a square wave using a unit step function.

採用された回答

Chaya N
Chaya N 2016 年 10 月 19 日
編集済み: Chaya N 2016 年 10 月 19 日
A square wave is more accurately an impulse function (that is also periodic), which is the first derivative of a step function. To define the function, you need a time index for the rising edge and another time index for the falling edge.
For example, to plot one cycle of a square wave with a rising edge at t = 0 and a falling edge at t = 0.5, the function would be f[t] = u[t] - u[t-0.5].
Below I am including a very simple code to show two full cycles of your square wave for (-1 < t <= 0) and (0 < t <= 1). Please note the format and sign of the time stamps and how the individual cycles are grouped inside the square brackets (which are not required in the code but only meant as illustration for the grouping here). You can expand upon this and hopefully construct your required wave.
t = linspace(-2, 2, 1000); % This defines the time scale in the plot
f = @(t) [(t > 0) - (t > 0.5)] + [(t > -1) - (t > -0.5)];
stairs(t, f(t));
The plot from the above function looks like the figure below.
Good luck!

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by