Square wave with values 0 to 1 rather than -1 to 1

11 views (last 30 days)
Hi,
I know that the square(t) function produces a wave with an amplitude from -1 to 1, is it possible to produce it from 0 to 1 instead?
Thanks

Answers (2)

madhan ravi
madhan ravi on 5 Dec 2018
t = linspace(0,1).';
x = square(t);
  1 Comment
Michael Au-Duong
Michael Au-Duong on 5 Dec 2018
Not sure that works in the way that I'd want it to. For example, the code that I'm working with
freq = 1.5669*10^03;
timex = 0:0.01:0.5;
y = square(2*pi*freq.*timex, 25);
figure
plot(timex,y)
The code produces a square wave with amps of 1 and -1, i'm looking to get that to be 1 and 0 instead.
Thanks

Sign in to comment.


Anamitra Datta
Anamitra Datta on 10 Apr 2021
The above answers are wrong. It should be:
Assuming your period is 2*pi
x = 0.5*square(t)+0.5
This produces a square wave of 1 to 0 though in a cycle
If you need 0 to 1 in a cycle, you should do:
x = 0.5*square(t-pi)+0.5

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!