# How can I generate an ideal 2D low-pass filter in MATLAB?

I've been tasked with creating a 32 x 32 half-band low-pass image filter in MATLAB. My thinking is to generate the ideal filter mask in the frequency domain and compute the corresponding convolution mask using the inverse FFT. I first generate the filter in the frequency domain.

filter = zeros(32);
filter (1:8, 1:8) = 1;
filter (1:8, 24:32) = 1;
filter (24:32, 1:8) = 1;
filter (24:32, 24:32) = 1;


This filter turns out to be the following in the frequency domain. I've confirmed my MATLAB code produces this pattern which is symmetric. Note, I'm assuming I need to define the mask with the frequency ranges from 0 -> 2pi rad/sec, hence putting the "ones" in the corners.

I then generate the convolution mask using the "iift2" MATLAB function. However, this mask turns out to be complex which raises some red flags. I would expect it to be a real filter.

mask = ifft2(filter)


When I convolve this filter mask with my image however, I get some weird artifacts.

image_filtered = imfilter(image_orig, mask); I'm curious if there is some flaw in my thinking here.

• Does this answer your question? Why is it a bad idea to filter by zeroing out FFT bins? – Marcus Müller Feb 7 at 20:50
• all the things said for 1D filters there 100% directly translate to 2D filters, plus you forgot the circular nature of DFT convolution. – Marcus Müller Feb 7 at 20:51
• also "ideal low-pass filter" has a specific meaning, and that is a low-pass filter with infinitely steep transition, which would be infinite in size and hence can not be implemented. – Marcus Müller Feb 7 at 20:52
• @MarcusMüller I'm more interested in whether or not the fourier mask I've defined is correct and why I would be getting a complex convolution mask when performing the IFFT. Ideally, I would like the IFFT to create a 2D signal that entirely real. – Izzo Feb 8 at 18:06
• again, no, you didn't define that mask correctly for any practical filtering problem. – Marcus Müller Feb 8 at 18:24