How to make a square wave of bits from a ones and zeros vector?

9 views (last 30 days)
Hello everyone!
I'll really appreciate your help with this little problem.
I have a vector, to say something X, which has only 1's and 0's (bits), and I want to get a plot from this vector with a square shape. I would like to give to this plot the period of each bit to make a square wave... because if I plot only the vector X, I'll get peaks and not squares or pulses.
Thanks for your support!
I'll be reading your comments.

Accepted Answer

Walter Roberson
Walter Roberson on 14 Feb 2021
x = randi([0 1], 1, 10)
x = 1×10
0 0 1 1 1 1 0 0 1 0
stairs(x); ylim([-0.1 1.1])
Not like that? You need the bits to be separated ?
duty = 0.8;
xx = reshape([x; zeros(size(x))],1,[]);
t = [0:length(x)-1];
tt = reshape([t;t+duty], 1, []);
stairs(tt, xx); ylim([-.1 1.1])
  1 Comment
Carlos Alfredo Vega Aguilar
Thank you so much!
That's what I was looking for!
I haven't accepted your answer before because I had problems with my computer and I couldn't check your code before but, it really works!

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots 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!