MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Sam on 7 Feb 2013

I have an image that I am trying to do a 2D FFT on. The issue I'm having is that when I do the transformation, I get numbers which are extremely large and won't display correctly when I try to show the transform.

For example, I can read in a jpg or a png with nothing unusual about it, all numbers between 0 and 255. When I perform the transform, however, I get numbers on the order of 1E7, which is too large to be plotted without some adjustments. I have looked at some sample code online, and no one else seems to have to correct for the transform producing numbers which are too large.

Does anyone have any tips or information on how to correctly perform and FFT2, or let me know if I have to do a constant scaling factor on all of my FFTs, or any other sort of information?

Answer by Walter Roberson on 7 Feb 2013

I suspect that if you check, it will be one single location that has those large values: the location corresponding to the DC offset, which in turn corresponds to the average value of the data.

If you were to use im2double() on the image and then subtract off mean2() of the that, the result should have 0 for that average.

Show 1 older comment

Walter Roberson on 7 Feb 2013

You will probably need to display real() or imag() or abs() of fftA.

Also did you try imagesc() to automatically adjust the visualization?

imagesc(real(fftA))

Image Analyst on 7 Feb 2013

I usually take the log of the spectrum before displaying it because usually the DC spike is so tall that it suppresses all the other stuff, which is the more interesting stuff that you want to see. Taking the log lets you see all of it fairly well. Sam, I have a demo if you want where I demonstrate that.

Answer by Youssef KHMOU on 7 Feb 2013

Edited by Youssef KHMOU on 7 Feb 2013

Hi, as others mentioned above , when you perform the FFT2, the first element F(1,1) contains the sum of all elements of the 2d input signal, and you should use the abs, abs² or 10 (20) log10( abs) ,

The only thing i can tell you is that you can per example use the 2D AUTOCORRELATION FUNCTION, that you can use to even study the 2D AWGN .

I made a small tutorial (pdf file) on my submission about the 2D Autocorrelation function which actionly uses fftshift, fft2 and ifft 2 :

http://www.mathworks.com/matlabcentral/fileexchange/37624-2d-autocorrelation-function

i hope that helps

Answer by Youssef KHMOU on 8 Feb 2013

hi Sam,

In your question you said that you apply the transformation on png or jpg sample with numbers between 0 255, so its obviously uint8 class, but when you apply fft2 you get a warning :

Warning: FFTN on values of class UINT8 is obsolete. Use FFTN(DOUBLE(X)) or FFTN(SINGLE(X)) instead.

Here is an example of one way i use fft2 :

% First example I=imread('circuit.tif'); FI=fft2(double(I)); surf(10*log10(abs(fftshift(FI)))) figure, imagesc(10*log10(abs(fftshift(FI))));

If you are concerned with large values, you can normalize you result :

FI=FI/max(FI(:));

## 0 Comments