Need to make a square pulse w/o a special function
Show older comments
hi everyone,
really stuck here, need to make a periodic square (step pulse) that turns on to a certain value at a time, and turns off again. I need to make a code for it as i cant use a prebuilt function like square etc. Preferably use for loops and if statements etc.
can someone help me
1 Comment
Saurabh Sakpal
on 3 Aug 2015
Accepted Answer
More Answers (3)
Geoff Hayes
on 3 Aug 2015
% create an array of 10000 elements linearly spaced from 0 to 10
% (can consider this a time vector of then seconds)
t = linspace(0,10,10000);
% set the amplitude
a = 2.0;
% create the square pulse
y = (a/2)*sign(sin(2*pi*t)) + (a/2);
plot(t,y);
As the result of
sign(sin(2*pi*t))
is an array of values between -1 and +1, then in order to get the range as shown in your figure (which is the interval [0 a]) we just multiply the result by a/2 and then add a/2. Try the above and see what happens!
1 Comment
Saurabh Sakpal
on 4 Aug 2015
Saurabh Sakpal
on 4 Aug 2015
0 votes
Saurabh Sakpal
on 4 Aug 2015
0 votes
1 Comment
Star Strider
on 4 Aug 2015
What does ‘its not working’ mean? We cannot guess what you want. You must tell us as specifically as you can.
My repmat code produces a single string of 1s and 0s of whatever length and ratio you want for as many periods as you want. You can specify them individually.
To define it as a function of time, define a time vector for it. The linspace function is perfect for this.
Categories
Find more on MATLAB 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!