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

조회 수: 4 (최근 30일)
Saurabh Sakpal
Saurabh Sakpal 2015년 8월 3일
댓글: Star Strider 2015년 8월 4일
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