## Types of Sample Time

### Discrete Sample Times

Given a block with a discrete sample time, Simulink® software executes the block output or update method at times

${t}_{n}=n{T}_{s}+|{T}_{o}|$,

where the sample time period ${T}_{s}$ is always greater than zero and less than the simulation time ${T}_{sim}$. The number of periods ($n$) is an integer that must satisfy

$0\le n\le \frac{{T}_{sim}}{{T}_{s}}$.

As simulation progresses, the software computes block outputs only once at each of these fixed time intervals of ${t}_{n}$. These simulation times at which the software executes the output method of a block for a given sample time are called sample time hits. Discrete sample times are the only type for which sample time hits are known a priori.

If you need to delay the initial sample hit time, you can define an offset, ${T}_{o}$.

The Unit Delay block is an example of a block with a discrete sample time.

#### Controllable Sample Time

You can configure a block to use a controllable sample time with a resolution Tbase. Tbase is the smallest allowable time interval between block executions. To set Tbase in your own C S-Function block, use the `ssSetControllableSampleTime` function.

When a block uses controllable sample time, you can dynamically configure the block to execute at n multiples of Tbase. The time of the next block execution is

 Tnext = n Tbase + T (1)

You can set n in your C S-Function block using the `ssSetNumTicksToNextHitForControllableSampleTime` function.

### Continuous Sample Times

#### Continuous Sample Time

Continuous sample time hits are divided into major time steps and minor time steps. Minor time steps are subdivisions of the major time steps. The solver produces a result at each major time step. The solver uses results at the minor time steps to improve the accuracy of the result at the major time step.

The ODE solver you choose integrates all continuous states from the simulation start time to a given major or minor time step. The solver determines the times of the minor steps and uses the results at the minor time steps to improve the accuracy of the results at the major time steps. You see the block output only at the major time steps.

To specify continuous sample time for a block, such as the Derivative block, for the Sample time parameter, enter `[0, 0]` or `0`.

#### Fixed-in-Minor-Step Samle Time

If the sample time of a block is [`0 1`], the block has fixed-in-minor-step sample time. For this sample time, the block does not execute at the minor time steps. The block executes only at major time steps. Fixed-in-minor-step sample time eliminates unnecessary computations of blocks with outputs that cannot change between major steps.

While you can explicitly set a block to fixed-in-minor-step sample time, more often the software sets this condition as either an inherited sample time or as an alteration to a specification of continuous sample time. Fixed-in-minor-step sample time is equivalent to the fastest discrete rate in a system that uses a fixed-step solver. When you use a fixed-step solver, fixed-in-minor-step sample time is converted to the fastest discrete sample time.

#### Variable Sample Time

Blocks that use a variable sample time have an implicit sample time parameter that the block specifies. The block tells the software when it executes. The compiled sample time is [`–2` `Tvo`], where `Tvo` is a unique variable offset.

The Hit Scheduler block and the Pulse Generator block both have variable sample time. Variable sample time is supported only for variable-step solvers. The Hit Scheduler block is not supported for fixed-step solvers. When you use a fixed-step solver to simulate a model that contains a Pulse Generator block, the block specifies a discrete sample time.

To learn how to write your own block that uses a variable sample time, see C MEX S-Function Examples.

### Events and Asynchronous Sample Times

#### Asynchronous Sample Time

An asynchronous sample time is similar to a triggered sample time. In both cases, you need to specify an inherited sample time because the Simulink engine does not regularly execute the block. Instead, a runtime condition determines when the block executes. For an asynchronous sample time, an S-function makes an asynchronous function call.

The differences between these sample time types are:

• Only a function-call subsystem can have an asynchronous sample time. See Using Function-Call Subsystems.

• The source of the function-call signal is an S-function that has the option `SS_OPTION_ASYNCHRONOUS`.

• The asynchronous sample time can also occur when a virtual block is connected to an asynchronous S-function or an asynchronous function-call subsystem.

• The asynchronous sample time is important to certain code generation applications. See Asynchronous Events (Simulink Coder).

• The sample time is `[-1,-n]`.

For an explanation of how to use blocks to model and generate code for asynchronous event handling, see Rate Transitions and Asynchronous Blocks (Simulink Coder).

#### Aperiodic Partitions

Aperiodic partitions in the model introduce this type of sample time. Aperiodic partitions have no constraints and can be made to execute at any time. You can specify hit times in the Schedule Editor to drive the timeline of aperiodic partitions. Events can determine the execution timing of the blocks that have this sample time specified. Subsystem blocks can have an aperiodic as a specified sample time parameter.

### Other Sample Times

#### Inherited Sample Time

If a block sample time is set to `[–1 0]` or `–1`, the sample time is inherited, and software determines the best sample time for the block based on the context of the block within the model. The software determines the sample time for blocks with inherited sample time during compilation. Because the inherited setting is overwritten in compilation, the Sample Time Legend never shows inherited sample time [`-1 0`] in a compiled model. For more information, see View Sample Time Information.

Some blocks inherit sample time by default. For these blocks, the parameter is not visible unless you specify a non-inherited value. For example, the Gain and Rounding Function blocks do not have a visible sample time parameter and have inherited sample time by default. As a best practice, do not change the Sample time parameter for these blocks. For more information, see Blocks for Which Sample Time Is Not Recommended.

All inherited blocks are subject to the process of sample time propagation. For more information, see How Propagation Affects Inherited Sample Times.

#### Constant Sample Time

In Simulink software, a constant is a symbolic name or expression whose value you can change only outside the algorithm or through supervisory control. Blocks whose outputs do not change during normal execution of the model, such as the Constant block, are always considered to be constant.

The software assigns constant sample time to these blocks. They run the block output method:

• At the start of a simulation

• In response to runtime changes in the environment, such as tuning a parameter

For constant sample time, the block sample time assignment is `[inf 0]` or `inf`.

For a block to allow constant sample time, the block must not have continuous or discrete states and must not drive an output port of a conditionally executed subsystem. For more information, see Using Enabled Subsystems.

The Simulink block library includes several blocks whose ports can produce outputs at different sample rates, such as the MATLAB S-Function block, the Level-2 MATLAB S-Function block, and the C S-Function block. Some ports of these blocks can have a constant sample time.

#### Triggered Sample Time

If a block is inside a triggered subsystem, such as a function-call subsystems or enabled subsystem, the block may be constant or have a triggered sample time, except in the case of an asynchronous function call. You cannot specify the triggered sample time type explicitly. To achieve a triggered sample time during compilation, set the block sample time to inherited (`–1`). The software then determines the specific times at which the block executes during simulation.

### Multirate Sample Times

#### Union Rate

Union sample times are container sample times that are a union of the component sample times. The execution for these sample times, is the union of the time-lines of the component sample times.

#### Multirate Sample Time

Multirate sample time is introduced in the model by the Simulink engine. An Update Diagram turns the subsystem or any multi-rate block (for example, Rate Transition block) yellow because the blocks contain more than one sample time.

While assessing a system for multiple sample times, the software does not consider either constant `[inf, 0]` or model-wide events. Thus, a subsystem that contains one block with a constant value output, and one block with a discrete sample time is not be designated as multirate. If blocks have a continuous and fixed-in-minor-step sample times, these blocks are also not designated as multirate because the timeline for these sample times is the same. Such blocks are designated as having a continuous sample time.

### Dataflow Sample Time

Blocks that have data flow as their sample time is data driven. For example, Dataflow Subsystem (DSP System Toolbox) block. This block uses synchronous dataflow as a model of computation, which is data-driven and statically scheduled. For more information, see Dataflow Subsystem (DSP System Toolbox).

### Initialize, Reinitialize, Terminate Sample Times

You can add custom routines to the default routines using Initialize Function, Reinitialize Function, and Terminate Function blocks to change or read block states. You can also implement custom routines using Reset Function blocks. Initialize, reset, terminate sample times are specified when the Initialize Function, Reinitialize Function, and Terminate blocks are present in a model. These blocks contain an algorithm or semantics that introduces this sample time in the model.