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

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)

t = linspace(0,1).';
x = square(t);

1 Comment

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.

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

Asked:

on 5 Dec 2018

Answered:

on 10 Apr 2021

Community Treasure Hunt

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

Start Hunting!