2
votes

I am currently using the following matlab function:

function out = fft_2d(in)

out = fftshift(fft2(ifftshift(in)));

As I understand it, this takes a "natural order" input, in, and "swaps" it to be passed to fft2, and then shifts the result of fft2 again using fftshift to give me back the natural ordering output. is this correct?

I am porting this code to C, and I want to use CUFFT to do this. According to the docs, I think I would use:

/* Create a 2D FFT plan. */
cufftPlan2d(&plan, NX, NY, CUFFT_C2R);
/* Use the CUFFT plan to transform the signal out of place. */
cufftExecC2R(plan, idata, odata);

But what kind of shifting will I have to do to the data coming out of cufftExecC2R? Also, does odata need to be a NX*NY block of contiguous data? Does it have to be in column or row major order? Row i'd guess, since thats what C is.

Thanks

1
I am not sure what ifftshift does and why you need it but the cuda fft part looks fine.fabrizioM
according to matlab help it undoes the results of fftshiftDerek

1 Answers

2
votes

Input to CUDA FFT:

Pad width to cell(width/2+1)*2 due to complex format in frequency domain. This initial padding will be size of result image --> must crop result.

Then pad to whole power of 2 * whole power of 2 real float matrix (zero padded from right and bottom).

Output, r,i,r,i,... even complex float values (real column, imaginary column, real column, ...), zero padded around center.

Use complex multiplication in frequency domain, not regular one.

After IFFT, crop sides of images to receive height * ceil(width/2+1)*2 center. Crop again to remove possible extra line at right if any (crop to height*width).

Do not forget to FIT-shift. I can not remember for certain when, So try shifting after ifft snd if result is wrong, then after fft.

Try multiplying with delta kernel for testing.

Kernel should be padded around center, not corner.

For even matrices, center is half a cell right and under center.

You cab write custom CUDA kernel to do padding and another to do unpaddings and shift in one go.