# 2-D FIR Filter

Perform 2-D FIR filtering on input matrix

## Library

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`

.

Port | Input/Output | Supported Data Types | Complex 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

$$(x+y)\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 an
**Output 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.

Use the **Padding options** parameter to specify how to pad the
boundary of your input matrix. To pad your matrix with a constant value, select
`Constant`

. To pad your input matrix by repeating its
border values, select `Replicate`

. To pad your input matrix
with its mirror image, select `Symmetric`

. To pad your input
matrix using a circular repetition of its elements, select
`Circular`

. For more information on padding, see the Image Pad block reference page.

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** of```
Input
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(i,j)={\displaystyle \sum _{m=0}^{(Ma-1)}{\displaystyle \sum _{n=0}^{(Na-1)}A(m,n)*H(i-m,j-n)}}$$

where $$0\le i<Ma+Mh-1$$ and $$0\le j<Na+Nh-1$$. If you select `Correlation `

and set the
**Output size** parameter to `Full`

, the
block filters your input using the following algorithm

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

where $$0\le i<Ma+Mh-1$$ and $$0\le j<Na+Nh-1$$.

The `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 Parameters.

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.

## Parameters

**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 a**Coefficient 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 a**Coefficient 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).**Padding options**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`Symmetric`

to 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`

.**Pad value source**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`

.**Pad value**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`

.**Rounding mode**Select the Rounding Modes for fixed-point operations.

**Saturate on integer overflow**Select the overflow mode for fixed-point operations. See Precision and Range.

**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**Saturate on integer overflow**parameters; instead, they are 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.See Specify Data Types Using Data Type Assistant (Simulink) for more information.

**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

**Coefficient 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 I1 Sign of Input I2 Sign of Product Output unsigned unsigned unsigned unsigned signed signed signed unsigned signed signed signed signed Click the

**Show data type assistant**button to display the**Data Type Assistant**, which helps you set the**Product output data type**parameter.See Specify Data Types Using Data Type Assistant (Simulink) for more information.

**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 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 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.See Specify Data Types Using Data Type Assistant (Simulink) for more information.

**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.

**Show data type assistant**button to display the**Data Type Assistant**, which helps you set the**Product output data type**parameter.See Specify Data Types Using Data Type Assistant (Simulink) for more information.

**Lock data type settings against change 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. For more information, see

`fxptdlg`

(Fixed-Point Designer), a reference page on the Fixed-Point Tool in the Simulink^{®}documentation.

## See Also

Image Processing Toolbox |

## Extended Capabilities

## See Also

**Introduced before R2006a**