Generating a single pulse in time?
23 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi I would like to write a simple code for a single pulse.So for example: if I would like to see pulse in the time period: 400<t<500
Why does If (t>400&&t<500) s=2.0 else s=0.0 end
When I plot this I get: a result where s=0 for all time and no pulse.
but
if (t>400&&t<500) s=2.0; else s=0.01; end
a pulse with a baseline of 0.01 (and maximum value 2.0)?
Thank you for your help.
0 commentaires
Réponse acceptée
David Sanchez
le 28 Nov 2013
Your tspan is
tspan=[0,1000];
it should be:
tspan=0:1000;
You will get this plot:
Plus de réponses (5)
Wayne King
le 28 Nov 2013
Modifié(e) : Wayne King
le 28 Nov 2013
You did not tell us the sampling frequency so I'll just assume it is 1 Hz in my example.
t = 0:1:1000;
s = 0.01*ones(length(t));
s(t>400 & t<500) = 2;
plot(t,s)
Note that in this case you could just do the simpler (assuming the first sample is t=0):
s = 0.01*ones(10001,1);
s(402:500) = 2;
but the example I have should work when the sampling frequency is not 1.
2 commentaires
Talha Khalid
le 9 Nov 2020
@Wayne King
I just tested your answer. It doesn't generate the required result. See the attached picture
David Sanchez
le 28 Nov 2013
It is easier to do:
t = 0:1000;
s = zeros(length(t));
s(t>400 & t<500) = 2;
plot(t,s)
Wayne King
le 28 Nov 2013
See my earlier answer for how to do it.
Using your if statement approach, when t is a vector how do you expect the if statement to evaluate (t> 400 && t<500)?
To get that approach to work you'd have to loop through the t-values like this
s = zeros(1000,1);
t = 1:1000;
for nn = 1:length(t)
tval = t(nn);
if (tval > 400 && tval<500)
s(nn) = 2;
else
s(nn) = 0.01;
end
end
plot(t,s)
But that is not an efficient use of MATLAB's inherit array operations. Use the approach I gave you earlier.
0 commentaires
Voir également
Catégories
En savoir plus sur Arithmetic Operations 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!