Effacer les filtres
Effacer les filtres

How to plot shifting square pulse

6 vues (au cours des 30 derniers jours)
David Bustamante
David Bustamante le 18 Avr 2020
Commenté : Star Strider le 19 Avr 2020
I cant figure out how to plot a graph of a square pulse shifting to the right across the x axis. This is currently what I have, but it just outputs a singular square wave, and I want a moving plot that shifts to the right with a step of 0.5. Any help is appreciated, thank you!
x = linspace(-5,5,10000);
pulse = rect(x,2);
%plot(x,pulse);
tau = 0;
for k = 1:length(x)
pause(3)
plot(x - tau,pulse);
drawnow;
tau = tau + 0.5;
end
function a = rect(t,len)
x = zeros(length(t));
for i=1:length(t)
if abs(t(i)) > len/2.0
x(i) = 0;
else
x(i) = 1;
end
end
a = x;
end

Réponse acceptée

Star Strider
Star Strider le 19 Avr 2020
It is difficult to understand your code.
Here is a slightly simpler version:
x = linspace(-5, 5, 1000);
pulse = [0 ones(1,20) 0];
figure
for k = 1:numel(x)-numel(pulse)
plot(x(k:k+numel(pulse)-1), pulse)
axis([min(x) max(x) 0 1.5])
drawnow
end
Adapt it to do what you want.
Have fun with it!
.
  2 commentaires
David Bustamante
David Bustamante le 19 Avr 2020
That's exactly what I wanted thank you!
Star Strider
Star Strider le 19 Avr 2020
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Tags

Produits


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by