# Variable-size signals and Unit Delay

I received requests recently about variable-size signals and the Unit Delay block.

What should a Unit Delay do when the size of a signal changes?

Unfortunately, there is not only one correct answer to this question. Let's say I have a Unit Delay block and its input signal changes from a size of 3 to a size of 2, what should the output be? When should the dimension of the output change?

Let's look at the options you have.

MathWorks Solution

As explained in Variable-Size Signal Basics, blocks supporting variable-size signals can propagate sizes in one of two ways:

• At every step of model execution
• During initialization of a conditionally executed subsystem

How do you know into which category a specific block is? A good rule is that, most blocks with states fall in the second category. To ensure consistency and avoid confusion, those blocks are allowed to propage sizes only during the initialization of a conditionally executed system.

Let's look at an example

Here I create a variable-size signal by alternating between 3 constants and 2 random numbers. If I want to apply a Unit Delay to this signal, I need to place it inside a Conditionally Executed Subsystem and ensure that the subsystem is enabled and disabled every time the dimension change. Here I do that using a Stateflow Chart:

The result is that every time the dimension changes, the state of the Unit Delay is reset to its initial condition, zero in this example:

My Alternative Solution

For many reasons, one might need a Unit Delay block who works with variable-size signals, but who does not need to be placed inside a conditionally executed subsystem. To help you getting started with that, I created technical solution 1-ET7RPB. I am not going into the details here, but this solution contains the MATLAB and C versions of my alternative version of a Unit Delay block implemented as an S-Function.

To process a signal similar to the previous example, this Unit Delay block does not need to be placed inside a conditionally executed subsystem:

Instead, the dimension of the output changes one time step after the dimension of the input:

If you are interested, you can download all the files in this example from technical solution 1-ET7RPB.