Specify Sample Time
Designate Sample Times
金宝app®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
,
] whereTo
is the sampling period andTs
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 ofTo
2
as theSampleTime
parameter. Because the offset value is zero, you do not need to specify it; however, you can enter [2,0
] in theSample timefield.
For nondiscrete blocks, the components of the vector are symbolic values that represent one of the types inTypes 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 aninherited sample timeis 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, seeView Sample Time Information.)
Designations of Sample Time Information
Sample Time Type | Sample Time | Color | Annotation | Explicit |
---|---|---|---|---|
Discrete | [Ts, To] | In descending order of speed: red, green, blue, light blue, dark green, orange | D1, D2, D3, D4, D5, D6, D7,... Di | Yes |
Continuous | [0, 0] | black | Cont | Yes |
Fixed in minor step | [0, 1] | gray | FiM | Yes |
Inherited | [–1, 0] | N/A | N/A | Yes |
Constant | [Inf, 0] | magenta | Inf | Yes |
Variable | [–2,Tvo] | brown | V1, V2,... Vi | No |
控制able | [base, -2i], i = 0, 1, 2, …, | brown | Ctrl1, Ctrl2, Ctrl3, …. Ctrli | Yes |
Hybrid | N/A | yellow | N/A | No |
Triggered | Source: D 1, Source: D2, ...Source: Di |
cyan | T1, T2,... Ti | No |
Asynchronous | [–1, –n] | purple | A1, A2,... Ai | No |
Dataflow | N/A | light purple | N/A | No |
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. (SeeModel References.)
For example, suppose that a model defines three sample times:1
,2
, and3
. Further, suppose that it references a model that defines two sample times:2
and3
. In this case, blocks operating at the2
sample rate appear as green in the parent model and as red in the referenced model.
It is important to note thatMuxandDemuxblocks are simply grouping operators; signals passing through them retain their timing information. For this reason, the lines emanating from aDemuxblock can have different colors if they are driven by sources having different sample times. In this case, theMuxandDemuxblocks 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_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 to0.1
. The goal is to achieve an output sample time of0.2
. TheRate Transitionblock 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:
In the Simulink model window, double-click the block. The block parameter dialog box opens.
Enter the sample time in theSample timefield.
ClickOK.
Following is a figure of a parameters dialog box for theSine Waveblock after entering0.1
in theSample timefield.
To specify and inspect block-based sample times throughout a model, consider using the Model Data Editor (on theModelingtab, clickModel Data Editor). On theInports/Outports,Signals, and数据存储tabs, set theChange viewdrop-down list toDesign
and use theSample Timecolumn. For more information, seeModel Data Editor.
Specify Port-Based Sample Times Interactively
TheRate Transitionblock has port-based sample times. You can set the output port sample time interactively by completing the following steps:
Double-click theRate Transitionblock. The parameters dialog box opens.
Leave the drop-down menu choice of theOutput port sample time optionsas
Specify
.Replace the
-1
in theOutput port sample timefield with0.2
.ClickOK.
For more information about the sample time options in the Rate Transition parameters dialog box, see theRate Transitionreference page.
Specify Block-Based Sample Times Programmatically
To set a block sample time programmatically, set itsSampleTime
parameter to the desired sample time using theset_param
command. For example, to set the sample time of theGainblock 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 theRate Transitionblock to0.2
, use theset_param
command with the parameterOutPortSampleTime
:
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 APISimulink.BlockDiagram.getSampleTimes
.
To access the sample time of a single block, use the APISimulink.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, seeSpecify 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 of0.5
second by setting the sample time to0.5
.
Change the Sample Time After Simulation Start Time
To change a sample time after simulation begins, you must stop the simulation, reset theSampleTime
parameter, and then restart execution.