Documentation Center

  • Trial Software
  • Product Updates

Cumulative Sum

Cumulative sum of channel, column, or row elements

Library

Math Functions / Math Operations

dspmathops

Description

The Cumulative Sum block computes the cumulative sum along the specified dimension of the input or across time (running sum).

The inputs can be a vector or a matrix. The output always has the same dimensions, rate, data type, and complexity as the input.

Input and Output Characteristics

Valid Input

The Cumulative Sum block accepts vector or matrix inputs containing real or complex values.

Valid Reset Signal

The optional reset port, Rst, accepts scalar values, which can be any built-in Simulink® data type including boolean. The rate of the input to the Rst port must be the same or slower than that of the input data signal. The sample time of the input to the Rst port must be a positive integer multiple of the input sample time.

Output Characteristics

The output always has the same dimensions, rate, data type, and complexity as the data signal input.

Computing the Running Sum Along Channels of the Input

When you set the Sum input along parameter to Channels (running sum), the block computes the cumulative sum of the elements in each input channel. The running sum of the current input takes into account the running sum of all previous inputs. In this mode, you must also specify a value for the Input processing parameter. When you set the Input processing parameter to Columns as channels (frame based), the block computes the running sum along each column of the current input. When you set the Input processing parameter to Elements as channels (sample based), the block computes a running sum for each element of the input across time. See the following sections for more information:

Computing the Running Sum for Each Column of the Input

When you set the Input processing parameter to Columns as channels (frame based), the block treats each input column as an independent channel. As the following figure and equation illustrate, the output has the following characteristics:

  • The first row of the first output is the same as the first row of the first input.

  • The first row of each subsequent output is the sum of the first row of the current input (time t), and the last row of the previous output (time t - Tf, where Tf is the frame period).

  • The output has the same size, dimension, data type, and complexity as the input.

Given an M-by-N matrix input, u, the output, y, is an M-by-N matrix whose first row has elements

Computing the Running Sum for Each Element of the Input

When you set the Input processing parameter to Elements as channels (sample based), the block treats each element of the input matrix as an independent channel. As the following figure and equation illustrate, the output has the following characteristics:

  • The first output is the same as the first input.

  • Each subsequent output is the sum of the current input (time t) and the previous output (time t - Ts, where Ts is the sample period).

  • The output has the same size, dimension, data type, and complexity as the input.

Given an M-by-N matrix input, u, the output, y, is an M-by-N matrix with the elements

Resetting the Running Sum

When you are computing the running sum, you can configure the block to reset the running sum whenever it detects a reset event at the optional Rst port. The rate of the input to the Rst port must be the same or slower than that of the input data signal. The sample time of the input to the Rst port must be a positive integer multiple of the input sample time. The reset sample time must be a positive integer multiple of the input sample time. The input to the Rst port can be of the boolean data type.

If a reset event occurs while the block is performing sample-based processing, the block initializes the current output to the values of the current input. If a reset event occurs while the block is performing frame-based processing, the block initializes the first row of the current output to the values in the first row of the current input.

The Reset port parameter specifies the reset event, which can be one of the following:

  • None disables the Rst port.

  • Rising edge — Triggers a reset operation when the Rst input does one of the following:

    • Rises from a negative value to a positive value or zero

    • Rises from zero to a positive value, where the rise is not a continuation of a rise from a negative value to zero (see the following figure)

  • Falling edge — Triggers a reset operation when the Rst input does one of the following:

    • Falls from a positive value to a negative value or zero

    • Falls from zero to a negative value, where the fall is not a continuation of a fall from a positive value to zero (see the following figure)

  • Either edge — Triggers a reset operation when the Rst input is a Rising edge or Falling edge (as described above)

  • Non-zero sample — Triggers a reset operation at each sample time that the Rst input is not zero

    Note:   When you run simulations in the Simulink MultiTasking mode, reset signals have a one-sample latency. Thus, when the block detects a reset event, a one-sample delay occurs at the reset port rate before the block applies the reset. For more information on latency and the Simulink tasking modes, see Excess Algorithmic Delay (Tasking Latency) and the topic on models with multiple sample rates in the Simulink Coder™ documentation.

Summing Along Columns

When you set the Sum input along parameter to Columns, the block computes the cumulative sum of each column of the input. In this mode, the current cumulative sum is independent of the cumulative sums of previous inputs.

y = cumsum(u)						% Equivalent MATLAB code

The output has the same size, dimension, data type, and complexity as the input. The mth output row is the sum of the first m input rows.

Given an M-by-N input, u, the output, y, is an M-by-N matrix whose jth column has elements

The block treats length-M unoriented vector inputs as M-by-1 column vectors when summing along columns.

Summing Along Rows

When you set the Sum input along parameter to Rows, the block computes the cumulative sum of the row elements. In this mode, the current cumulative sum is independent of the cumulative sums of previous inputs.

y = cumsum(u,2)						% Equivalent MATLAB code

The output has the same size, dimension, and data type as the input. The nth output column is the sum of the first n input columns.

Given an M-by-N input, u, the output, y, is an M-by-N matrix whose ith row has elements

When you sum along rows, the block treats length-N unoriented vector inputs as 1-by-N row vectors.

Fixed-Point Data Types

The following diagram shows the data types used within the Cumulative Sum block for fixed-point signals.

You can set the accumulator and output data types in the block dialog as discussed in Dialog Box.

Dialog Box

The Main pane of the Cumulative Sum block dialog appears as follows.

Sum input along

Specify the dimension along which to compute the cumulative summations. You can choose to sum along Channels (running sum), Columns, or Rows. For more information, see the following sections:

Input processing

Specify how the block should process the input when computing the running sum along the channels of the input. You can set this parameter to one of the following options:

  • Columns as channels (frame based) — When you select this option, the block treats each column of the input as a separate channel.

  • Elements as channels (sample based) — When you select this option, the block treats each element of the input as a separate channel.

    Note:   The option Inherit from input (this choice will be removed - see release notes) will be removed in a future release. See Frame-Based Processing in the DSP System Toolbox™ Release Notes for more information.

This parameter is available only when you set the Sum input along parameter to Channels (running sum).

Reset port

Determines the reset event that causes the block to reset the sum along channels. The rate of the input to the Rst port must be the same or slower than that of the input data signal. The sample time of the input to the Rst port must be a positive integer multiple of the input sample time. This parameter appears only when you set the Sum input along parameter to Channels (running sum). For more information, see Resetting the Running Sum.

The Data Types pane of the Cumulative Sum block dialog appears as follows.

    Note:   Floating-point inheritance takes precedence over the data type settings defined on this pane. When inputs are floating point, the block ignores these settings, and all internal data types are floating point.

Rounding mode

Select the rounding mode for fixed-point operations.

Overflow mode

Select the overflow mode for fixed-point operations.

Accumulator data type

Specify the accumulator data type. See Fixed-Point Data Types for illustrations depicting the use of the accumulator data type in this block. You can set this parameter to:

  • A rule that inherits a data type, for example, Inherit: Same as input

  • An expression that evaluates to a valid data type, for example, fixdt([],16,0)

Click the Show data type assistant button to display the Data Type Assistant, which helps you set the Accumulator data type parameter.

See Specify Data Types Using Data Type Assistant for more information.

Output data type

Specify the output data type. See Fixed-Point Data Types for illustrations depicting the use of the output data type in this block. You can set it to:

  • A rule that inherits a data type, for example, Inherit: Same as accumulator

  • An expression that evaluates to a valid data type, for example, fixdt([],16,0)

Click the Show data type assistant button to display the Data Type Assistant, which helps you set the Output data type parameter.

See Specify Block Output Data Types for more information.

Minimum

Specify the minimum value that the block should output. The default value is [] (unspecified). Simulink software uses this value to perform:

  • Simulation range checking (see Signal Ranges)

  • Automatic scaling of fixed-point data types

Maximum

Specify the maximum value that the block should output. The default value is [] (unspecified). Simulink software uses this value to perform:

  • Simulation range checking (see Signal Ranges)

  • Automatic scaling of fixed-point data types

Lock data type settings against changes by the fixed-point tools

Select this parameter to prevent the fixed-point tools from overriding the data types you specify on the block mask.

Supported Data Types

Input and Output PortsSupported Data Types

Data input port, In

  • Double-precision floating point

  • Single-precision floating point

  • Fixed point

  • 8-, 16-, and 32-bit signed integers

  • 8-, 16-, and 32-bit unsigned integers

Reset input port, Rst

All built-in Simulink data types:

  • Double-precision floating point

  • Single-precision floating point

  • Boolean

  • 8-, 16-, and 32-bit signed integers

  • 8-, 16-, and 32-bit unsigned integers

Output port

  • Double-precision floating point

  • Single-precision floating point

  • Fixed point

  • 8-, 16-, and 32-bit signed integers

  • 8-, 16-, and 32-bit unsigned integers

See Also

Cumulative ProductDSP System Toolbox
DifferenceDSP System Toolbox
Matrix SumDSP System Toolbox
cumsumMATLAB

Was this topic helpful?