Beruflich Dokumente
Kultur Dokumente
n −1 n −1
F (k ) = ∑ e − i (2π k ) j / n f ( j ) = ∑ e − i (2π / n ) kj f ( j ).
j =0 j =0
Three very important properties include trig functions, inverse discrete Fourier
transforms and the convolution identity. These allowed us to formulate a “filter” to purge
images of unwanted periodic noise, see the Matlab code fftsine.m
In this lecture we will extend this to 2D images where f is now a nx x ny matrix
with components f(jx, jy), 0 ≤ jx ≤ nx – 1 and 0 ≤ jy ≤ ny – 1. The 2D discrete
Fourier transform of a matrix f is another matrix of the same dimension whose (kx, ky)
component is
ny −1 nx −1
F (kx, ky ) = ∑ ∑e
jy = 0 jx = 0
− i [(2π / nx ) kx jx + (2π / ny ) ky jy ]
f ( jx, jy ).
1 ny −1 nx −1
f ( jx, jy ) = ∑
nx ny ky =0
∑e
kx = 0
i [(2π / nx ) kx jx + (2π / ny ) ky jy ]
F (kx, ky ).
Both the 2D discrete Fourier transform and its inverse can easily be computed by
using the Matlab commands fft2() and ifft2(). For example for nx = 2 and ny = 3:
a= 1 2 5
6 7 11
>> ffta = fft2(a)
>> ifft2(ffta)
ans = 1 2 5
6 7 11.
As in the 1D Fourier transform of the sine and cosine functions, the Fourier
transforms will identify the frequencies as “spikes” in the matrix of the Fourier
transforms. Let x = 0: 1/nx:1-1/nx and y = 0: 1/ny:1-1/ny.
The discrete Fourier transform of cos(2π(fx x + fy y)) is
nx ny
, when (kx, ky ) = ± ( f x , f y )
2
0, when (kx, ky ) ≠ ±( f x , f y ).
nx ny
i, when (kx, ky ) = −( f x , f y )
2
nx ny
− i, when (kx, ky ) = ( f x , f y )
2
0, when (kx, ky ) ≠ ±( f x , f y ).
The following Matlab code illustrates the above properties. Here the padded fast
Fourier transform has been used, and the frequency domains have been shifted so that the
smallest frequencies are in the center. The sine and cosine functions have different
amplitudes, which may be identified in the mesh() plot of log(1+abs(fftu)) where fftu is
the Fourier transform.
Matlab Code ffttrig2d.m
nx = 500;
ny = 500;
for i = 1:nx
for j=1:ny
u(i,j) = 1 + ...
2^0*(1+sin(2*pi*((i-1)/nx)*200))+...
2^2.*(1+sin(2*pi*((j-1)/ny)*200))+...
2^4.*(1+cos(2*pi*((i-1)/nx+(j-1)/ny)*141))+...
2^6.*(1+sin(2*pi*((i-1)/nx-(j-1)/ny)*141));
end
end
fftu = fft2(u,2*nx-1,2*ny-1);
fftu = fftshift(fftu);
mesh(log(1+abs(fftu)));
The convolution identity also generalizes to the 2D Fourier transform. Let a be a
nx x ny matrix with components a(i,j). The polynomial of two variables x and y
associated with this matrix is
ny −1 nx −1
pa ( x, y ) = ∑ ∑ a (i, j ) xi y j .
j =0 i =0
pconv 2( a ,b ) ( x, y ) = pa ( x, y ) pb ( x, y ).
>> a = [1 2 5; 6 7 11]
1 2 5
6 7 11
>> b = [2 5 9; 1 2 8]
2 5 9
1 2 8
>> conv2(a,b)
2 9 29 43 45
13 48 128 144 139
6 19 73 78 88.
88 = 11*8 = a(1,2)*b(1,2).
The following Matlab calculation verifies this for the above two matrices:
>> fft2(conv2(a,b))
1.0e+002 *
Columns 1 through 4
Column 5
-2.7193 - 2.0698i
0.5290 + 0.9709i
1.0034 - 0.1752i
>> norm(fft2(conv2(a,b))-fft2(a,3,5).*fft2(b,3,5))
4.8969e-014.
The convolution identity can be restated by taking the inverse Fourier transform
of both sides
If b is an image, then its Fourier transform will reflect the frequencies of the periodic
parts of the image. By masking or filtering out the unwanted frequencies one can
obtained a new image by applying the inverse Fourier transformation. A filter is a matrix
with the same dimension as the Fourier transform of the padded image. The components
of the filter usually vary from 0 to 1. If the component is 1, then the frequency is allowed
to pass; if the component is 0, then the frequency is tossed out. Let Filter represent such a
matrix. Then the filtered image is Newb
The following Matlab code illustrates this for a low frequency sine wave with higher
frequency sine “noise.”
clear;
nx = 200;
ny = 600;
for i = 1:nx % Define the big wave with periodic noise.
for j = 1:ny
u(i,j) = 0+100*(1+sin(2*pi*((j-1)/ny)*5)) + ...
15.*(1+sin(2*pi*((i-1)/nx)*80))+...
15.*(1+sin(2*pi*((j-1)/ny)*80));
end
end
sine = uint8(u);
imwrite(sine, 'sine.jpg');
fftu = fft2(u,2*nx-1,2*ny-1); % Padded fft.
fftu = fftshift(fftu);
figure(1);
mesh(u');
figure(2)
mesh(log(1+(abs(fftu))));
filter = ones(2*nx-1,2*ny-1);
d0 = 150; % Use ideal low pass filter.
for i = 1:2*nx-1
for j =1:2*ny-1
dist = ((i-(nx+1))^2 + (j-(ny+1))^2)^.5;
if dist > d0
filter(i,j) = 0;
end
end
end
figure(3)
mesh(filter);
fil_sine = filter.*fftu;
figure(4)
mesh(log(1+abs(fil_sine)));
fil_sine = ifftshift(fil_sine);
fil_sine = ifft2(fil_sine,2*nx-1,2*ny-1);
fil_sine = real(fil_sine(1:nx,1:ny));
fil_sine = uint8(fil_sine);
imwrite(fil_sine, 'sine_fil.jpg');
Sine Wave with Noise Fourier Transform of Noisy Sine Wave