# Gain

Multiply input by constant

Math Operations

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

The gain is converted from doubles to the data 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.

## Data Type Support

The Gain block accepts a real or complex scalar, vector, or matrix of any numeric data type that Simulink® supports. The Gain block supports fixed-point data types. If the input of the Gain block is real and the gain is complex, the output is complex.

## Parameters and Dialog Box

The Main pane of the Gain block dialog box appears as follows:

The Signal Attributes pane of the Gain block dialog box appears as follows:

The Parameter Attributes pane of the Gain block dialog box appears as follows:

### Gain

Specify the value by which to multiply the input.

#### Settings

 Default: 1 Minimum: value of Parameter minimum parameter Maximum: value of Parameter maximum parameter

The gain can be a scalar, vector, or matrix.

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Multiplication

Specify the multiplication mode.

#### Settings

Default: `Element-wise(K.*u)`

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

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Sample time

 Note:   This parameter is not visible in the block dialog box unless it is explicitly set to a value other than `-1`. To learn more, see Blocks for Which Sample Time Is Not Recommended

### Output minimum

Lower value of the output range that Simulink checks.

#### Settings

Default: `[]` (unspecified)

Specify this number as a finite, real, double, scalar value.

 Note:   If you specify a bus object as the data type for this block, do not set the minimum value for bus data on the block. Simulink ignores this setting. Instead, set the minimum values for bus elements of the bus object specified as the data type. For information on the Minimum parameter for a bus element, see `Simulink.BusElement`.

Simulink uses the minimum to perform:

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

#### Command-Line Information

 Parameter: `OutMin` Type: string Value: `'[ ]'` Default: `'[ ]'`

### Output maximum

Upper value of the output range that Simulink checks.

#### Settings

Default: `[]` (unspecified)

Specify this number as a finite, real, double, scalar value.

 Note:   If you specify a bus object as the data type for this block, do not set the maximum value for bus data on the block. Simulink ignores this setting. Instead, set the maximum values for bus elements of the bus object specified as the data type. For information on the Maximum parameter for a bus element, see `Simulink.BusElement`.

Simulink uses the maximum value to perform:

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

#### Command-Line Information

 Parameter: `OutMax` Type: string Value: `'[ ]'` Default: `'[ ]'`

### Output data type

Specify the output data type.

#### Settings

Default: ```Inherit: Inherit via internal rule```

`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)`, i.e., 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.

It is not always possible for the software to optimize code efficiency and numerical accuracy at the same time. If the internal rule doesn't 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`.

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

`double`

Output data type is `double`.

`single`

Output data type is `single`.

`int8`

Output data type is `int8`.

`uint8`

Output data type is `uint8`.

`int16`

Output data type is `int16`.

`uint16`

Output data type is `uint16`.

`int32`

Output data type is `int32`.

`uint32`

Output data type is `uint32`.

`fixdt(1,16,0)`

Output data type is fixed point `fixdt(1,16,0)`.

`fixdt(1,16,2^0,0)`

Output data type is fixed point `fixdt(1,16,2^0,0)`.

`<data type expression>`

Use a data type object, for example, `Simulink.NumericType`.

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Mode

Select the category of data to specify.

#### Settings

Default: `Inherit`

`Inherit`

Inheritance rules for data types. Selecting `Inherit` enables a second menu/text box to the right. Select one of the following choices:

• `Inherit via internal rule` (default)

• `Inherit via back propagation`

• `Same as input`

`Built in`

Built-in data types. Selecting `Built in` enables a second menu/text box to the right. Select one of the following choices:

• `double` (default)

• `single`

• `int8`

• `uint8`

• `int16`

• `uint16`

• `int32`

• `uint32`

`Fixed point`

Fixed-point data types.

`Expression`

Expressions that evaluate to data types. Selecting `Expression` enables a second menu/text box to the right, where you can enter the expression.

#### Dependency

Clicking the Show data type assistant button enables this parameter.

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Data type override

Specify data type override mode for this signal.

#### Settings

Default: `Inherit`

`Inherit`

Inherits the data type override setting from its context, that is, from the block, `Simulink.Signal` object or Stateflow® chart in Simulink that is using the signal.

`Off`

Ignores the data type override setting of its context and uses the fixed-point data type specified for the signal.

#### Tip

The ability to turn off data type override for an individual data type provides greater control over the data types in your model when you apply data type override. For example, you can use this option to ensure that data types meet the requirements of downstream blocks regardless of the data type override setting.

#### Dependency

This parameter appears only when the Mode is ```Built in``` or `Fixed point`.

### Signedness

Specify whether you want the fixed-point data as signed or unsigned.

#### Settings

Default: `Signed`

`Signed`

Specify the fixed-point data as signed.

`Unsigned`

Specify the fixed-point data as unsigned.

#### Dependencies

Selecting Mode > ```Fixed point``` enables this parameter.

### Word length

Specify the bit size of the word that holds the quantized integer.

#### Settings

Default: `16`

Minimum: `0`

Maximum: `32`

#### Dependencies

Selecting Mode > ```Fixed point``` enables this parameter.

### Scaling

Specify the method for scaling your fixed-point data to avoid overflow conditions and minimize quantization errors.

#### Settings

Default: ```Binary point```

`Binary point`

Specify binary point location.

`Slope and bias`

Enter slope and bias.

#### Dependencies

Selecting Mode > ```Fixed point``` enables this parameter.

Selecting `Binary point` enables:

• Fraction length

• Calculate Best-Precision Scaling

Selecting `Slope and bias` enables:

• Slope

• Bias

• Calculate Best-Precision Scaling

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Fraction length

Specify fraction length for fixed-point data type.

#### Settings

Default: `0`

Binary points can be positive or negative integers.

#### Dependencies

Selecting Scaling > ```Binary point``` enables this parameter.

### Slope

Specify slope for the fixed-point data type.

#### Settings

Default: `2^0`

Specify any positive real number.

#### Dependencies

Selecting Scaling > ```Slope and bias``` enables this parameter.

### Bias

Specify bias for the fixed-point data type.

#### Settings

Default: `0`

Specify any real number.

#### Dependencies

Selecting Scaling > ```Slope and bias``` enables this parameter.

### Lock output data type setting against changes by the fixed-point tools

Select to lock the output data type setting of this block against changes by the Fixed-Point Tool and the Fixed-Point Advisor.

#### Settings

Default: Off

On

Locks the output data type setting for this block.

Off

Allows the Fixed-Point Tool and the Fixed-Point Advisor to change the output data type setting for this block.

#### Command-Line Information

 Parameter: `LockScale` Type: string Value: `'off'` | `'on'` Default: `'off'`

### Integer rounding mode

Specify the rounding mode for fixed-point operations.

#### Settings

Default: `Floor`

`Ceiling`

Rounds both positive and negative numbers toward positive infinity. Equivalent to the MATLAB® `ceil` function.

`Convergent`

Rounds number to the nearest representable value. If a tie occurs, rounds to the nearest even integer. Equivalent to the Fixed-Point Designer™ `convergent` function.

`Floor`

Rounds both positive and negative numbers toward negative infinity. Equivalent to the MATLAB `floor` function.

`Nearest`

Rounds number to the nearest representable value. If a tie occurs, rounds toward positive infinity. Equivalent to the Fixed-Point Designer `nearest` function.

`Round`

Rounds number to the nearest representable value. If a tie occurs, rounds positive numbers toward positive infinity and rounds negative numbers toward negative infinity. Equivalent to the Fixed-Point Designer `round` function.

`Simplest`

Automatically chooses between round toward floor and round toward zero to generate rounding code that is as efficient as possible.

`Zero`

Rounds number toward zero. Equivalent to the MATLAB `fix` function.

#### Command-Line Information

 Parameter: `RndMeth` Type: string Value: `'Ceiling'` | `'Convergent'` | `'Floor'` | `'Nearest'` | `'Round'` | `'Simplest'` | `'Zero'` Default: `'Floor'`

### Saturate on integer overflow

Specify whether overflows saturate.

#### Settings

Default: Off

On

Overflows saturate to either the minimum or maximum value that the data type can represent.

For example, an overflow associated with a signed 8-bit integer can saturate to -128 or 127.

Off

Overflows wrap to the appropriate value that is representable by the data type.

For example, the number 130 does not fit in a signed 8-bit integer and wraps to -126.

#### Tips

• Consider selecting this check box when your model has possible overflow and you want explicit saturation protection in the generated code.

• Consider clearing this check box when you want to optimize efficiency of your generated code.

Clearing this check box also helps you avoid overspecifying how a block handles out-of-range signals. For more information, see Checking for Signal Range Errors.

• When you select this check box, saturation applies to every internal operation on the block, not just the output or result.

• In general, the code generation process can detect when overflow is not possible. In this case, the code generator does not produce saturation code.

#### Command-Line Information

 Parameter: `SaturateOnIntegerOverflow` Type: string Value: `'off'` | `'on'` Default: `'off'`

### Parameter minimum

Specify the minimum value of the gain.

#### Settings

 Default: `[]`

The default value is `[]` (unspecified). Simulink software uses this value to perform:

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Parameter maximum

Specify the maximum value of the gain.

#### Settings

 Default: `[]`

The default value is `[]` (unspecified). Simulink software uses this value to perform:

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Parameter data type

Specify the data type of the Gain parameter.

#### Settings

Default: ```Inherit: Inherit via internal rule```

`Inherit: Inherit via internal rule`

Use an internal rule to inherit the data type.

`Inherit: Same as input`

Use data type of sole input signal.

`Inherit: Inherit from 'Gain'`

Use data type of the Gain value. For example:

If you set Gain to...The parameter data type inherits...
`2``double`
`single(2)``single`
`int8(2)``int8`

`double`

Data type is `double`.

`single`

Data type is `single`.

`int8`

Data type is `int8`.

`uint8`

Data type is `uint8`.

`int16`

Data type is `int16`.

`uint16`

Data type is `uint16`.

`int32`

Data type is `int32`.

`uint32`

Data type is `uint32`.

`fixdt(1,16)`

Data type is `fixdt(1,16)`.

`fixdt(1,16,0)`

Data type is `fixdt(1,16,0)`.

`fixdt(1,16,2^0,0)`

Data type is `fixdt(1,16,2^0,0)`.

`<data type expression>`

Use a data type object, for example, `Simulink.NumericType`.

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

### Mode

Select the category of data to specify.

#### Settings

Default: `Inherit`

`Inherit`

Inheritance rules for data types. Selecting `Inherit` enables a second menu/text box to the right. Select one of the following choices:

• `Inherit via internal rule` (default)

• `Same as input`

• `Inherit from 'Gain'`

`Built in`

Built-in data types. Selecting `Built in` enables a second menu/text box to the right. Select one of the following choices:

• `double` (default)

• `single`

• `int8`

• `uint8`

• `int16`

• `uint16`

• `int32`

• `uint32`

`Fixed point`

Fixed-point data types.

`Expression`

Expressions that evaluate to data types. Selecting `Expression` enables a second menu/text box to the right, where you can enter the expression.

#### Dependency

Clicking the Show data type assistant button enables this parameter.

#### Command-Line Information

See Block-Specific Parameters for the command-line information.

See Specify Data Types Using Data Type Assistant in the Simulink documentation.

### Scaling

Specify the method for scaling your fixed-point data to avoid overflow conditions and minimize quantization errors.

#### Settings

Default: ```Best precision```

`Binary point`

Specify binary point location.

`Slope and bias`

Enter slope and bias.

`Best precision`

Specify best-precision values.

#### Dependencies

Selecting Mode > ```Fixed point``` enables this parameter.

Selecting `Binary point` enables:

• Fraction length

• Calculate Best-Precision Scaling

Selecting `Slope and bias` enables:

• Slope

• Bias

• Calculate Best-Precision Scaling

## Examples

The following Simulink examples show how to use the Gain block:

• `sldemo_bouncesldemo_bounce`

• `sldemo_tonegen_fixptsldemo_tonegen_fixpt`

• `sldemo_hardstopsldemo_hardstop`

• `sldemo_enginewcsldemo_enginewc`

## Characteristics

 Direct Feedthrough Yes Sample Time Inherited from driving block Scalar Expansion Yes, of input and Gain parameter for `Element-wise(K.*u)` multiplication Dimensionalized Yes Multidimensionalized Yes, only if the Multiplication parameter specifies `Element-wise(K.*u)` Zero-Crossing Detection No