Sei sulla pagina 1di 7

2019801003

CMPE530 Mathematical Fundamentals of


Artificial Intelligence
Ü. Aybüke Çalıkoğlu, 2019801003 12/03/2019

Assignment № 1

For this assignment discrete Fourier transform of several functions is computed using fft algorithm
in MATLAB. Furthermore, the functions are reconstructed using inverse discrete Fourier transform
algorithm (ifft).

1 FFT AND IFFT in MATLAB


1.1 Definition of Sinusoidal and Square Waves

Sinusoidal and square waves are defined in MATLAB as given below:

Listing 1: Sinusoidal and Square waves


1 f = 100 ; % Frequency of the signal
2 Tsignal = 1/f ; % Signal period
3 fs = f*20 ; % Sampling frequency
4 T = 1/fs; % Sampling period
5 k =10 ; % Number of signal period
6 L = k*(Tsignal)/T; % Length of the signal
7 t = ((0:L-1)*T); % Time vector
8

9 sinusoidalfunction = sin(2*pi*f*t); % Sinusoidal wave


10 squarewave = square(2*pi*f*t) ; % Square wave

As given in Listing 1, the frequencies of the periodic sinusoidal and square waves are defined as
100 s−1 and sampling frequencies are defined as 2000 s−1 . Figure 1 shows the defined signals.

Figure 1: Sinusoidal and Square waves

Assignment № 1 Page 1
2019801003

1.2 FFT with and without Zero Padding

Listing 2: FFT application with zero padding.


1 n = 2^nextpow2(L) ; % Zero padding
2 P1 = fftshift(fft(sinusoidalfunction,n)/L) ; % FFT of sinusoidal wave
3 P2 = fftshift(fft(squarewave,n)/L) ; % FFT of square wave
4 mag = abs(P1) ; % Magnitude of FFT transform of sin wave
5 mag2 = abs(P2) ; % Magnitude of FFT transform of square wave

Zero padding means padding zeros to the end of the signal in one domain to increase the
length of the signal. Zero padding is preferable for some reasons. One is to increase the size
of the signal to increase the resolution in the frequency domain and the other is to increase the
computation speed as fft algorithm works fast when the length of the signal is power of two.

(a)

(b)

Figure 2: (a) FFT with zero padding, (b) FFT without zero padding.

Figure 2 shows the difference between the fft results of zero padded signals and original signals.
As can be seen, the resolution is higher for the zero padded case.

Assignment № 1 Page 2
2019801003

1.3 IFFT of the waves

The sinusoidal and square waves are recomposed by inverse fast fourier transform of the zero
padded signals in the frequency domain. Inverse fft computations for sinusoidal and square waves
are given below:

Listing 3: Recomposing signals.


1 rewavesin = ifft(ifftshift(P1*L)) ;
2 rewavesquare = ifft(ifftshift(P2*L)) ;

Furthermore, recomposed signals are given in Figure 3. Additional zeros at the end of the signals
can be seen for these waves which are recomposed from the frequency domain.

Figure 3: IFFT of the zero padded waves.

2 FFT in Image Processing

FFT is an important image processing tool for processing digital images. For this assignment FFT
is used for basic image processing applications.

2.1 FFT of an Image

First, fft2 algorithm is used to transform the image in spatial domain to the frequency domain.

Listing 4: fft2 algorithm for image processing.


1 RGB=imread('diver1.jpg');
2 I=rgb2gray(RGB); % convert the image to grey
3 [M, N]=size(I); % image size
4

5 M2 =2^nextpow2(M) ; %zero padding


6 N2 =2^nextpow2(N) ;
7

8 A1 = fftshift(fft2(im2double(I),M2,N2)/(M*N)); % compute FFT of the grey image

Assignment № 1 Page 3
2019801003

MATLAB computation for Fourier transform of the digital image is given in Listing 4. Furthermore,
fft2 result for an example image is given in Figure 4.

(a)

(b)

Figure 4: (a) Original image, (b) FFT of the image.

2.2 Frequency Seperation

For Assignment №1, frequency separation of the example image is applied using the Fourier
Transform to separate the texture data from the color and luminance data of an image. MATLAB
computations for the filters can be seen in Listing 5. Here, Butterworth low pass filter is used to
obtain for low frequency information (color and luminance data).

Assignment № 1 Page 4
2019801003

As can be seen in Listing 5 Cut of frequencies of the filters are determined as 50 and the orders
of the filters are chosen as 5. The obtained filters in the frequency domain can be seen in Figure
5.

Listing 5: Butterworth filter.


1 R = 50 ; %Cut-off frequency of the LPF
2 n = 5 ; %Order of filter
3 X = 0:M2-1 ;
4 Y = 0:N2-1 ;
5 [X Y] = meshgrid(X,Y) ;
6 Cx = 0.5*M2 ;
7 Cy = 0.5*N2 ;
8 Lo = 1./(1+(((X-Cx).^2+(Y-Cy).^2)./(R).^2).^n) ; %Butterworth filter
9 Hi = 1-Lo ; % High pass filter=(1-low ...
pass filter)

(a) (b)

Figure 5: (a) Butterworth LPF, (b) Butterworth HPF.

Low pass filter and high pass filter are applied to an image by multiplication in the frequency
domain. The low pass filtered and high pass filtered image are reconstructed by inverse discrete
fourier transform in MATLAB. MATLAB computations for ifft2 can be seen in Listing 6.

Listing 6: Frequency Seperation with BLPF and BHPF.


1 %Low pass filtered image
2 LPF_image = M*N*ifft2(ifftshift(A1.*Lo)) ;
3 %High pass filtered image
4 HPF_image = M*N*ifft2(ifftshift(A1.*Hi) ;
5 %Reconstructed image
6 Re_image = M*N*ifft2(ifftshift(A1.*Lo+A1.*Hi)) ;

Furthermore, the original signal is recomposed by inverse discrete fourier transform of the sum-
mation of low pass filtered and high pass filtered images in the frequency domain. Recomposed
low pass filtered image which carries color and luminescence information, high pass filtered image
which carries texture information and summation of the images can be seen in Figure 6.

Assignment № 1 Page 5
2019801003

(a)

(b)

(c)

Figure 6: (a) LP filtered image, (b) HP filtered image, (c) The image reconstructed by inverse
transformation of summation of filtered images in the frequency domain.

Assignment № 1 Page 6
2019801003

2.3 Blurring and Sharpening Images

For this assignment, basic filter is applied to an image in the frequency domain to change the
sharpness of the figure. In this manner, high frequency component of the example image is
scaled up for sharpening and scaled down for blurring effect. Then, manipulated high frequency
component is added to a low frequency component and image is recomposed by inverse discrete
fourier transform.
Listing 7: Sample Bash code.
1 Blurfilter = Hi.*0.5 ;
2 Sharpfilter = Hi.*2 ;
3 %%Blurring
4 Softer_image = M*N*ifft2(ifftshift(A1.*(Lo + Blurfilter)) ;
5 %%Sharpening
6 Sharper_image = M*N*ifft2(ifftshift(A1.*(Lo + Sharpfilter)) ;

MATLAB computations for the explained filter application is given in Listing 7. Furthermore, ob-
tained blurred and sharpened images are given in Figure 7.

(a)

(b)

Figure 7: (a) Blurred image, (b) Sharpened image.

Assignment № 1 Page 7

Potrebbero piacerti anche