Difference

Compute element-to-element difference along specified dimension of input

Library

Math Functions / Math Operations

dspmathops

Description

The Difference block computes the difference between adjacent elements in rows, columns, or a specified dimension of the input array u. You can configure the block to compute the difference only within the current input, or across consecutive inputs (running difference).

Basic Operation

When you set the Running difference parameter to No, the block computes the difference between adjacent elements in the specified dimension of the current input. In this mode, the block can compute the difference along the columns, rows, or a specified dimension of the input.

Columnwise Differencing

When you set the Difference along parameter to Columns, the block computes differences between adjacent elements in each column of the input.

y = diff(u)						% Equivalent MATLAB code

For M-by-N inputs, the output is an (M-1)-by-N matrix whose jth column has the following elements:

yi,j=ui+1,jui,j1i(M1)

Rowwise Differencing

When you set the Difference along parameter to Rows, the block computes differences between adjacent elements in each row of the input.

y = diff(u,[],2)						% Equivalent MATLAB code

The output is an M-by-(N-1) matrix whose ith row has the following elements:

yi,j=ui,j+1ui,j1j(N1)

Differencing Along Arbitrary Dimensions

When you set the Difference along parameter to Specified dimension, the behavior of the block is an extension of the rowwise differencing described earlier. The block computes differences between adjacent elements along the dimension you specify in the Dimension parameter.

y = diff(u,[],d) % Equivalent MATLAB code where d is the dimension

The output is an array whose length in the specified dimension is one less than that of the input, and whose lengths in other dimensions are unchanged. For example, consider an M-by-N-by-P-by-R input array with elements u(i,j,k,l) and assume that Dimension is 3. The output of the block is an M-by-N-by-(P–1)-by-R array with the following elements:

yi,j,k,l=ui,j,k+1,lui,j,k,l1k(P1)

Running Operation

When you set the Running difference parameter to Yes, the block computes the running difference along the columns of the input.

For an M-by-N input matrix, the output is an M-by-N matrix whose jth column has the following elements:

yi,j=ui+1,jui,j2i(M1)

In running mode, the first element of the output for each column is the first input element minus the last input element of the previous frame. For the first frame, the block subtracts zero from the first input element.

y1,j(t)=u1,j(t)uM,j(tTf)

Fixed-Point Data Types

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

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

Examples

For an example showing the modes of the Difference block, open ex_differenceex_difference.

The following figure shows the output of the block when it is differencing along columns in nonrunning mode.

The following figure shows the output of the block when it is differencing along rows in nonrunning mode.

The following figure shows the second frame of block output when the block is computing the running difference.

Dialog Box

The Main pane of the Difference block appears as follows.

Running difference

Specify whether or not the block computes a running difference. When you select No, the block computes the difference between adjacent elements in the current input. When you select Yes, the block computes the running difference across consecutive inputs. In running mode, the block always computes the difference along the columns of the input. See Running Operation for more information.

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

Difference along

Specify whether the block computes the difference along the columns, rows, or specified dimension of the input.

Dimension

Specify the one-based dimension along which to compute element-to-element differences.

This parameter is only visible when you select Specified dimension for the Difference along parameter.

The Data Types pane of the Difference block 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: Inherit via internal rule

  • 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

PortSupported Data Types

Input

  • Double-precision floating point

  • Single-precision floating point

  • Fixed point

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

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

Output

  • 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 SumDSP System Toolbox
diffMATLAB

Was this topic helpful?