Need to make a square pulse w/o a special function
2 次查看(过去 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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!