| 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 Fixed-point pane of the Difference block appears as follows.

Select the rounding mode for fixed-point operations.
Select the overflow mode for fixed-point operations.

Use this parameter to specify how you would like to designate the accumulator word and fraction lengths:
When you select Inherit via internal rule, the accumulator word length and fraction length are calculated automatically. For information about how the accumulator word and fraction lengths are calculated when an internal rule is used, see Inherit via Internal Rule.
When you select Same as input, these characteristics match those of the input to the block.
When you select Binary point scaling, you can enter the word length and fraction length of the accumulator, in bits.
When you select Slope and bias scaling, you can enter the word length, in bits, and the slope of the accumulator. This block requires power-of-two slope and a bias of zero.
Choose how you specify the output word length and fraction length:
When you select Same as accumulator, these characteristics match those of the accumulator.
When you select Same as input, these characteristics match those of the input to the block.
When you select Binary point scaling, you can enter the word length and fraction length of the output, in bits.
When you select Slope and bias scaling, you can enter the word length, in bits, and the slope of the output. This block requires power-of-two slope and a bias of zero.
Select this parameter to prevent any fixed-point scaling you specify in this block mask from being overridden by the autoscaling tool in the Fixed-Point Tool.
| Port | Supported Data Types |
|---|---|
Input |
|
Output |
|
| Cumulative Sum | Signal Processing Blockset |
| diff | MATLAB |
![]() | Detrend | Differentiator Filter | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |