## Design Linear Filters in the Frequency Domain

This topic describes functions that perform filtering in the frequency domain. For information about designing filters in the spatial domain, see What Is Image Filtering in the Spatial Domain?.

### 2-D Finite Impulse Response (FIR) Filters

The Image Processing Toolbox™ software supports one class of linear filter: the two-dimensional finite impulse response (FIR) filter. FIR filters have a finite extent to a single point, or impulse. All of the Image Processing Toolbox filter design functions return FIR filters.

FIR filters have several characteristics that make them ideal for image processing in the MATLAB® environment:

• FIR filters are easy to represent as matrices of coefficients.

• Two-dimensional FIR filters are natural extensions of one-dimensional FIR filters.

• There are several well-known, reliable methods for FIR filter design.

• FIR filters are easy to implement.

• FIR filters can be designed to have linear phase, which helps prevent distortion.

Another class of filter, the infinite impulse response (IIR) filter, is not as suitable for image processing applications. It lacks the inherent stability and ease of design and implementation of the FIR filter. Therefore, this toolbox does not provide IIR filter support.

Note

Most of the design methods described in this section work by creating a two-dimensional filter from a one-dimensional filter or window created using Signal Processing Toolbox™ functions. Although this toolbox is not required, you might find it difficult to design filters if you do not have the Signal Processing Toolbox software.

### Create 2-D Filter Using Frequency Transformation of 1-D Filter

This example shows how to transform a one-dimensional FIR filter into a two-dimensional FIR filter using the `ftrans2` function. This function can be useful because it is easier to design a one-dimensional filter with particular characteristics than a corresponding two-dimensional filter. The frequency transformation method preserves most of the characteristics of the one-dimensional filter, particularly the transition bandwidth and ripple characteristics. The shape of the one-dimensional frequency response is clearly evident in the two-dimensional response.

This function uses a transformation matrix, a set of elements that defines the frequency transformation. This function's default transformation matrix produces filters with nearly circular symmetry. By defining your own transformation matrix, you can obtain different symmetries. (See Jae S. Lim, Two-Dimensional Signal and Image Processing, 1990, for details.)

Create a 1-D FIR filter using the `firpm` function from the Signal Processing Toolbox.

`b = firpm(10,[0 0.4 0.6 1],[1 1 0 0])`
```b = 1×11 0.0537 -0.0000 -0.0916 -0.0001 0.3131 0.4999 0.3131 -0.0001 -0.0916 -0.0000 0.0537 ```

Transform the 1-D filter to a 2-D filter.

`h = ftrans2(b);`

View the frequency response of the filters.

```[H,w] = freqz(b,1,64,"whole"); plot(w/pi-1,fftshift(abs(H))) title("One-Dimensional Frequency Response")```

```freqz2(h,[32 32]) title("Corresponding Two-Dimensional Frequency Response")```

### Create Filter Using Frequency Sampling Method

This example shows how to create a 2-D filter based on a desired frequency response using the frequency sampling method.

Given a matrix of points that define the shape of the frequency response, the frequency sampling method creates a filter whose frequency response passes through those points. Frequency sampling places no constraints on the behavior of the frequency response between the given points. Usually, the response ripples in these areas. Ripples are oscillations around a constant value. The frequency response of a practical filter often has ripples where the frequency response of an ideal filter is flat.

Define and display the target 2-D frequency response of an 11-by-11 filter.

```Hd = zeros(11,11); Hd(4:8,4:8) = 1; [f1,f2] = freqspace(11,"meshgrid"); mesh(f1,f2,Hd) title("Desired Frequency Response")```

Create the filter based on the target frequency response using the `fsamp2` function. `fsamp2` returns the filter `h` with a frequency response that passes through the points in the input matrix `Hd`

`h = fsamp2(Hd);`

Plot the frequency response of the filter.

```freqz2(h,[32 32]) title("Actual Frequency Response")```

Notice the ripples in the actual frequency response compared to the desired frequency response. These ripples are a fundamental problem with the frequency sampling design method. They occur wherever there are sharp transitions in the desired response.

You can reduce the spatial extent of the ripples by using a larger filter. However, a larger filter does not reduce the height of the ripples, and requires more computation time for filtering. To achieve a smoother approximation to the desired frequency response, consider using the frequency transformation method or the windowing method.

### Create Filter Using Windowing Method

The windowing method involves multiplying the ideal impulse response with a window function to generate a corresponding filter, which tapers the ideal impulse response. Like the frequency sampling method, the windowing method produces a filter whose frequency response approximates a desired frequency response. The windowing method, however, tends to produce better results than the frequency sampling method.

The toolbox provides two functions that design two-dimensionsal filters using a window-based filter design.

`fwind1` supports two different methods for making the two-dimensional windows:

• When you specify a single one-dimensional window, `fwind1` transforms the one-dimensional window using a process similar to rotation. The two-dimensional window is nearly circularly symmetric.

• When you specify two one-dimensional windows, `fwind1` computes their outer product. The two-dimensional window is rectangular and separable.

This example creates a two-dimensional window from a single one-dimensional window by using the `fwind1` function. First, define and display the target 2-D frequency response of an 11-by-11 filter.

```Hd = zeros(11,11); Hd(4:8,4:8) = 1; [f1,f2] = freqspace(11,"meshgrid"); mesh(f1,f2,Hd) axis([-1 1 -1 1 0 1.2]) title("Desired Frequency Response")```

Specify the one-dimensional window. The example uses the `hamming` function from the Signal Processing Toolbox.

`win1d = hamming(11);`

Create an 11-by-11 filter from the desired frequency response `Hd` and the one-dimensional window.

`h = fwind1(Hd,win1d);`

Display the frequency response of the filter.

```freqz2(h,[32 32]) axis([-1 1 -1 1 0 1.2]) title("Actual Frequency Response")```