Gain

Multiply input by constant

  • Library:
  • Simulink / Commonly Used Blocks

    Simulink / Math Operations

    HDL Coder / Commonly Used Blocks

    HDL Coder / HDL Floating Point Operations

    HDL Coder / Math Operations

  • Gain block

Description

The Gain block multiplies the input by a constant value (gain). The input and the gain can each be a scalar, vector, or matrix.

You specify the value of gain in the Gain parameter. The Multiplication parameter lets you specify element-wise or matrix multiplication. For matrix multiplication, this parameter also lets you indicate the order of the multiplicands.

Gain is converted from doubles to the data type specified in the block mask offline using round-to-nearest and saturation. The input and gain are then multiplied, and the result is converted to the output data type using the specified rounding and overflow modes.

Ports

Input

expand all

The Gain block accepts real or complex-valued scalar, vector, or matrix input. The Gain block supports fixed-point data types. If the input of the Gain block is real and gain is complex, the output is complex.

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

Output

expand all

The Gain block outputs the input multiplied by a constant gain value. When the input to the Gain block is real and gain is complex, the output is complex.

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

Parameters

expand all

Main

Specify the value by which to multiply the input. Gain can be a real or complex-valued scalar, vector, or matrix.

Programmatic Use

Block Parameter: Gain
Type: character vector
Values: '1' | real- or complex-valued scalar, vector, or matrix
Default: '1'

Specify one of these multiplication modes:

  • Element-wise(K.*u) — Each element of the input is multiplied by each element of the gain. The block performs expansions, if necessary, so that the input and gain have the same dimensions.

  • Matrix(K*u) — The input and gain are matrix-multiplied with the input as the second operand.

  • Matrix(u*K) — The input and gain are matrix-multiplied with the input as the first operand.

  • Matrix(K*u) (u vector) — The input and gain are matrix multiplied with the input as the second operand. This mode is identical to Matrix(K*u), except for how dimensions are determined.

    Suppose that K is an m-by-n matrix. Matrix(K*u)(u vector) sets the input to a vector of length n and the output to a vector of length m. In contrast, Matrix(K*u) uses propagation to determine dimensions for the input and output. For an m-by-n gain matrix, the input can propagate to an n-by-q matrix, and the output becomes an m-by-q matrix.

Programmatic Use

Parameter: Multiplication
Type: character vector
Value: 'Element-wise(K.*u)' | 'Matrix(K*u)' | 'Matrix(u*K)' | 'Matrix(K*u) (u vector)'
Default: 'Element-wise(K.*u)'

Specify the sample time as a value other than -1. For more information, see Specify Sample Time.

Dependencies

This parameter is not visible unless it is explicitly set to a value other than -1. To learn more, see Blocks for Which Sample Time Is Not Recommended.

Programmatic Use

Block Parameter: SampleTime
Type: character vector
Values: scalar or vector
Default: '-1'

Signal Attributes

Lower value of the output range that Simulink® checks.

Simulink uses the minimum to perform:

Note

Output minimum does not saturate or clip the actual output signal. Use the Saturation block instead.

Programmatic Use

Block Parameter: OutMin
Type: character vector
Values: '[ ]'| scalar
Default: '[ ]'

Upper value of the output range that Simulink checks.

Simulink uses the maximum value to perform:

Note

Output maximum does not saturate or clip the actual output signal. Use the Saturation block instead.

Programmatic Use

Block Parameter: OutMax
Type: character vector
Values: '[ ]'| scalar
Default: '[ ]'

Choose the data type for the output. The type can be inherited, specified directly, or expressed as a data type object such as Simulink.NumericType.

When you select an inherited option, the block exhibits these behaviors:

  • Inherit: Inherit via internal rule — Simulink chooses a data type to balance numerical accuracy, performance, and generated code size, while taking into account the properties of the embedded target hardware. If you change the embedded target settings, the data type selected by the internal rule might change. For example, if the block multiplies an input of type int8 by a gain of int16 and ASIC/FPGA is specified as the targeted hardware type, the output data type is sfix24. If Unspecified (assume 32-bit Generic), in other words, a generic 32-bit microprocessor, is specified as the target hardware, the output data type is int32. If none of the word lengths provided by the target microprocessor can accommodate the output range, Simulink software displays an error in the Diagnostic Viewer.

  • Inherit: Keep MSB– Simulink chooses a data type that maintains the full range of the operation, then reduces the precision of the output to a size appropriate for the embedded target hardware.

    Tip

    For more efficient generated code, deselect the Saturate on integer overflow parameter.

    This rule never produces overflows.

  • Inherit: Match scaling– Simulink chooses a data type whose scaling matches the scaling of the input types. If the full range of the type does not fit on the embedded target hardware, the range is reduced yielding a type appropriate for the embedded target hardware. This rule can produce overflows.

    It is not always possible for the software to optimize code efficiency and numerical accuracy at the same time. If these internal rules do not meet your specific needs for numerical accuracy or performance, use one of the following options:

    • Specify the output data type explicitly.

    • Use the simple choice of Inherit: Same as input.

    • Explicitly specify a default data type such as fixdt(1,32,16) and then use the Fixed-Point Tool to propose data types for your model. For more information, see fxptdlg (Fixed-Point Designer).

    • To specify your own inheritance rule, use Inherit: Inherit via back propagation and then use a Data Type Propagation block. Examples of how to use this block are available in the Signal Attributes library Data Type Propagation Examples block.

  • Inherit: Inherit via back propagation — Use data type of the driving block.

  • Inherit: Same as input — Use data type of input signal.

Programmatic Use

Block Parameter: OutDataTypeStr
Type: character vector
Values: 'Inherit: Inherit via internal rule' | 'Inherit: Keep MSB' | 'Inherit: Match scaling' | 'Inherit: Same as input' | 'Inherit: Inherit via back propagation' | 'single' | 'half' | 'int8' | 'uint8' | 'int16' | 'uint16' | 'int32' | 'uint32' | 'int64' | 'uint64' | 'fixdt(1,16)' | 'fixdt(1,16,0)' | 'fixdt(1,16,2^0,0)' | '<data type expression>'
Default: 'Inherit: Inherit via internal rule'

Select this parameter to prevent the fixed-point tools from overriding the Output data type you specify on the block. For more information, see Use Lock Output Data Type Setting (Fixed-Point Designer).

Programmatic Use

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

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

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

Programmatic Use

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

Specify whether overflows saturate or wrap.

ActionRationaleImpact on OverflowsExample

Select this check box (on).

Your model has possible overflow, and you want explicit saturation protection in the generated code.

Overflows saturate to either 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. Any block operation result greater than this maximum value causes overflow of the 8-bit integer. With the check box selected, the block output saturates at 127. Similarly, the block output saturates at a minimum output value of -128.

Do not select this check box (off).

You want to optimize efficiency of your generated code.

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

Overflows wrap 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. Any block operation result greater than this maximum value causes overflow of the 8-bit integer. With the check box cleared, 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 this check box, saturation applies to every internal operation on the block, not just the output, or result. Usually, the code generation process can detect when overflow is not possible. In this case, the code generator does not produce saturation code.

Programmatic Use

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

Parameter Attributes

Specify the minimum value of gain. The default value is [] (unspecified). Simulink uses this value to perform:

Programmatic Use

Block Parameter: ParamMin
Type: character vector
Value: scalar
Default: '[ ]'

Specify the maximum value of gain. The default value is [] (unspecified). Simulink uses this value to perform:

Programmatic Use

Block Parameter: ParamMax
Type: character vector
Value: scalar
Default: '[ ]'

Specify the data type of the Gain parameter.

Programmatic Use

Block Parameter: ParamDataTypeStr
Type: character vector
Values: 'Inherit: Inherit via internal rule | 'Inherit: Same as input' | 'Inherit: Inherit via back propagation' | 'single' | 'half' | 'int8' | 'uint8' | 'int16' | 'uint16', 'int32' | 'uint32' | 'int64' | 'uint64' | 'fixdt(1,16)' | 'fixdt(1,16,0)' | 'fixdt(1,16,2^0,0)' | '<data type expression>'
Default: 'Inherit: Inherit via internal rule'

Block Characteristics

Data Types

Boolean | double | fixed point | half | integer | single

Direct Feedthrough

yes

Multidimensional Signals

yes

Variable-Size Signals

yes

Zero-Crossing Detection

no

Extended Capabilities

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

PLC Code Generation
Generate Structured Text code using Simulink® PLC Coder™.

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

Introduced before R2006a