0
votes

So I have this matrix M=(50,50,250) and I want to get the FFT2 of the slices S=(50,50) along the 3rd dimension = 250.

Let's say I do

FT = fftshift(fft2(M));

Is this calculating the FT as I want?

Because in the function description it says the function returns the 2-D DFT for each higher dimensional slice of X. For example, if size(X) = [100 100 3], then fft2 computes the DFT of X(:,:,1), X(:,:,2) and X(:,:,3).

So, I am assuming it is computing the FT of the slices S=(50x250) along the 1st dimension = 50.

Can someone clear this to me?

1
Is this calculating the FT as I want? Yes. So, I am assuming it is computing the FT of the slices S=(50x250) along the 1st dimension = 50. Sort of, it is calculating the slice 50*50 along the 3rd dimension for 250 frames/units.GameOfThrows
Arg, fftshift! Prakhar’s answer tells you how to use its second argument to tell it what dimension to shift in, but I have run into so many bugs by forgetting that second argument to fftshift that I made my own i/fftshift function that errors if you don’t give it a second argument when the first argument is 2D/ND array!Ahmed Fasih
But doesn't matlab fftshift function already works for a fft2 without you having to take those steps of doing it separately for each dimension?Andre

1 Answers

3
votes

I think it is quite clear from the documentation. fft2 computes 250 2D DFTs, one for each of the 50x50 slices in M. So your FT(:, :, i) is a 2D DFT of M(:, :, i). It behaves same as:

FT = zeros(size(M));
for i = 1 : size(M, 3)
    FT(:, :, i) = fft2(M(:, :, i));
end

You could verify this as follows (error should be very small):

FT1 = fft2(M);
error = norm(abs(FT1(:) - FT(:)));

However, the behavior of fftshift is not consistent with what you want. You should instead use the second argument of fftshift to shift the result along specific dimensions (1 and 2 in your case):

FT = fftshift(fftshift(fft2(M), 1), 2);