Ideal Low Pass Filter Concept in MATLAB

6 views (last 30 days)
Premnath
Premnath on 22 Oct 2013
Answered: Hari on 24 Feb 2025
Please help me understand the following MATLAB code for Ideal Low pass filter. I am unable to understand the Part2 in the below code. Please explain me why we are doing like this.
%IDEAL LOW-PASS FILTER
%Part 1
function idealfilter(X,P) % X is the input image and P is the cut-off freq
f=imread(X); % reading an image X
[M,N]=size(f); % Saving the the rows of X in M and columns in N
F=fft2(double(f)); % Taking Fourier transform to the input image
%Part 2 % I don't understand this part
u=0:(M-1);
v=0:(N-1);
idx=find(u>M/2);
u(idx)=u(idx)-M;
idy=find(v>N/2);
v(idy)=v(idy)-N;
[V,U]=meshgrid(v,u);
D=sqrt(U.^2+V.^2);
%Part 3
H=double(D<=P); % Comparing with the cut-off frequency
G=H.*F; % Convolution with the Fourier transformed image
g=real(ifft2(double(G))); % Inverse Fourier transform
imshow(f),figure,imshow(g,[ ]); % Displaying input and output image
end
I tried to run each commands in Part2 individually for M= 8 and N=8. I get
u=0:(M-1); ==> u = 0 1 2 3 4 5 6 7
v=0:(N-1); ==> v = 0 1 2 3 4 5 6 7
idx=find(u>M/2); ==> idx = 6 7 8
u(idx)=u(idx)-M; ==> 0 1 2 3 4 -3 -2 -1
idy=find(v>N/2); ==> idy = 6 7 8
v(idy)=v(idy)-N; ==> 0 1 2 3 4 -3 -2 -1
[V,U]=meshgrid(v,u); ==>
V=
0 1 2 3 4 -3 -2 -1
0 1 2 3 4 -3 -2 -1
0 1 2 3 4 -3 -2 -1
0 1 2 3 4 -3 -2 -1
0 1 2 3 4 -3 -2 -1
0 1 2 3 4 -3 -2 -1
0 1 2 3 4 -3 -2 -1
0 1 2 3 4 -3 -2 -1
U =
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4
-3 -3 -3 -3 -3 -3 -3 -3
-2 -2 -2 -2 -2 -2 -2 -2
Please help me understand this program.. If possible explain this with an example.

Answers (1)

Hari
Hari on 24 Feb 2025
Hi Premnath,
I understand that you want to uderstand the Part 2 of the code for an Ideal Low-Pass Filter, focusing on how it manipulates frequency indices and sets up the frequency domain.
Here is the explantion of the important steps of the code you have provided:
Frequency Indexing:
This part of the code generates frequency index vectors u and v for the image dimensions. It then adjusts indices greater than half the size to negative values, effectively centering the zero frequency.
u = 0:(M-1); v = 0:(N-1);
u(u > M/2) = u(u > M/2) - M;
v(v > N/2) = v(v > N/2) - N;
Meshgrid Creation:
Here, the code uses “meshgrid” to create a 2D grid of frequency coordinates. This grid is used to map out the frequency space, allowing for further calculations.
[V, U] = meshgrid(v, u);
Distance Calculation:
This section calculates the Euclidean distance D from the origin for each point in the frequency domain. This distance helps in defining the boundary of the filter.
D = sqrt(U.^2 + V.^2);
Example Explanation:
For an 8x8 matrix, this code shifts the frequency indices so that they range symmetrically around zero, like turning u into [0 1 2 3 4 -3 -2 -1], which is crucial for centering.
Purpose in Filtering:
By setting up D, the code allows you to apply a circular filter, retaining frequencies within a specified cutoff P and attenuating others, effectively filtering the image.
Refer to the documentation of “meshgrid” function to know more about its usage:
Hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!