Sum - Add or subtract inputs - Simulink (original) (raw)

Libraries:
Simulink / Commonly Used Blocks
Simulink / Math Operations
HDL Coder / Commonly Used Blocks
HDL Coder / Math Operations

Alternative Configurations of Sum Block:
Add | Subtract | Sum of Elements

Description

The Sum block performs addition or subtraction on its inputs. TheAdd, Subtract, Sum of Elements, andSum blocks are alternative configurations of the same block. This block can add or subtract scalar, vector, or matrix inputs. It can also collapse the elements of a signal and perform a summation.

You specify the operations of the block with the List of signs parameter with plus signs (+), minus signs (-), and spacers (|).

The Sum block first converts the input data type to its accumulator data type, then performs the specified operations. The block converts the result to its output data type using the specified rounding and overflow modes.

Calculation of Block Output

Output calculation for the Sum block depends on the number of block inputs and the sign of input ports.

Number of Sum Block Input Ports Input Port Signs Block Output Calculation Formula Output Formula Variables
One input port The input port sign is + y = e[0] + e[1] + e[2] ... + e[m] e[i] is the_i_th element of input u
The input port sign is – y = 0.0 – e[0] – e[1] – e[2] ... – e[m]
Two or more input ports All input port signs are – y = 0.0 – u[0] – u[1] – u[2] ... – u[n] u[i] is the input to the_i_th input port
The _k_th input port is the first port where the sign is + y = u[k] – u[0] – u[1] – u[2] – u[k–1] (+/–) u[k+1] ... (+/–) u[n]

Examples

expand all

This example shows how the Sum block reorders inputs. If you use a - sign as the first operation, the block reorders the inputs, if possible, to use a + operation. For example, in the expression output = -a-b+c, the Sum block reorders the input so that output = c-a-b. To initialize the accumulator, the Sum block uses the first + input port.

The block avoids performing a unary minus operation on the first operand a because doing so can change the value of a for fixed-point data types. In that case, the output value differs from the result of accumulating the values for a, b, and c.

Both the constant inputs use int8 data types. The Sum block also uses int8 for the accumulator and output data types and has Saturate on integer overflow turned on. The Sum block reorders the inputs to give the ideal result of 127.

  1. Reorders inputs from (-Input1 + Input2) to (Input2 - Input1).
  2. Initializes the accumulator by using the first + input port. Accumulator = int8(-1) = -1
  3. Continues to accumulate values. Accumulator = Accumulator - int8(-128) = 127
  4. Calculates the block output. Output = int8(127) = 127

If the Sum block does not reorder the inputs, then you get the nonideal result of 126.

  1. Initializes the accumulator by using the first input port. Accumulator = int8(-(-128)) = 127
  2. Because saturation is on, the initial value of the accumulator saturates at 127 and does not wrap.
  3. Continues to accumulate values. Accumulator = Accumulator + int8(-1) = 126
  4. Calculates the block output. Output = int8(126) = 126

To explicitly specify a unary minus operation for output = -a-b+c, you can use the Unary Minus block in the Math Operations library.

Ports

Inputs

expand all

The inputs can be of different data types unless you select Require all inputs to have the same data type.

Input signal to the addition or subtraction operation. If there is only one input signal, then addition or subtraction is performed on the elements over all dimensions or the specified dimension.

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

_n_th input signal to the operations. The number of inputs matches the number of signs in the List of signs parameter. The block applies the operations to the inputs in the order listed. You can also use a numerical value equal to the number of input ports as the List of signs parameter. The block creates the input ports and applies addition to all inputs. For example, if you assign 5 for theList of signs parameter, the block creates5 input ports and adds them together to produce the output.

All nonscalar inputs must have the same dimensions. Scalar inputs are expanded to have the same dimensions as other inputs.

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

Output

expand all

Output signal resulting from addition and/or subtraction operations. The output signal has the same dimension as the input signals.

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

Parameters

expand all

Main

Designate the icon shape of the block as rectangular or round.

The default value depends on the block configuration.

For a rectangular block, the first input port is the top port. For a round Sum block, the first input port is the port closest to the 12 o'clock position going in a counterclockwise direction around the block. Similarly, other input ports appear in counterclockwise order around the block.

Programmatic Use

Block Parameter:IconShape
Type: character vector
Values:'rectangular' |'round'

Enter addition and subtraction operations performed on the inputs. An input port is created for each operation. A spacer (|) creates extra space between the input ports on the block icon. Addition is the default operation. If you only want to add the inputs, enter the number of input ports. The operations are performed in the order listed.

The default value depends on the block configuration.

When you enter only one sign, the block enables the Apply over parameter. For a vector input, + or - adds or subtracts the elements over all dimensions or in the specified dimension.

Tips

You can manipulate the positions of the input ports on the block by inserting spacers (|) between the signs in theList of signs parameter. For example, “++|--” creates an extra space between the second and third input ports.

Programmatic Use

Block Parameter:Inputs
Type: character vector
Values:'+' | '-'

Specify how to apply function along specified dimensions.

Dependencies

To enable this parameter, enter only one sign in theList of signs parameter.

Programmatic Use

Block Parameter:CollapseMode
Type: character vector
Values: 'All dimensions' | 'Specified dimension'
Default: 'All dimensions'

Specify the dimension along which to apply the summation as a positive integer.

The block follows the same summation rules as the MATLAB® sum function. For more information, see Algorithms.

For example, in this model, List of signs is set to +, Apply over is set toSpecified dimension, andDimension is set to 2. The block returns the sum of the input values of each row.

2D matrix with Constant block value [1 2 3;4 5 6] as input to Sum block configured for dimension 2

If the specified dimension is greater than the dimension of the input, an error message appears.

Dependencies

To enable this parameter, set Apply over toSpecified dimension.

Programmatic Use

Block Parameter:CollapseDim
Type: character vector
Value: integer
Default:'1'

Specify the time interval between samples. To inherit the sample time, set this parameter to -1. For more information, see Specify Sample Time.

Dependencies

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

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: SampleTime
Values: "-1" (default) | scalar or vector in quotes

Signal Attributes

The Data Type Assistant helps you set data attributes. To use the Data Type Assistant, click the Show data type assistant button. For more information, see Specify Data Types Using Data Type Assistant.

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

When you select an inherited option, the block behaves as follows:

Programmatic Use

Block Parameter:OutDataTypeStr
Type: character vector
Values: 'Inherit: Inherit via internal rule | 'Inherit: Keep MSB' 'Inherit: Keep LSB' 'Inherit: Inherit via back propagation' 'Inherit: Same as first input' 'Inherit: Same as accumulator' 'double' 'single' 'half' 'int8' 'uint8' 'int16' 'uint16' 'int32' 'uint32' 'int64' 'uint64' 'fixdt(1,16)' 'fixdt(1,16,0)' 'fixdt(1,16,2^0,0)'
Default: 'Inherit: Inherit via internal rule'

Choose the data type of the accumulator. The type can be inherited, specified directly, or expressed as a data type object such asSimulink.NumericType. When you chooseInherit: 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.

Programmatic Use

Block Parameter: AccumDataTypeStr
Type: character vector
Values:'Inherit: Inherit via internal rule' | 'Inherit: Same as first input' 'double' 'single' 'half' 'int8' 'uint8' 'int16' 'uint16','int32' 'uint32' 'int64' 'uint64' 'fixdt(1,16)' 'fixdt(1,16,0)' 'fixdt(1,16,2^0,0)' ''
Default:'Inherit: Inherit via internal rule'

Specify if input signals must all have the same data type. If you enable this parameter, then an error occurs during simulation if the input signal types are different.

Programmatic Use

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

Select to lock data type settings of this block against changes by the Fixed-Point Tool and the Fixed-Point Advisor. For more information, see Lock the Output Data Type Setting (Fixed-Point Designer).

Programmatic Use

Block Parameter: LockScale
Values: 'off' | 'on'
Default: 'off'

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

To set the block parameter value programmatically, use the set_param function.

Parameter: RndMeth
Values: 'Floor' (default) | 'Ceiling' 'Convergent' 'Nearest' 'Round' 'Simplest' 'Zero'

Specify whether overflows saturate or wrap.

For example, the maximum value that the signed 8-bit integer int8 can represent is 127. Any block operation result greater than this maximum value causes overflow of the 8-bit integer.

Tips

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: SaturateOnIntegerOverflow
Values: 'off' (default) | 'on'

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

Alternative Configurations

expand all

The Add block sets Icon shape torectangular and List of signs to ++.

Libraries:
Simulink / Math Operations
HDL Coder / HDL Floating Point Operations
HDL Coder / Math Operations

The Subtract block sets Icon shape to rectangular and List of signs to +-.

Libraries:
Simulink / Math Operations
HDL Coder / HDL Floating Point Operations
HDL Coder / Math Operations

The Sum of Elements block sets Icon shape to rectangular andList of signs to +.

Libraries:
Simulink / Math Operations
HDL Coder / HDL Floating Point Operations
HDL Coder / Math Operations

Algorithms

The block follows the same summation rules as the MATLABsum function.

Suppose that you have a 2-by-3 matrix U.

Extended Capabilities

expand all

HDL Coder™ provides additional configuration options that affect HDL implementation and synthesized logic.

HDL Architecture of Sum, Add, and Subtract Blocks

The default Linear architecture generates a chain of N operations (adders) for N inputs.

HDL Architecture of Sum of Elements Block

For the Sum of Elements block, HDL Coder supports Tree architectures for Sum of Elements blocks that have a single vector input with multiple elements.

This block has multi-cycle implementations that introduce additional latency in the generated code. To see the added latency, view the generated model or validation model. See Generated Model and Validation Model (HDL Coder).

Architecture Additional cycles of latency Description
Linear 0 Generates a linear chain of adders to compute the sum of products.For multiple inputs that have different bit widths, the Linear architecture optimizes the resource utilization by implementing adders in multiple stages with pipelines in between the stages. The output of each stage is calculated based on the width of the inputs to that stage.
Tree 0 Generates a tree structure of adders to compute the sum of products.

HDL Block Properties

Note

To use the LatencyStrategy setting in the Native Floating Point tab of the HDL Block Properties dialog box, specifyLinear or Tree as the HDL Architecture.

General
ConstrainedOutputPipeline Number of registers to place at the outputs by moving existing delays within your design. Distributed pipelining does not redistribute these registers. The default is0. For more details, see ConstrainedOutputPipeline (HDL Coder).
InputPipeline Number of input pipeline stages to insert in the generated code. Distributed pipelining and constrained output pipelining can move these registers. The default is0. For more details, see InputPipeline (HDL Coder).
OutputPipeline Number of output pipeline stages to insert in the generated code. Distributed pipelining and constrained output pipelining can move these registers. The default is0. For more details, see OutputPipeline (HDL Coder).

Note

The Sum of Elements block does not support HDL code generation with double data types in the Native Floating Point mode.

Native Floating Point
LatencyStrategy Specify whether to map the blocks in your design to inherit,Max, Min, Zero, orCustom for the floating-point operator. The default isinherit. See also LatencyStrategy (HDL Coder).
NFPCustomLatency To specify a value, setLatencyStrategy to Custom. HDL Coder adds latency equal to the value that you specify for theNFPCustomLatency setting. See also NFPCustomLatency (HDL Coder).

Complex Data Support

The default Linear implementation supports complex data.

The Tree implementation supports complex data with+ for the List of signs block parameter. With native floating point support, the Tree implementation supports complex data with both + and - for List of signs.

Limitations and Considerations

To generate HDL code for multi-input Sum block that has mixed scalar and vector inputs, you must specify vector input at one of the first two inputs of the Sum block.

Version History

Introduced before R2006a

expand all

When you enter only one sign for List of signs, the block enables the Apply over parameter. For a vector input,+ or - adds or subtracts the elements over all dimensions or in the specified dimension.

To better reflect the supported behavior, the Sum over parameter is now called Apply over.