# Weighted Sample Time

Support calculations involving sample time

• Libraries:

## Description

The Weighted Sample Time block outputs one of these values, depending on settings of the Operation and Weight value parameters and the execution context of the function that contains the block:

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

• Weighted sample rate –- When used in a periodic function, the weighted sample rate is the reciprocal of the weighted sample time or ```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.

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`

## Ports

### Input

expand all

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

Data Types: `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 (`Ts` times the value specified for the Weight value parameter `w`), or ```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: `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64` | `Boolean` | `fixed point`

## Parameters

expand all

### Main

Specify the operation to use for adjusting the value of 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: `TsamMathOp` Type: character vector Values: ```'+' | '-' | '*' | '/' | 'Ts Only' | '1/Ts Only'``` Default: `'Ts Only'`

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 * w`Output Data Type of Two ModesBlock Execution
A power of 2, 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'` | `''` Default: `'Inherit: Inherit via internal rule'`

Specify the rounding mode for fixed-point operations. For more information, see Rounding (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'`

## Block Characteristics

 Data Types `Boolean` | `double` | `fixed point` | `integer` | `single` Direct Feedthrough `yes` Multidimensional Signals `no` Variable-Size Signals `no` Zero-Crossing Detection `no`

## Version History

Introduced before R2006a