Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

Getting Fourier Transform from Phase and Magnitude - Matlab

Date: 2023-02-22 14:00:31

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 :) 

Answer:

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.


Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com