Need to make a square pulse w/o a special function
조회 수: 4 (최근 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
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
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
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
2015년 8월 3일
% 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!
Saurabh Sakpal
2015년 8월 4일
댓글 수: 1
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 Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!