| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Signal Processing Blockset |
| Contents | Index |
| Learn more about Signal Processing Blockset |
Math Functions / Math Operations
dspmathops
The Difference block computes the difference between adjacent elements in rows, columns, or a specified dimension of the input array u. This block accepts real and complex fixed-point and floating-point inputs, except for complex unsigned fixed-point inputs.
When the Difference along parameter is set to Columns, the block computes differences between adjacent elements along each column.
y = diff(u) % Equivalent MATLAB code
For sample-based inputs, the output is a sample-based (M-1)-by-N matrix whose jth column has elements
![]()
For convenience, length-M 1-D vector inputs are treated as M-by-1 column vectors for columnwise differencing, and the output is 1-D.
For example, the following figure shows the block output for sample-based inputs:

For frame-based inputs, the output is a frame-based M-by-N matrix whose jth column has elements
![]()
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, zero is subtracted from the first input element.
![]()
For example, the following figure shows the second frame of the block output for a frame-based input:

When the Difference along parameter is set to Rows, the block computes differences between adjacent elements along each row. The result is the same regardless of the frame status of the input signal.
y = diff(u,[],2) % Equivalent MATLAB code
The output is an M-by-(N-1) matrix whose ith row has elements
![]()
The frame status of the output is the same as the input. For convenience, length-N 1-D vector inputs are treated as 1-by-N row vectors for rowwise differencing, and the output is 1-D.
For example, the following figure shows the block output for sample-based inputs. The output is the same for frame-based inputs:

When the Difference along parameter is set 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 specified by 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 elements
![]()
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 .
The Main pane of the Difference block appears as follows.

Specify whether the block performs columnwise differencing, rowwise differencing, or differencing along a specified 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. |
Select the rounding mode for fixed-point operations.
Select the overflow mode for fixed-point operations.
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 Using the Data Type Assistant for more information.
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 Specifying Block Output Data Types for more information.
Specify the minimum value that the block should output. The default value, [], is equivalent to -Inf. Simulink software uses this value to perform:
Simulation range checking (see Checking Signal Ranges)
Automatic scaling of fixed-point data types
Specify the maximum value that the block should output. The default value, [], is equivalent to Inf. Simulink software uses this value to perform:
Simulation range checking (see Checking Signal Ranges)
Automatic scaling of fixed-point data types
Select this parameter to prevent the fixed-point tools from overriding the data types you specify on the block mask.
| Port | Supported Data Types |
|---|---|
Input |
|
Output |
|
| Cumulative Sum | Signal Processing Blockset |
| diff | MATLAB |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2010- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |