# 2D Fourier transform of a table - ArrayPlot issue

Recently, I have encountered an issue with ArrayPlot after performing a Fourier transform of a table.

Picture presented above is an ArrayPlot of a 2D table. Using Fourier on this 2D table I obtain the following ArrayPlot:

After this procedure one ends up with the result which consists of data at four vertices of the square. The thing is that I would like the result to be in this form:

I have tried different Fourier transform parameters but none of them had delivered the demanded image.

-
I guess what you're looking for is an analogue of Matlab's fftshift. It's not really an issue with ArrayPlot, it's an question of visualizing the result of the Fourier transform. – Rahul Mar 7 '14 at 12:11

This is really ancient code that should do what you need. It was written on MMA version 3 or 4 (or 5??) - I don't remember - and I have not tested it on newer versions. If it does not work you'll probably have to find a newer procedure to extract the dimensions from an image.

So, here's the code, it works on a matrix of values and gives you a matrix that represents the (generally complex) FFT you are after:

FFT2D[mat_] := Module[{ft, hshift, wshift},
ft = Fourier[mat];
{hshift, wshift} = IntegerPart[(# + .5)/2] & /@ Dimensions[ft];
Transpose[RotateLeft[
Transpose[RotateLeft[
ft,
hshift]],
wshift]]
]


This is how I used it back then (again, you might have to adjust the code, possibly by changing ListDensityPlot with ArrayPlot)

mat = GrayScaleMatrix["Image1.bmp"];
(* this was a silly conversion from color to grayscale, see later *)
{hshift, wshift} = IntegerPart[(# + .5)/2] & /@ Dimensions[mat]
(* this is just extracting the dimensions of the image*)
ListDensityPlot[mat, MeshRange -> {{-wshift, wshift}, {-hshift, hshift}}]
(* and this is plotting it to show the gray scaled image *)


Instead of Fourier, I would use FFT2D. That's it.

fft = FFT2D[mat];


Plotting might require the previously extracted information about the image's dimensions.

ListDensityPlot[Abs[fft], MeshRange -> {{-wshift, wshift}, {-hshift, hshift}}]


Addendum - And this is the procedure I used to convert color bitmaps into gray scale ones (I wanted to experiment with actual pictures and I did not have the time to figure out how to properly create a gray scaled version). I include it here just in case you want to try with the actual data it has worked for me

GrayScaleMatrix[filename_String] := Module[{pic},
pic = Import[filename];
pic[[1, 1]] /. {r_, g_, b_} :> (.3r + .6g + .1b)
]

-
RotateLeft can rotate rows and columns in one go: RotateLeft[ft, {hshift, wshift}] – Simon Woods Mar 7 '14 at 17:17

You schould always share some code :)

pic = Import["http://i.stack.imgur.com/y1IuJ.png"]
dat = ImageData@ColorConvert[pic, "GrayScale"];
Dimensions@dat


img // Image // ImageCrop[#, {60, 30}] & // ColorNegate