Accelerating the pace of engineering and science

# 2-D FIR Filter

Perform 2-D FIR filtering on input matrix

Filtering

visionfilter

## Description

The 2-D Finite Impulse Response (FIR) filter block filters the input matrix I using the coefficient matrix H or the coefficient vectors HH and HV.

PortInput/OutputSupported Data TypesComplex Values Supported

I

Vector or matrix of intensity values

• Double-precision floating point

• Single-precision floating point

• Fixed point

• 8-, 16-, 32-bit signed integer

• 8-, 16-, 32-bit unsigned integer

Yes

H

Matrix of filter coefficients

Same as I port.

Yes

HH

Vector of filter coefficients

Same as I port. The input to ports HH and HV must be the same data type.

Yes

HV

Vector of filter coefficients

Same as I port. The input to ports HH and HV must be the same data type.

Yes

PVal

Scalar value that represents the constant pad value

Input must have the same data type as the input to I port.

Yes

Output

Scalar, vector, or matrix of filtered values

Same as I port.

Yes

If the input has a floating-point data type, then the output uses the same data type. Otherwise, the output can be any fixed-point data type.

Select the Separable filter coefficients check box if your filter coefficients are separable. Using separable filter coefficients reduces the amount of calculations the block must perform to compute the output. For example, suppose your input image is M-by-N and your filter coefficient matrix is x-by-y. For a nonseparable filter with the Output size parameter set to Same as input port I, it would take

$x\cdot y\cdot M\cdot N$

multiply-accumulate (MAC) operations for the block to calculate the output. For a separable filter, it would only take

$\left(x+y\right)\cdot M\cdot N$

MAC operations. If you do not know whether or not your filter coefficients are separable, use the isfilterseparable function.

Here is an example of the function syntax, [S, HCOL, HROW] = isfilterseparable(H). The isfilterseparable function takes the filter kernel, H, and returns S, HCOL and HROW. Here, S is a Boolean variable that is 1 if the filter is separable and 0 if it is not. HCOL is a vector of vertical filter coefficients, and HROW is a vector of horizontal filter coefficients.

Use the Coefficient source parameter to specify how to define your filter coefficients. If you select the Separable filter coefficients check box and then select a Coefficient source of Specify via dialog, the Vertical coefficients (across height) and Horizontal coefficients (across width) parameters appear in the dialog box. You can use these parameters to enter vectors of vertical and horizontal filter coefficients, respectively.

You can also use the variables HCOL and HROW, the output of the isfilterseparable function, for these parameters. If you select the Separable filter coefficients check box and then select a Coefficient source of Input port, ports HV and HH appear on the block. Use these ports to specify vectors of vertical and horizontal filter coefficients.

If you clear the Separable filter coefficients check box and select a Coefficient source of Specify via dialog, the Coefficients parameter appears in the dialog box. Use this parameter to enter your matrix of filter coefficients.

If you clear the Separable filter coefficients check box and select a Coefficient source of Input port, port H appears on the block. Use this port to specify your filter coefficient matrix.

The block outputs the result of the filtering operation at the Output port. The Output size parameter and the sizes of the inputs at ports I and H dictate the dimensions of the output. For example, assume that the input at port I has dimensions (Mi, Ni) and the input at port H has dimensions (Mh, Nh). If you select anOutput size of Full, the output has dimensions (Mi+Mh-1, Ni+Nh-1). If you select an Output size of Same as input port I, the output has the same dimensions as the input at port I. If you select an Output size of Valid, the block filters the input image only where the coefficient matrix fits entirely within it, so no padding is required. The output has dimensions (Mi-Mh+1, Ni-Nh+1). However, if all(size(I)<size(H)), the block errors out.

If, for the Padding options parameter, you select Constant, the Pad value source parameter appears in the dialog box. If you select Specify via dialog, the Pad value parameter appears in the dialog box. Use this parameter to enter the constant value with which to pad your matrix. If you select Pad value source ofInput port, the PVal port appears on the block. Use this port to specify the constant value with which to pad your matrix. The pad value must be real if the input image is real. You will get an error message if the pad value is complex when the input image is real.

Use the Filtering based on parameter to specify the algorithm by which the block filters the input matrix. If you select Convolution and set the Output size parameter to Full, the block filters your input using the following algorithm

$C\left(i,j\right)=\sum _{m=0}^{\left(Ma-1\right)}\sum _{n=0}^{\left(Na-1\right)}A\left(m,n\right)*H\left(i-m,j-n\right)$

where $0\le i and $0\le j. If you select Correlation and set the Output size parameter to Full, the block filters your input using the following algorithm

$C\left(i,j\right)=\sum _{m=0}^{\left(Ma-1\right)}\sum _{n=0}^{\left(Na-1\right)}A\left(m,n\right)\cdot conj\left(H\left(m+i,n+j\right)\right)$

where $0\le i and $0\le j.

TThe imfilter function from the Image Processing Toolbox™ product similarly performs N-D filtering of multidimensional images.

### Fixed-Point Data Types

The following diagram shows the data types used in the 2-D FIR Filter block for fixed-point signals.

You can set the coefficient, product output, accumulator, and output data types in the block mask as discussed in Dialog Box.

The output of the multiplier is in the product output data type if at least one of the inputs to the multiplier is real. If both of the inputs to the multiplier are complex, the result of the multiplication is in the accumulator data type. For details on the complex multiplication performed, refer to Multiplication Data Types.

## Dialog Box

The Main pane of the 2-D FIR Filter dialog box appears as shown in the following figure.

Separable filter coefficients

Select this check box if your filter coefficients are separable. Using separable filter coefficients reduces the amount of calculations the block must perform to compute the output.

Coefficient source

Specify how to define your filter coefficients. Select Specify via dialog to enter your coefficients in the block parameters dialog box. Select Input port to specify your filter coefficient matrix using port H or ports HH and HV.

Coefficients

Enter your real or complex-valued filter coefficient matrix. This parameter appears if you clear the Separable filter coefficients check box and then select a Coefficient source of Specify via dialog. Tunable.

Vertical coefficients (across height)

Enter the vector of vertical filter coefficients for your separable filter. This parameter appears if you select the Separable filter coefficients check box and then select aCoefficient source of Specify via dialog.

Horizontal coefficients (across width)

Enter the vector of horizontal filter coefficients for your separable filter. This parameter appears if you select the Separable filter coefficients check box and then select aCoefficient source of Specify via dialog.

Output size

This parameter controls the size of the filtered output. If you choose Full, the output has dimensions (Ma+Mh-1, Na+Nh-1). If you choose Same as input port I, the output has the same dimensions as the input at port I If you choose Valid, output has dimensions (Ma-Mh+1, Na-Nh+1).

Specify how to pad the boundary of your input matrix. Select Constant to pad your matrix with a constant value. Select Replicate to pad your input matrix by repeating its border values. Select Symmetricto pad your input matrix with its mirror image. Select Circular to pad your input matrix using a circular repetition of its elements. This parameter appears if you select an Output size of Full or Same as input port I.

Use this parameter to specify how to define your constant boundary value. Select Specify via dialog to enter your value in the block parameters dialog box. Select Input port to specify your constant value using the PVal port. This parameter appears if you select a Padding options of Constant.

Enter the constant value with which to pad your matrix. This parameter is visible if, for the Pad value source parameter, you select Specify via dialog. Tunable. The pad value must be real if the input image is real. You will get an error message if the pad value is complex when the input image is real.

Filtering based on

Specify the algorithm by which the block filters the input matrix. You can select Convolution or Correlation.

The Data Types pane of the 2-D FIR Filter dialog box appears as shown in the following figure.

Rounding mode

Select the Rounding Modes for fixed-point operations.

Overflow mode

Select the Overflow mode for fixed-point operations.

Coefficients

Choose how to specify the word length and the fraction length of the filter coefficients.

• When you select Inherit: Same word length as input, the word length of the filter coefficients match that of the input to the block. In this mode, the block automatically sets the fraction length of the coefficients to the binary-point only scaling that provides you with the best precision possible given the value and word length of the coefficients.

• When you select fixdt(1,16), you can enter the word length of the coefficients, in bits. In this mode, the block automatically sets the fraction length of the coefficients to the binary-point only scaling that provides you with the best precision possible given the value and word length of the coefficients.

• When you select fixdt(1,16,0), you can enter the word length and the fraction length of the coefficients, in bits.

• When you select <data type expression>, you can enter the data type expression.

The filter coefficients do not obey the Rounding mode and the Overflow mode parameters; instead, they always saturated and rounded to Nearest.

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

Product output

Use this parameter to specify how to designate the product output word and fraction lengths. Refer to Fixed-Point Data Types and Multiplication Data Types for illustrations depicting the use of the product output data type in this block:

• When you select Inherit: Same as input, these characteristics match those of the input to the block.

• When you select fixdt([],16,0), you can enter the word length and the fraction length of the product output, in bits.

• When you select <data type expression>, you can enter the data type expression.

If you set the Coefficent source (on the Main tab) to Input port the Product Output will inherit its sign according to the inputs. If either or both input I1 and I2 are signed, the Product Output will be signed. Otherwise, the Product Output is unsigned. The following table shows all cases.

Sign of Input I1Sign of Input I2Sign of Product Output
unsignedunsignedunsigned
unsignedsignedsigned
signedunsignedsigned
signedsignedsigned

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

Accumulator

Use this parameter to specify how to designate the accumulator word and fraction lengths. Refer to Fixed-Point Data Types and Multiplication Data Types in the DSP System Toolbox™ documentation for illustrations depicting the use of the accumulator data type in this block. The accumulator data type is only used when both inputs to the multiplier are complex:

• When you select Inherit: Same as input, these characteristics match those of the input to the block.

• When you select Inherit: Same as product output, these characteristics match those of the product output.

• When you select fixdt([],16,0), you can enter the word length and the 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. All signals in the Computer Vision System Toolbox™ software have a bias of 0.

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

Output

Choose how to specify the word length and fraction length of the output of the block:

• When you select Inherit: Same as input, these characteristics match those of the input to the block.

• When you select fixdt([],16,0), you can enter the word length and the fraction length of the output, in bits.

You can choose to set signedness of the output to Auto, Signed or Unsigned.

• When you select <data type expression>, you can enter the a data type expression.

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