title: Getting Fourier Transform from Phase and Magnitude - Matlab
body: <p>The magnitude and phase of a fourier transform F are defined as:</p>
  
  <pre><code>Mag = sqrt(Real(F)^2 + Imaginary(F)^2)
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Phase = arctan(Imaginary(F)/Real(F))
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>Real = Mag/sqrt(1 + tan(Phase)^2)
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Imaginary = Real*tan(Phase)
  </code></pre>
  
  <p>and finally combining and inverse fft2:</p>
  
  <pre><code>F = Real + i*Imaginary
  image = ifft2(F)
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>thanks a lot :)</p>