Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Fourier Transform from Phase and Magnitude - Matlab

The magnitude and phase of a fourier transform F are defined as:

Mag = sqrt(Real(F)^2 + Imaginary(F)^2)

and

Phase = arctan(Imaginary(F)/Real(F))

Ive tried to write matlab code that takes in a grayscale image matrix, performs fft2() on the matrix and then calculates the magnitude and phase from the transform. I then wish to calculate the imaginary and real parts of the fourier transform. This is done by rearranging the first two equations into:

Real = Mag/sqrt(1 + tan(Phase)^2)

and

Imaginary = Real*tan(Phase)

and finally combining and inverse fft2:

F = Real + i*Imaginary
image = ifft2(F)

I'd expect to see the same image as the input, but i get garbage. Is my maths wrong? My matlab mfile code is as follows:

function y = forwardBackwardFFT(image)

F = fft2(image);
mag = sqrt(real(F).^2 + imag(F).^2);
phase = atan(imag(F)./real(F));

re = sqrt((mag.^2)./(1 + tan(phase).^2));
im = re.*tan(phase);
F = re + i*im;
f = ifft2(F);

subplot(1,2,1);
imshow(image);
Title('Original Image');

subplot(1,2,2);
imshow(f);
Title('Image after forward and backward FFT');
y = f;

thanks a lot :)

like image 746
Dave Anderson Avatar asked Jun 18 '11 01:06

Dave Anderson


People also ask

How do you combine magnitude and phase in MATLAB?

For images in Matlab, it consist of a 2D complex array. You can create a 2D complex array merging the magnitude and phase like this: FreqDomain = abs(Y). *exp(i*angle(X));

How do you find the Fourier transform in MATLAB?

Compute the Fourier transform of the signal, and create the vector f that corresponds to the signal's sampling in frequency space. y = fft(x); fs = 1/Ts; f = (0:length(y)-1)*fs/length(y);


1 Answers

Your function is trying to test two things at once: (1) FFT and inverse FFT an image, and (2) disassemble a complex number into real and imaginary parts, transform to amplitude and phase, and then put it back together again. Instead of trying the whole thing at once and wondering why it doesn't work, you should test each of these two functions separately.

To test whether ifft(fft(image)) gives back the original image, you can just remove or comment out all the complex number manipulations:

function y = forwardBackwardFFT(image)

F = fft2(image);
%# stuff removed
f = ifft2(F);

subplot(1,2,1);
imshow(image);
title('Original Image');

subplot(1,2,2);
imshow(f, []);
title('Image after forward and backward FFT');
y = f;

This works. So the problem is with your complex number manipulations. Consider what happens when phase=0 or phase=pi/2. The tangent of 0 is 0, leading to a division by zero; and tan(pi/2) is infinite.

Here is some code that works:

mag =  sqrt(real(F).^2 + imag(F).^2);
phase = atan2(imag(F),real(F));

re = mag .* cos(phase);
im = mag .* sin(phase);
F = re + 1i*im;

You will have to do imagesc(abs(f)) in order to show the resulting inverse-transformed image, to get rid of a (nearly zero) imaginary component.

A more idiomatic way to get the magnitude and phase of a complex number is to simply do:

mag = abs(F);
phase = angle(F);

Hope this helps.

like image 93
nibot Avatar answered Oct 20 '22 06:10

nibot