I don't have time to debug your code right now, but I suggest you check all your output with a known maths package like MATLAB. I believe MATLAB has 2D FFT transforms available. It's also very easy to use.
P.S. Is this for image analysis?
P.P.S. I'm pretty sure there's a lot of literature available on this topic. You could have a look at some journals if general web pages are not sufficient.