MATLAB Answers

Create a binary signal

40 views (last 30 days)
Rafael Rafael
Rafael Rafael on 21 Mar 2019
Commented: Adam Danz on 22 Mar 2019
Hello, anyone have any ideia how can I make this signal in a plot? The time must be between -3 and 3 with a range of 1ms.
Thanks

  0 Comments

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 22 Mar 2019
Edited: Adam Danz on 22 Mar 2019
This solution uses the square() funciton which converts a sin wave into a square wave with normalized values between [-1:1]. All you have to do is create a sin wave that fits your description, convert it to square wave, and then vertically scale it.
% Set params
width = 1.0; % Width of each step
height = 1.0; % Height of step (max - min)
base = 0.0; % min value
start = -3; % starting x value
stop = 3; % ending x value
res = 0.001; % resolution
horzShift = .5; % horizontally shift (negative = leftward)
% Compute square wave
x = start : res : stop;
y = (square(sin((x+horzShift)/width*pi))+1)/2*height + base;
% figure
plot(x,y, 'k-', 'linewidth', 3)
ylim([base - 1, base+height+1])

  2 Comments

Rafael Rafael
Rafael Rafael on 22 Mar 2019
Oh thanks, that is an inteligent solution :)
Adam Danz
Adam Danz on 22 Mar 2019
Glad it worked for you!

Sign in to comment.

More Answers (0)

Sign in to answer this question.