Main Content

2-D FIR Filter

Perform 2-D FIR filtering on input matrix

Library

Filtering

visionfilter

  • 2-D FIR Filter block

Description

The 2-D Finite Impulse Response (FIR) filter block filters the input matrixIusing the coefficient matrixHor the coefficient vectorsHHandHV.

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 theSeparable filter coefficientscheck 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 isM-by-Nand your filter coefficient matrix is x-by-y. For a nonseparable filter with theOutput sizeparameter set toSame as input port I, it would take

x y M N

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

( x + y ) M N

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

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

Use theCoefficient sourceparameter to specify how to define your filter coefficients. If you select theSeparable filter coefficientscheck box and then select aCoefficient sourceofSpecify via dialog,Vertical coefficients (across height)andHorizontal 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 variablesHCOLandHROW,output of theisfilterseparablefunction, for these parameters. If you select theSeparable filter coefficientscheck box and then select aCoefficient sourceofInput port, ports HV and HH appear on the block. Use these ports to specify vectors of vertical and horizontal filter coefficients.

If you clear theSeparable filter coefficientscheck box and select aCoefficient sourceofSpecify via dialog,Coefficientsparameter appears in the dialog box. Use this parameter to enter your matrix of filter coefficients.

If you clear theSeparable filter coefficientscheck box and select aCoefficient sourceofInput port, portHappears 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. TheOutput sizeparameter and the sizes of the inputs at portsIandHdictate 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 sizeofFull,output has dimensions (Mi+Mh-1,Ni+Nh-1). If you select anOutput sizeofSame as input port I,output has the same dimensions as the input at port I. If you select anOutput sizeofValid,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, ifall(size(I),block errors out.

Use thePadding optionsparameter to specify how to pad the boundary of your input matrix. To pad your matrix with a constant value, selectConstant. To pad your input matrix by repeating its border values, selectReplicate. To pad your input matrix with its mirror image, selectSymmetric. To pad your input matrix using a circular repetition of its elements, selectCircular. For more information on padding, see theImage Padblock reference page.

If, for thePadding optionsparameter, you selectConstant,Pad value sourceparameter appears in the dialog box. If you selectSpecify via dialog,Pad valueparameter appears in the dialog box. Use this parameter to enter the constant value with which to pad your matrix. If you selectPad value sourceofInput port,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 theFiltering based onparameter to specify the algorithm by which the block filters the input matrix. If you selectConvolutionand set theOutput sizeparameter toFull,block filters your input using the following algorithm

C ( i , j ) = m = 0 ( M a 1 ) n = 0 ( N a 1 ) A ( m , n ) * H ( i m , j n )

where 0 i < M a + M h 1 and 0 j < N a + N h 1 . If you selectCorrelationand set theOutput sizeparameter toFull,block filters your input using the following algorithm

C ( i , j ) = m = 0 ( M a 1 ) n = 0 ( N a 1 ) A ( m , n ) c o n j ( H ( m + i , n + j ) )

where 0 i < M a + M h 1 and 0 j < N a + N h 1 .

Theimfilterfunction 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 inParameters.

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 toMultiplication 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. SelectSpecify via dialogto enter your coefficients in the block parameters dialog box. SelectInput portto 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 theSeparable filter coefficientscheck box and then select aCoefficient sourceofSpecify via dialog. Tunable.

Vertical coefficients (across height)

Enter the vector of vertical filter coefficients for your separable filter. This parameter appears if you select theSeparable filter coefficientscheck box and then select aCoefficient sourceofSpecify via dialog.

Horizontal coefficients (across width)

Enter the vector of horizontal filter coefficients for your separable filter. This parameter appears if you select theSeparable filter coefficientscheck box and then select aCoefficient sourceofSpecify via dialog.

Output size

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

Padding options

Specify how to pad the boundary of your input matrix. SelectConstantto pad your matrix with a constant value. SelectReplicateto pad your input matrix by repeating its border values. SelectSymmetricto pad your input matrix with its mirror image. SelectCircularto pad your input matrix using a circular repetition of its elements. This parameter appears if you select anOutput sizeofFullorSame as input port I.

Pad value source

Use this parameter to specify how to define your constant boundary value. SelectSpecify via dialogto enter your value in the block parameters dialog box. SelectInput portto specify your constant value using the PVal port. This parameter appears if you select aPadding optionsofConstant.

Pad value

Enter the constant value with which to pad your matrix. This parameter is visible if, for thePad value sourceparameter, you selectSpecify 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 selectConvolutionorCorrelation.

Rounding mode

Select theRounding Modes为定点操作。

Saturate on integer overflow

Select the overflow mode for fixed-point operations. SeePrecision and Range.

Coefficients

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

  • When you selectInherit: Same word length as input,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 selectfixdt(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 selectfixdt(1,16,0), you can enter the word length and the fraction length of the coefficients, in bits.

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

The filter coefficients do not obey theRounding modeand theSaturate on integer overflowparameters; instead, they are always saturated and rounded toNearest.

Click theShow data type assistantbuttonto display theData Type Assistant, which helps you set theProduct output data typeparameter.

SeeSpecify 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 toFixed-Point Data TypesandMultiplication Data Typesfor illustrations depicting the use of the product output data type in this block:

  • When you selectInherit: Same as input,se characteristics match those of the input to the block.

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

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

If you set theCoefficient source(on theMaintab) toInput port根据产品的输出将继承其标志to the inputs. If either or both inputI1andI2are 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 theShow data type assistantbuttonto display theData Type Assistant, which helps you set theProduct output data typeparameter.

SeeSpecify 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 toFixed-Point Data TypesandMultiplication Data Typesfor 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 selectInherit: Same as input,se characteristics match those of the input to the block.

  • When you selectInherit: Same as product output,se characteristics match those of the product output.

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

  • When you selectSlope 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 theShow data type assistantbuttonto display theData Type Assistant, which helps you set theProduct output data typeparameter.

SeeSpecify 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 selectInherit: Same as input,se characteristics match those of the input to the block.

  • When you selectfixdt([],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 toAuto,SignedorUnsigned.

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

Click theShow data type assistantbuttonto display theData Type Assistant, which helps you set theProduct output data typeparameter.

SeeSpecify 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, seefxptdlg(Fixed-Point Designer), a reference page on the Fixed-Point Tool in the Simulink®documentation.

See Also

imfilter

Image Processing Toolbox

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.

Introduced before R2006a