Main Content

filter2

2-D digital filter

Description

example

Y = filter2(H,X)applies a finite impulse response filter to a matrix of dataXaccording to coefficients in a matrixH.

example

Y = filter2(H,X,shape)returns a subsection of the filtered data according toshape. For example,Y = filter2(H,X,'valid')returns only filtered data computed without zero-padded edges.

Examples

collapse all

You can digitally filter images and other 2-D data using thefilter2function, which is closely related to theconv2function.

Create and plot a 2-D pedestal with interior height equal to one.

A = zeros(10); A(3:7,3:7) = ones(5); mesh(A)

Figure contains an axes object. The axes object contains an object of type surface.

Filter the data inAaccording to a filter coefficient matrixH, and return the full matrix of filtered data.

H = [1 2 1; 0 0 0; -1 -2 -1]; Y = filter2(H,A,'full'); mesh(Y)

Figure contains an axes object. The axes object contains an object of type surface.

RotateH180 degrees and convolve the result withA. The output is equivalent to filtering the data inAwith the coefficients inH.

C = conv2(A,rot90(H,2)); mesh(C)

Figure contains an axes object. The axes object contains an object of type surface.

Input Arguments

collapse all

Coefficients of the rational transfer function, specified as a matrix.

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical
Complex Number Support:Yes

Input data, specified as a matrix. If one or both ofXandHare of typesingle, then the output is also of typesingle. Otherwise,filter2returns typedouble.

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical
Complex Number Support:Yes

Subsection of the filtered data, specified as one of these values:

  • 'same'— Return the central part of the filtered data, which is the same size asX.

  • 'full'— Return the full 2-D filtered data.

  • 'valid'— Return only parts of the filtered data that are computed without zero-padded edges.

Algorithms

Thefilter2function filters data by taking the 2-D convolution of the inputXand the coefficient matrixHrotated 180 degrees. Specifically,filter2(H,X,shape)相当于conv2(X,rot90(H,2),shape).

Extended Capabilities

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

See Also

||

Introduced before R2006a