Effacer les filtres
Effacer les filtres

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

4 vues (au cours des 30 derniers jours)
Saurabh Sakpal
Saurabh Sakpal le 3 Août 2015
Commenté : Star Strider le 4 Août 2015
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

Réponse acceptée

Star Strider
Star Strider le 3 Août 2015
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 commentaires
Image Analyst
Image Analyst le 3 Août 2015
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 le 3 Août 2015
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.

Connectez-vous pour commenter.

Plus de réponses (3)

Geoff Hayes
Geoff Hayes le 3 Août 2015
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 commentaire
Saurabh Sakpal
Saurabh Sakpal le 4 Août 2015
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?

Connectez-vous pour commenter.


Saurabh Sakpal
Saurabh Sakpal le 4 Août 2015
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 le 4 Août 2015
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 commentaire
Star Strider
Star Strider le 4 Août 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by