MATLAB Answers

I want to create a square wave shows in the Picture

1 view (last 30 days)
Zida Wang
Zida Wang on 9 Jun 2021
Answered: Steven Lord on 9 Jun 2021
Hi All,
I would like to create a square wave as shown in the figure, which start at 1 ends at 0, has opposite polarity at same peroid. Frequency is 32 Hz and sample rate is 32768.

Answers (2)

Adam Danz
Adam Danz on 9 Jun 2021
  • Define y as the step value at each step (row vector).
  • Define dx as the x-interval (scalar).
  • Define x0 as the starting x-coordinate (scalar).
% inputs
y = [1 0 -1 0];
dx = 1;
x0 = 0;
% Create step function
x = x0 + cumsum(dx*ones(size(y)));
xr = [x(1),repelem(x(2:end),1,2)]; % X is row vec
yr = [repelem(y(1:end-1),1,2),y(end)]; % Y is row vec
% Plot results
axis padded
grid on

Steven Lord
Steven Lord on 9 Jun 2021
Looking at the Plots tab of the Toolstrip, the thumbnail that looks closest to the picture that you posted is the stairs function. Take a look at the documentation page for that function and see if it suits your needs.




Community Treasure Hunt

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

Start Hunting!