Triangular pulse with function handle t(@)

Hi! I have a rectangular pulse thas is given by this function handle: @(t)( (t>=0).*(t<pulseDuration) )
And I need to change this function handle to create a triangular pulse. Any ideas?

14 件のコメント

Michael
Michael 2020 年 11 月 2 日
Hi Laura,
There are lots of ways to approach this. Have you given any attempt to a solution, by incorporating additional variables, or how you would like to design the triangular pulse? These will be important in solving the problem.
Michael
Laura Ferrer Pascual
Laura Ferrer Pascual 2020 年 11 月 3 日
Hi Michael,
I just need to implement a single triangular pulse where I could change the pulse duration. This is what I've tried:
@(t)( ((t>=0).*(t<(pulseDuration)).*(t/(pulseDuration))));
With this I obtain a sawtooth pulse
Bjorn Gustavsson
Bjorn Gustavsson 2020 年 11 月 3 日
If that gives you half of your desired triangular pulse, what would you need to add to that to get a triangular pulse, step by step.
Stephan
Stephan 2020 年 11 月 3 日
If you have access to Symbolic Toolbox you could use the inbuilt triangularPulse function:
syms x
f = matlabFunction(triangularPulse(x))
plot((-1:1),f(-1:1))
Laura Ferrer Pascual
Laura Ferrer Pascual 2020 年 11 月 3 日
Bjorn, I don't know how to construct the other half. Maybe is something obvious but I've been trying and I can't obtain it.
Bjorn Gustavsson
Bjorn Gustavsson 2020 年 11 月 3 日
Laura, so you have the "up-slope" half of your triangular pulse (from [0, 0] to [1, 1]), right? From the point at [1,1] you want a line-segment back down to [0,2]? If you try to write an expression for that along similar lines as your initial function, how would you go about that? Is it enough to change the less-than and larger-than or equal conditionals? What more do you need to modify?
If you manage to construct such a "down-slope" part, can you add them toghether? Straighaway or with some additional adjustment?
Laura Ferrer Pascual
Laura Ferrer Pascual 2020 年 11 月 3 日
For the up-slope trianngular pulse I have this:
  • t>0
  • t<(pulseDuration)/2
  • slope: t/pulseDuration
For the down-slope I have:
  • t>0
  • t<pulseDuration
  • t>(pulseDuration)/2
  • slope: -t/pulseDuration
All this makes the following code:
@(t)((t>0).*(t<pulseDuration).*(t/pulseDuration).*((t<(pulseDuration*0.5))-(t>(pulseDuration*0.5))))
But when this does not give a triangular pulse :( It gives something strange. Maybe is because I don't have to sum the two parts of the function?
Laura Ferrer Pascual
Laura Ferrer Pascual 2020 年 11 月 3 日
By the way, I've never used Matlab
Bjorn Gustavsson
Bjorn Gustavsson 2020 年 11 月 3 日
It's perfectly OK to be a beginner - everyone have been a beginner at everything they started with.
Your first up-slope function was correct for that part.
For the down-slope part you're correct about all points (and your first condition that t > 0 is extra, since the third point ensures that it is larger than pulseDuration/2 which is also larger than zero.), but in addition you have to think about the vertical off-set at time pulseDuration/2, or perhaps the intersection between the y-axis at time zero.
After that I think you will put the down-slope part together correctly. Then it is just to add them together, perhaps you will have to make sure that only one of the terms are non-zero at t=pulseDuration/2.
HTH
Laura Ferrer Pascual
Laura Ferrer Pascual 2020 年 11 月 3 日
Thank you for your help Bjorn, it's always easier to start when you have help.
One question, I cannot write t=pulseDuration/2 at the code because I will be defining t. So I don't know exactly how to define the maximum point of the triangular point at pulseDuration/2.
James Tursa
James Tursa 2020 年 11 月 3 日
編集済み: James Tursa 2020 年 11 月 3 日
Is the max height of the triangle pulse 1? Do you have to use a one-liner function handle or can you use a function file? It might be easier for you to write a function file for this instead of trying to come up with a one-liner. You can always create a function handle that points to the function file if needed.
Laura Ferrer Pascual
Laura Ferrer Pascual 2020 年 11 月 3 日
Yes, the max height of the triangle pulse is 1. I think I have to use a one-liner function handle and anyway I don't know how to use a function file (or what is it hahahah)
Bjorn Gustavsson
Bjorn Gustavsson 2020 年 11 月 3 日
This is a task that is so much easier to handle in a .m-file where you can write a function using all of matlab's programming capabilities: conditionals (if-elseif-else-end), loops (for and while-loops) and on and on. The definition of dynamical/anonymous functions is way trickier, and doing this with multiple cases easily becomes a mess of parenthesises and operator precedences bungled up. If we take a look at your first function
ft1 = @(t)((t>=0).*(t<(pulseDuration)).*(t/(pulseDuration)));
You first define the lower and upper time-limits of the up-slope segment and then the shape of the function in that range.
If we modify that to just be half the length we get something like below:
ft2a = @(t)(t>0).*(t<pulseDuration/2).*(t/(pulseDuration/2));
Just a modified upper limit and a modified linear segment.
You are allowed to add multiple such segments together, if you just define their different ranges in time to give you what you want. So if we do that and add another segment that doesn't overlap you get something like this:
ft2b = @(t)(t>0).*(t<pulseDuration/2).*(t/(pulseDuration/2)) + (t>=pulseDuration/2).*(t<pulseDuration).*(some-line-equation);
I'll let you doodle-out what your linear equation in the second term should be, and since your pulseDuration is already defined you're allowed to use it as much as you need in the function. If you want you can also use pulseDuration as a second input argument to the function which make the function more adaptable to triangular pulses of different widths.
Laura Ferrer Pascual
Laura Ferrer Pascual 2020 年 11 月 3 日
Thank you very much! I finally did it !!

サインインしてコメントする。

 採用された回答

James Tursa
James Tursa 2020 年 11 月 3 日
編集済み: James Tursa 2020 年 11 月 3 日

0 投票

If you must use a one-liner function handle, the general form could be this:
f = @(t) (t >= 0).*(t < pulseDuration/2).*(expression1) + (t >= pulseDuration/2).*(t < pulseDuration).*(expression2)
You would need to fill in the two places I have marked as expression1 and expression2. The general idea is to have this done in pieces, with each piece having the form ( ).*( ).*(expression). The first two ( ).*( ) simply generates a logical array that has 1's where t is valid for the associated expression. You can do this for as many different segments as needed ... in your case two.
The first expression1 would be a line segment that starts at (0,0) and goes to (pulseDuration/2,1).
The second expression2 would be a line segment that starts at (pulseDuration/2,1) and goes to (pulseDuration,0).
See if you can figure out what these two different expressions should be. Both should be expressions involving t and pulseDuration. You might simply draw these line segments out on a piece of paper and then use standard techniques to figure out the expressions. Or you could Google how to generate the expression for a line given two points on the line and do this once for each segment.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeTime Series Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by