Problem in plotting periodic waves

15 vues (au cours des 30 derniers jours)
Hani Syed
Hani Syed le 10 Nov 2017
Commenté : Star Strider le 15 Nov 2017
Hello, I want to plot simple one period ( square wave whose duty cycle is 25% )that's it with the help of simple for and if statements but its so annoyingly hard in MATLAB. please help me, i shall be grateful. This is my code. WHen i run it i cannot get anything. Yes i am trying and i am relatively new to MATLAB so this matrices thing is so confusing.Thank you.
T=1;
Tp = 0.25;
one = [1 1 1 1 1];
zero = [0 0 0 0 0 ];
x = 0:0.1:0.25;
t=0;
for t= 0:0.25:1
if t < 0.50
A = one;
else
A = zero;
end
end plot (t,A)

Réponses (1)

Star Strider
Star Strider le 10 Nov 2017
The Signal Processing Toolbox has the pulstran (link) funciton.
Otherwise, try this:
p = 20; % Period
d = 0.25; % Duty Cycle
sqwv = @(p,d) [ones(1,ceil(p*d)) zeros(1,p-ceil(p*d))] .* ((d > 0) & (d < 1)); % Square Wave Anonymous Function
pulsetrain = @(n) repmat(sqwv(p,d), 1, n); % Create Pulse Train Of ‘n’ Periods
pt = pulsetrain(5); % Pulse Train
t = 1:length(pt); % Time Vector
figure(1)
plot(t, pt)
set(gca, 'YLim',[-0.1 1.1])
grid
  2 commentaires
Hani Syed
Hani Syed le 15 Nov 2017
But your period is of 20 and x axis is streched to 100. I want my period to be 1 with duty cycle of .25 and 5 periods .
Star Strider
Star Strider le 15 Nov 2017
I don’t see that requirement anywhere in your original Question.
It’s straightforward to adapt my code to do that:
p = 20; % Period
d = 0.25; % Duty Cycle
sqwv = @(p,d) [ones(1,ceil(p*d)) zeros(1,p-ceil(p*d))] .* ((d > 0) & (d < 1)); % Square Wave Anonymous Function
pulsetrain = @(n) repmat(sqwv(p,d), 1, n); % Create Pulse Train Of ‘n’ Periods
pt = pulsetrain(5); % Pulse Train
t = linspace(0, 5, length(pt)); % Time Vector
figure(1)
plot(t, pt)
set(gca, 'YLim',[-0.1 1.1])
grid

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by