Documentation Center

  • Trial Software
  • Product Updates

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. 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 TimeColorAnnotationExplicit
Discrete[Ts, To]In descending order of speed: red, green, blue, light blue, dark green, orangeD1, D2, D3, D4, D5, D6, D7,... DiYes
Continuous[0, 0]blackContYes
Fixed in minor step[0, 1]grayFiMYes
Inherited[–1, 0]N/AN/AYes
Constant[Inf, 0]magentaInfYes
Variable[–2,Tvo]brownV1, V2,... ViNo
HybridN/AyellowN/ANo
TriggeredSource: D1, Source: D2, ...Source: DicyanT1, T2,... TiNo
Asynchronous[–1, –n]purpleA1, 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 Reference.)

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, ex_specify_sample_timeex_specify_sample_time, serves as a reference for this section.

ex_specify_sample_time

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.

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.

set_param('Specify_Sample_Time/Gain','SampleTime','-1')

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 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.

Was this topic helpful?