Need to make a square pulse w/o a special function

3 次查看(过去 30 天)
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

采纳的回答

Star Strider
Star Strider 2015-8-3
No loops needed. Just use the repmat funciton.
See if this does what you want:
LP = 10; % Pulse Length
IP = 10; % Inter-Pulse Interval
NC = 3; % Number Of Cycles
PT = repmat([ones(1,LP) zeros(1,IP)], 1, NC); % Complete Pulse Train
TV = 0:length(PT)-1; % Time Vector
figure(1)
plot(TV, PT, 'LineWidth',1.5)
hold on
plot(xlim, [0 0], '-k')
hold off
axis([0 length(TV) -1.25 1.25])
set(gca, 'YTick', [-1 0 1], 'YTickLabel',{'-a', '', 'a'})
ylabel('Volts')
Experiment to get the result you need. The ‘PT’ assignment is the actual pulse train. You can use it in any subsequent code, such as fft or others. I plotted it here because that seems to be what you want.
  2 个评论
Image Analyst
Image Analyst 2015-8-3
repmat() might be banned by the instructor also.
And a "for" loop is so trivial that I don't even think we need to give an example for a for loop. Same for "if" statements. If he can't even do a for loop or if block, then this link might be helpful: http://www.mathworks.com/matlabcentral/answers/8026-best-way-s-to-master-matlab
Star Strider
Star Strider 2015-8-3
Possibly. We may not know everything we need to about this problem.
If repmat is not allowed, the repmat call could easily be replaced by a single for loop with a serial horizontal concatenation at every iteration.

请先登录,再进行评论。

更多回答(3 个)

Geoff Hayes
Geoff Hayes 2015-8-3
Saurabh - why not use the sine and sign functions to do this? You could try something like
% 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 个评论
Saurabh Sakpal
Saurabh Sakpal 2015-8-4
I want it to be "Aperiodic" and unsymettrical as well. Like o for 20 seconds, off for 80 seconds.
Is there any function that does this?

请先登录,再进行评论。


Saurabh Sakpal
Saurabh Sakpal 2015-8-4
Sorry i want to apologise, i AM allowed to use function. I just wanted to avoid them as it is easier to modify parts of the code rather than a pre built fucntion. But feel free to use functions.

Saurabh Sakpal
Saurabh Sakpal 2015-8-4
Hey guys its not working, as i the repmat function doesnt go as a function of time. I need to pass the pulse as a function of time. Still Stuck.
  1 个评论
Star Strider
Star Strider 2015-8-4
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by