Home > Net >  how to create a pulse train using anonymous function - Matlab
how to create a pulse train using anonymous function - Matlab

Time:07-05

I have a pulse shape that is a function of t, call it h(t), that is in the form of:

h = @(t) function of t

I want to create a pulse train that is consisted of N pulses h(t). I did this by:

for n=0:N-1
    comb = comb   h(t - n);
end

However, once I do that I can't change t in the train. How can I create this train so that it will also be a function of t? Thanks.

CodePudding user response:

You just need to make comb an anonymous function too. You can initialise it to some trivial function (i.e. it always outputs 0), and then repeatedly modify it. Since variables declared before an anonymous function declaration, including anonymous functions, are kind of "frozen" to the definition at that point this will work.

h = @(t) _______ % some function of 't'
comb = @(t) 0;

for n = 0:N-1
    comb = @(t) comb(t)   h(t - n);
end

We can test this with h = @(t) sin(t) and N=3:

>> comb(pi/2)
ans = 1.1242

>> h(pi/2)   h(pi/2-1)   h(pi/2-2)
ans = 1.1242

Note that just displaying comb can be a bit misleading, since information about the recursive definition is somewhat lost

disp(comb)
  @(t)comb(t) h(t-n)
  • Related