Re: how to generate pulse function ?
- To: mathgroup at smc.vnet.net
- Subject: [mg41919] Re: how to generate pulse function ?
- From: wself at msubillings.edu (Will Self)
- Date: Tue, 10 Jun 2003 04:46:51 -0400 (EDT)
- References: <bc1k0c$bo3$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
"sabrina" <mystery at newbie.com> wrote in message news:<bc1k0c$bo3$1 at smc.vnet.net>... > Hello all: > I use the following code to generate the pulse function, but it is not > right: > > u[x_,step_,nPulse_]:= Module[ > {tlist={},func}, > tlist=Table[t,{t,0,nPulse*step,step}]; > func=1-UnitStep[Apply[Times,(x-#)&/@tlist]]; > Return[func]; > ]; > s=u[t, 500,4]; > Plot[s,{t,0,2200}]; > > > The problem is when the nPulse is odd number, it shows the right answer, but > when the nPulse is even, the plot is not right. Would you please help me to > make the function right? Thanks a lot! > > Sabrina I gather that you want nPulse to be the number of pulses. To use your code, you will just have to keep odd and even separate: u[x_, step_, nPulse_] := 1 - UnitStep[Apply[Times, (# - x) & /@ Table[t, {t, 0, If[OddQ[nPulse], nPulse + 2, nPulse + 3]*step, step}]]] Plot[u[t, 500, 3], {t, 0, 5000}]; Plot[u[t, 500, 4], {t, 0, 5000}]; It may be more intuitively evident if you make one pulse and then translate it as many times as needed: onePulse[x_, step_] := UnitStep[x] - UnitStep[x - step] Plot[onePulse[t, 500], {t, 0, 5000}]; uu[x_, step_, nPulse_] := Sum[onePulse[x - 2k*step, step], {k, 0, nPulse - 1}] Plot[uu[t, 500, 3], {t, 0, 5000}]; Plot[uu[t, 500, 4], {t, 0, 5000}]; This is slower, but unless you are generating a great many pulses, it would work fine. If you need speed with a lot of pulses, use the following, which is faster than either of the others: uuu[x_, step_, nPulse_] := 0 /; x >= 2step*nPulse uuu[x_, step_, nPulse_] := (1 + (-1)^Floor[x/step])/2 Plot[uuu[x, 500, 3], {x, 0, 5000}]; Plot[uuu[x, 500, 4], {x, 0, 5000}];