Main Content

Specify Sample Time

Designate Sample Times

Simulink® allows you to specify a block sample time directly as a numerical value or symbolically by defining a sample time vector. Sample time value must be a double or a two-element array of double that contains the sample time period and offset. In the case of a discrete sample time, the vector is [Ts, To] where Ts is the sampling period and To is the initial time offset. For example, consider a discrete model that produces its outputs every two seconds. If your base time unit is seconds, you can directly set the discrete sample time by specifying the numerical value of 2 as the SampleTime parameter. Because the offset value is zero, you do not need to specify it; however, you can enter [2,0] in the Sample time field.

For nondiscrete blocks, the components of the vector are symbolic values that represent one of the types in Types of Sample Time. The following table summarizes these types and the corresponding sample time values. The table also defines the explicit nature of each sample time type and designates the associated color and annotation. Because an inherited sample time is explicit, you can specify it as [-1, 0] or as -1. Whereas, a triggered sample time is implicit; only Simulink can assign the sample time of [-1, -1]. (For more information about colors and annotations, see View Sample Time Information.)

Designations of Sample Time Information

Sample Time TypeSample TimeAnnotationExplicit
Discrete[Ts, To]D1, D2, D3, D4, D5, D6, D7,... DiYes
Continuous[0, 0]ContYes
Fixed in minor step[0, 1]FiMYes
Inherited[–1, 0]N/AYes
Constant[Inf, 0]InfYes
Variable[–2,Tvo]V1, V2,... ViNo
Asynchronous[–1, –n]A1, A2,... AiNo

The color that is assigned to each block depends on its sample time relative to other sample times in the model. This means that the same sample time may be assigned different colors in a parent model and in models that it references. (See Model References.)

For example, suppose that a model defines three sample times: 1, 2, and 3. Further, suppose that it references a model that defines two sample times: 2 and 3. In this case, blocks operating at the 2 sample rate appear as green in the parent model and as red in the referenced model.

It is important to note that Mux and Demux blocks are simply grouping operators; signals passing through them retain their timing information. For this reason, the lines emanating from a Demux block can have different colors if they are driven by sources having different sample times. In this case, the Mux and Demux blocks are color coded as hybrids (yellow) to indicate that they handle signals with multiple rates.

Similarly, Subsystem blocks that contain blocks with differing sample times are also colored as hybrids, because there is no single rate associated with them. If all the blocks within a subsystem run at a single rate, the Subsystem block is colored according to that rate.

You can use the explicit sample time values in this table to specify sample times interactively or programmatically for either block-based or port-based sample times.

The following model, serves as a reference for this section.

In this example, set the sample time of the input sine wave signal to 0.1. The goal is to achieve an output sample time of 0.2. The Rate Transition block serves as a zero-order hold. The resulting block diagram after setting the sample times and simulating the model is shown in the following figure. (The colors and annotations indicate that this is a discrete model.)

ex_specify_sample_time after Setting Sample Times

Specify Block-Based Sample Times Interactively

To set the sample time of a block interactively:

  1. In the Simulink model window, double-click the block. The block parameter dialog box opens.

  2. Enter the sample time in the Sample time field.

  3. Click OK.

Following is a figure of a parameters dialog box for the Sine Wave block after entering 0.1 in the Sample time field.

To specify and inspect block-based sample times throughout a model, consider using the Model Data Editor (on the Modeling tab, click Model Data Editor). On the Inports/Outports, Signals, and Data Stores tabs, set the Change view drop-down list to Design and use the Sample Time column. For more information, see Model Data Editor.

Specify Port-Based Sample Times Interactively

The Rate Transition block has port-based sample times. You can set the output port sample time interactively by completing the following steps:

  1. Double-click the Rate Transition block. The parameters dialog box opens.

  2. Leave the drop-down menu choice of the Output port sample time options as Specify.

  3. Replace the -1 in the Output port sample time field with 0.2.

  4. Click OK.

For more information about the sample time options in the Rate Transition parameters dialog box, see the Rate Transition reference page.

Specify Block-Based Sample Times Programmatically

To set a block sample time programmatically, set its SampleTime parameter to the desired sample time using the set_param command. For example, to set the sample time of the Gain block in the Specify_Sample_Time model to inherited (-1), enter the following command:

set_param('Specify_Sample_Time/Gain','SampleTime','[-1, 0]')

As with interactive specification, you can enter just the first vector component if the second component is zero.


Specify Port-Based Sample Times Programmatically

To set the output port sample time of the Rate Transition block to 0.2, use the set_param command with the parameter OutPortSampleTime:

set_param('Specify_Sample_Time/Rate Transition',...
'OutPortSampleTime', '0.2')

Access Sample Time Information Programmatically

To access all sample times associated with a model, use the API Simulink.BlockDiagram.getSampleTimes.

To access the sample time of a single block, use the API Simulink.Block.getSampleTimes.

Specify Sample Times for a Custom Block

You can design custom blocks so that the input and output ports operate at different sample time rates. For information on specifying block-based and port-based sample times for S-functions, see Specify S-Function Sample Times .

Determining Sample Time Units

Since the execution of a Simulink model is not dependent on a specific set of units, you must determine the appropriate base time unit for your application and set the sample time values accordingly. For example, if your base time unit is second, then you would represent a sample time of 0.5 second by setting the sample time to 0.5.

Change the Sample Time After Simulation Start Time

To change a sample time after simulation begins, you must stop the simulation, reset the SampleTime parameter, and then restart execution.

Related Topics