Main Content

Weighted Sample Time Math

Support calculations involving sample time

  • Weighted Sample Time Math block

Libraries:
Simulink / Math Operations

Description

The Weighted Sample Time block outputs a weighted sample time, weighted sample rate, or mathematically adjusted weighted sample time, depending on settings of the Operation and Weight value parameters and the execution context of the function outputs.

You specify the block operation by using the Operation parameter.

The sample time used to derive the output depends on whether the block is used within a periodic or aperiodic (or triggered) execution context. For a periodic execution context, the block uses the sample time of the model. For an aperiodic or triggered execution context, the block uses the sample time of the discrete signal.

Use the Weight value parameter to specify the sample time weight factor. If the weight value, w, is 1, that value does not appear in the equation on the block icon.

Tip

You can use the Weighted Sample Time and Weighted Sample Time Math blocks to extract the sample time from a Simulink® signal. Set the Operation parameter to Ts Only and the Weight value parameter to 1.0. With this configuration, the block outputs the sample time of the input signal.

The block computes its output by using the precedence rules for MATLAB® operators. For example, if the Operation parameter is set to +, the block calculates the output by using this expression:

u + (Ts * w)

If the Operation parameter is set to /, the block calculates the output by using this expression:

(u / Ts) / w)

For more information, see Operator Precedence.

Examples

expand all

This example shows how to add the sample time value to a signal using the Weighted Sample Time Math block.

Using the Weighted Sample Time block, you can see the sample time of this model is 0.2. When you set the Operation parameter to + and the Weight value to 1 on the Weighted Sample Time Math block, the block adds the sample time value of 0.2 to the input signal. When you set the Weight value to 3 in the Weighted Sample Time Math1 block, the block adds Ts*3 to the input signal, thus increasing each value by 0.6.

Ports

Input

expand all

Specify input signal as a scalar, vector, or matrix.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean | fixed point

Output

expand all

Depending on settings of the Operation and Weight value parameters and the execution context of the function that contains the block, the block outputs a weighted sample time, weighted sample rate, or mathematically adjusted weighted sample time.

  • Weighted sample time –- When used in a periodic function, the weighted sample time is the sample time of an input signal times the value specified for the Weight value parameter (Ts * w). When used in an aperiodic or triggered function, the weighted sample time is the elapsed time since the last execution times the weight value.

  • Weighted sample rate –- When used in a periodic function, the weighted sample rate is the reciprocal of the weighted sample time (1/(Ts * w)). The weighted sample rate is not relevant when the block is used in an aperiodic function.

  • Mathematically adjusted weighted sample time –- The block adds the value of an input signal u to, subtracts u from, or multiplies or divides u by a weighted sample time.

The sample time used to derive the output and the data type of the output depend on whether the block is used within a periodic or aperiodic (or triggered) execution context.

Execution Context of BlockSample Time Used to Derive OutputData Type of Output Signal
PeriodicSample time of the model
  • Sign is the same sign as the value of the input signal.

  • Word length is the larger value of the input signal word length or half of a long long.

  • Scaling is best precision scaling of the weighted sample time.

Aperiodic or triggeredElapsed time since the last execution
  • Data type is unsigned.

  • Word length is large enough to account for the setting of model configuration parameter Application lifespan.

  • Scaling is best precision scaling of the setting of model configuration parameter Clock resolution.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | Boolean | fixed point

Parameters

expand all

Main

Specify the operation to use for adjusting the input signal. For example, if you specify the + operation, the block adds the weighted sample time to the value of the input signal. The value 1/Ts Only is not relevant when you use the block in an aperiodic function.

Programmatic Use

Block Parameter: TsampMathOp
Type: character vector
Values: '+' | '-' | '*' | '/' | 'Ts Only' | '1/Ts Only'
Default: '+'

Enter the weight for the sample time as a real-valued scalar. The block multiplies the sample time of the input signal by the value that you specify. For example, if you use the block in a periodic function of a model that has a sample time of 0.2 and you specify a weight value of 3, the weighted sample time is 0.6.

Programmatic Use

Block Parameter: weightValue
Type: character vector
Values: real-valued scalar
Default: 1.0

Select whether to adjust the sample time by applying online calculations or by applying offline scaling.

Result of Ts * wOutput Data Type of Two ModesBlock Execution
Power of 2 (positive or negative) or an integer value The same when Output data type is set to Inherit: Inherit via internal rule Equally efficient in both modes
Not a power of 2 and not an integer valueDifferentMore efficient for the offline scaling mode

Note

When the Implement using parameter is not enabled, operations default to online calculations.

Dependencies

To enable this parameter, set Operation to * or /.

Programmatic Use

Block Parameter: TsampMathImp
Type: character vector
Values: 'Online Calculations' | 'Offline Scaling Adjustment'
Default: 'Online Calculations'

Signal Attributes

Specify the data type for the block output signal.

Programmatic Use

Block Parameter: OutDataTypeStr
Type: character vector
Values: 'Inherit: Inherit via internal rule' | 'Inherit: Inherit via back propagation' | double | single | int8 | uint8 | int16 | uint16 | int32 | uint32 | int64 | uint64 | fixdt(1,16,0) | fixdt(1,16,20,0) | '<data type expression>'
Default: 'Inherit: Inherit via internal rule'

Specify the rounding mode for fixed-point operations. For more information, see Rounding Modes (Fixed-Point Designer).

Block parameters round to the nearest representable value. To control the rounding of a block parameter, enter an expression by using a MATLAB rounding function in the mask field.

Dependencies

To enable this parameter, set the Operation parameter to +, -, *, or /. If you set the Operation to * or /, you must also set the Implement using parameter to Online Calculations.

Programmatic Use

Block Parameter: RndMeth
Type: character vector
Values: 'Ceiling' | 'Convergent' | 'Floor' | 'Nearest' | 'Round' | 'Simplest' | 'Zero'
Default: 'Floor'

Specify whether integer overflows saturate or wrap.

GoalActionOverflow BehaviorExample

Model has possible overflow conditions and you want explicit saturation protection in the generated code.

Select check box (on).

Saturate to the minimum or maximum value that the data type can represent.

The maximum value that the int8 (signed, 8-bit integer) data type can represent is 127. A block operation result greater than this maximum value causes overflow of the 8-bit integer. When you select this parameter, the block output saturates at 127. Similarly, the block output saturates at a minimum output value of -128.

You want to optimize generated code.

You want to avoid overspecifying how a block handles out-of-range signals. For more information, see Troubleshoot Signal Range Errors.

Clear check box (off).

Wrap overflows to the appropriate value that is representable by the data type.

The maximum value that the int8 (signed, 8-bit integer) data type can represent is 127. A block operation result greater than this maximum value causes overflow of the 8-bit integer. When you clear this parameter, the software interprets the overflow-causing value as int8, which can produce an unintended result. For example, a block result of 130 (binary 1000 0010) expressed as int8, is -126.

When you select Saturate on integer overflow, saturation applies to every internal operation on the block, not just the output, or result. When the code generator detects that overflow is not possible, it does not produce saturation code.

Dependencies

To enable this parameter, set the Operation parameter to +, -, *, or /. If you set Operation to * or /, you must also set the Implement using parameter to Online Calculations.

Programmatic Use

Block Parameter: SaturateOnIntegerOverflow
Type: character vector
Values: 'off' | 'on'
Default: 'off'

Select how you would like to specify the data type properties of the Output data type. You can choose:

  • Inherit — Specify a rule for inheriting a data type, for example, Inherit: Same as input.

  • Built in— Specify a built-in data type, for example, double.

  • Fixed point — Specify the fixed-point attributes of the data type.

  • Expression — Specify an expression that evaluates to a valid data type, for example, fixdt(1,16).

For more information, see Specify Data Types Using Data Type Assistant.

Specify whether the fixed-point data is signed or unsigned. Signed data can represent positive and negative values, but Unsigned data represents positive values only. For more information, see Specify Data Types Using Data Type Assistant.

Specify the method for scaling your fixed-point data to avoid overflow conditions and minimize quantization errors. For more information, see Specify Data Types Using Data Type Assistant.

Dependencies

To enable this parameter, set Mode to Fixed point

Specify the bit size of the word that holds the quantized integer as a positive integer from 2 to 128. For more information, see Specify Data Types Using Data Type Assistant.

Dependencies

To enable this parameter, set Mode to Fixed point.

Specify the fraction length of the fixed-point data type as a positive or negative integer scalar.

Dependencies

To enable this parameter, set Mode to Fixed point and Scaling to Binary point.

Specify slope for the fixed-point data type. For more information, see Specifying a Fixed-Point Data Type.

Dependencies

To enable this parameter, set Scaling to Slope and bias.

Specify bias for the fixed-point data type as any real number. For more information, see Specifying a Fixed-Point Data Type.

Dependencies

To enable this parameter, set Scaling to Slope and bias.

Select the data type override mode for this signal. You can select:

  • Inherit — Inherits the data type override setting specified for the model.

  • Off — Ignores the data type override setting specified for the model and uses the fixed-point data type you specify.

Dependencies

To enable this parameter, set Mode to Built in or Fixed point. For more information, see Specify Data Types Using Data Type Assistant.

Block Characteristics

Data Types

Boolean | double | fixed point | integer | single

Direct Feedthrough

yes

Multidimensional Signals

no

Variable-Size Signals

no

Zero-Crossing Detection

no

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.

Fixed-Point Conversion
Design and simulate fixed-point systems using Fixed-Point Designer™.

Version History

Introduced before R2006a

expand all