Guy on Simulink

Simulink & Model-Based Design

The If-Else Construct in Models

Balaji Kalluri asked a question in the comments of a post about the MUX block.  Balaji asked:

Hi All
I would be highly obliged, if someone can show me a route to model a typical 2-to-1 Multiplexer, the way we have read & understood it. I want to translate behavior of typical digital 2-to-1 Mux works like this into SIMULINK model.

if(sel==0)
out = in1;
elseif (sel==1)
out = in2;

Today I want to look at one way to solve this question.

Controlling which signal passes through (Switch)

One way to think about this problem is to control which signal gets passed through the system.  The switch block provides a way to pass through one signal or another based on the value of a control signal.

Model of conditional pass through using a switch.

This creates an if-expression: when the conditional statement is true, out is set equal to the first inport, otherwise, out is set equal to the third inport.

Optimizing the model

This represents the desired logic, but there is another way to think about the problem.  Consider that both in1 and in2 are the result of some other expensive calculations.  When this is the case, you will want to perform only one of the calculations needed for your output expression instead of always computing both values and then discarding the one that isn’t needed.

Luckily, this is a common pattern and Simulink provides an optimization to improve efficiency.  By default, Conditional input branch execution is turned On (in the Configuration Parameters -> Optimization page).

Conditional Input Branch Execution optimization from Simulink configuration parameters. 

This optimization allows Simulink to group the upstream blocks together and tuck them into the conditional statement of the Switch block.  You may be able to understand the behavior if you turn on the block sorted order display, but I prefer to look at the code for simple models like this.

Comparison of generated code with and without conditional input branch execution optimization.

This optimization is not just limited to the Switch blocks.  Other blocks that produce conditional expressions can incorporate upstream and downstream blocks in their code.

I want to end this discussion with two important notes.

  1. I made up this example specifically to demonstrate the effect of this optimization.  Because it is so simple, I had to disable Expression Folding, or else the code would be the same, no matter what.  I do not encourage anyone disable expression folding without good reason.
  2. Because this is an optimization, you cannot guarantee this behavior.  If you add something as simple as a Test point between the Gain and Switch Block this will disable the optimization.

There are other ways to model efficient conditional behavior and explicitly guard the execution of an algorithm.  I will leave that topic for a future post.

Now it’s your turn

Do you think about the efficiency of your models?  How do you use the Switch block?  Leave a comment here and share what you know.

|
  • print

コメント

コメントを残すには、ここ をクリックして MathWorks アカウントにサインインするか新しい MathWorks アカウントを作成します。