Multimedia-Lecture-five
Fourier
Transformations
Eng: Noor Alhakim
Fourier Transformations
Fast Fourier Transformations (FFT)
on Signals using MATLAB
o FFT on random signal
o FFT on audio signal
o FFTshift on audio signal
o FFT on image signal
o FFTshift on image signal
o Lowpass filter on image using FFT, IFFT
o Highpass filter on image using FFT, IFFT
FFT on random signal
Example:
Define a random signal from three Use FFT function to convert from time domain
signals with 10 30 70 frequencies. to frequency domain
>> Y=fft(X,n)
Tip: Assume random
signal is sum of three X: data of signal
signals with different n: length of that signal
frequencies
%% Define a random signal
Fs=1000;
Ts=1/Fs;
time=[0:Ts:2-Ts];
freq=[10,30,70];
Amp=5;
for i=1:length(freq)
y(i,:)=Amp*sin(2*pi*freq(i)*time);
end
y_random=y(1,:)+y(2,:)+y(3,:)
subplot(4,1,1)
plot(time,y(1,:))
title('signal 1 in the Time Domain')
subplot(4,1,2)
plot(time,y(2,:))
title('signal 2 in the Time Domain')
subplot(4,1,3)
plot(time,y(3,:))
title('signal 3 in the Time Domain')
subplot(4,1,4)
plot(time,y_random)
title('total of three signals')
%% FFT on a random signal
%% length of signal
nfft=length(y_random)
%% length of signal nearest to power 2
nfft2=2^nextpow2(nfft)
%% apply FFT
FF=fft(y_random,nfft2/2)
%% plot the signal
figure
plot(abs(FF))
title('signal in the Frequency Domain')
Try to zoom in for each
signal to check their
frequencies which same
or too close from
frequencies in time
domain
FFT on 1D signal
Audio
FFT on audio signal
Example:
Read an audio file, then apply FFT
function on audio signal
%% FFT for audio signal
[y,fs]=audioread('test1.wav');
nfft=length(y);
%% length of signal nearest to power 2
nfft2=2^nextpow2(nfft);
%% apply FFT
FF=fft(y,nfft2/2);
%% plot the signal
figure
plot(abs(FF))
title('signal in the Frequency Domain')
FFTshift on audio signal
Use fftshift function to rearranges
the outputs of fft by moving the
zero-frequency component to the
center of the array
>>fftshift(fftsignal)
Apply fftshift function on
the previous example
audio signal and see the
result
FFT on 2D signal
Image in gray scale
FFT on image signal
Example:
Read an image in gray scale, then apply
FFT2 function on image signal
im=imread('Lenna.png');
I=rgb2gray(im); %% convert to grayscale
subplot(2,1,1);
imshow(I)
title('Orginal image')
%% FFT2 for image signal
ff=fft2(I);
Ab=abs(ff); %% Magnitude
%% Normalized to display the magnitude
Ab=(Ab-min(min(Ab)))./(max(max(Ab))).*255;
subplot(2,1,2);
imshow(Ab)
title('signal in the Frequency Domain using FFT2')
FFTshift on image signal
Use fftshift function to shift low
frequencies to center for the previous
example
>>fftshift(fftsignal)
%% fftshift for image signal
ffs=fftshift(ff);
Ab=abs(ffs);
Ab=(Ab-min(min(Ab)))./(max(max(Ab))).*255;
figure
imshow(Ab)
title('signal in the Frequency Domain using fftshift')
Also you can see the same
result with Log
Transformation as this
%% fftshift for image signal
ffs=fftshift(ff);
Ab=log(1+abs(ffs));
figure
imshow(Ab,[])
title('signal with Log Transformation')
Low/High Pass Filter on Image Using FFT
Low pass Filter (LPF): High pass Filter (HPF):
removes high-frequency noise from enhances the fine details and highlight the
a digital image and preserves low- edges in a digital image.
frequency components.
Note: Low frequencies are Note: High frequencies are represent
represent smooth parts of the rough parts (such as contours,
the image lines and so on)
% Low Pass Filter % Comparing with the cut-off frequency and
input_image = rgb2gray(imread(Lenna.png')); % determining the filtering mask
H = double(D <= D0);
[M, N] = size(input_image);
% Convolution between the Fourier Transformed
FT_img = fft2(double(input_image)); % image and the mask
G = H.*FT_img;
% Assign Cut-off Frequency
D0 = 30; % one can change this value accordingly % ifft2 (2D inverse fast fourier transform)
output_image = real(ifft2(double(G)));
% Designing filter
u = 0:(M-1); % Displaying Input Image and Output Image
idx = find(u>M/2); subplot(2, 1, 1), imshow(input_image),
u(idx) = u(idx)-M; subplot(2, 1, 2), imshow(output_image, [ ]);
v = 0:(N-1);
idy = find(v>N/2);
v(idy) = v(idy)-N;
% function meshgrid(v, u) returns
% 2D grid which contains the coordinates of vectors
% v and u. Matrix V with each row is a copy
% of v, and matrix U with each column is a copy of u
[V, U] = meshgrid(v, u);
% Calculating Euclidean Distance
D = sqrt(U.^2+V.^2);
% High Pass Filter % Comparing with the cut-off frequency and
input_image = rgb2gray(imread(‘Lenna.png')); % determining the filtering mask
H = double(D > D0);
[M, N] = size(input_image);
% Convolution between the Fourier Transformed image and the mask
FT_img = fft2(double(input_image)); G = H.*FT_img;
% Assign Cut-off Frequency % ifft2 (2D inverse fast fourier transform)
D0 = 10; % one can change this value accordingly output_image = real(ifft2(double(G)));
% Designing filter % Displaying Input Image and Output Image
u = 0:(M-1); subplot(2, 1, 1), imshow(input_image),
idx = find(u>M/2); subplot(2, 1, 2), imshow(output_image, [ ]);
u(idx) = u(idx)-M;
v = 0:(N-1);
idy = find(v>N/2);
v(idy) = v(idy)-N;
% MATLAB library function meshgrid(v, u) returns 2D grid
% which contains the coordinates of vectors v and u.
% Matrix V with each row is a copy of v, and matrix U
% with each column is a copy of u
[V, U] = meshgrid(v, u);
% Calculating Euclidean Distance
D = sqrt(U.^2+V.^2);
That’s All