tag:blogger.com,1999:blog-3115107570196929760.post2122068703546595737..comments2018-01-05T23:26:47.984-08:00Comments on Reflections: Interplay of fft, ifft, fftshift, and ifftshift in MATLABShalin Mehtanoreply@blogger.comBlogger8125tag:blogger.com,1999:blog-3115107570196929760.post-57835968153464925742014-04-28T23:08:43.003-07:002014-04-28T23:08:43.003-07:00what information do we get from phase spectrum abo...what information do we get from phase spectrum about an image? my code is<br /><br />i=imread('C:\Users\Public\Pictures\Sample Pictures\Lena.png');<br />i=rgb2gray(i);<br />figure,imshow((i));<br /><br />f=fftshift(fftn(i));<br />mag=abs(log(1+f));<br />figure,imshow(mag,[]);<br /><br />phase=angle(f);<br />figure,imshow(phase,[]);ramdas patilhttps://www.blogger.com/profile/13122270513709795576noreply@blogger.comtag:blogger.com,1999:blog-3115107570196929760.post-72210204747895509982011-12-29T21:12:56.204-08:002011-12-29T21:12:56.204-08:00Hi Jon,
You do not get real spectrum, because, acc...Hi Jon,<br />You do not get real spectrum, because, according to MATLAB’s convention, you do not have a function that is even around the center pixel.<br />Sound surprising, isn’t it? This took me some time to figure out.<br />For MATLAB fft-related functions, the center pixel is ceil(L/2)+1. The center of the 4×4 matrix is therefore, the pixel (3,3), not the ‘intersection’ of four pixels in the middle.<br />If you run following code, you notice that pixel (3,3) becomes (1,1) after ifftshift. The role of ifftshift is to move the center pixel to the first index in the matrix.<br /><br />H=zeros(4,4);<br />H(3,3)=1;<br />imagesc(ifftshift(H)); axis equal; axis tight;<br /><br />In case of even-sized matrix, the evenly symmetric function needs to be symmetric around the central pixel defined as above. If you run the following code for 4×4 matrix, you notice that ifft2 returns the spectrum with null-imaginary part and that the spectrum is centered around the pixel 3×3.<br /><br />H = zeros(4,4);<br />H(2:4,2:4) = 1;<br />T = fftshift(ifft2(ifftshift(H)));<br /><br />subplot(2,2,1), imagesc(H); colorbar; colormap gray; title(‘H’); axis equal; axis tight;<br />subplot(2,2,2), imagesc(ifftshift(H)); colorbar; colormap gray; title(‘H’); axis equal; axis tight;<br />subplot(2,2,3), imagesc(real(T)/max(max(abs(T)))); colorbar; colormap gray; title(‘Real Part’); axis equal; axis tight;<br />subplot(2,2,4), imagesc(imag(T)/max(max(abs(T)))); colorbar; colormap gray; title(‘Imaginary Part’); axis equal; axis tight;<br /><br />This odd definition of the center for even-sized matrices looks disturbing for 4×4 matrix. But, if you have a 1024×1024 matrix, this one-pixel shift in center won’t be noticed much. To get real and even spectrum, you will need to define your even function around the central pixel (513,513).Shalin Mehtahttps://www.blogger.com/profile/05632535422779114436noreply@blogger.comtag:blogger.com,1999:blog-3115107570196929760.post-53055835618377515412010-06-07T13:19:10.000-07:002010-06-07T13:19:10.000-07:00I just stumbled on this issue too after years of u...I just stumbled on this issue too after years of using the fft2 function "incorrectly" (I usually always needed intensity PSFs and hence this has never affected me). I was recently playing around with amplitude PSFs and anomalies in my coding emerged. My questions then is, if you're increasing the sampling for the fft2 using the 2nd and 3rd input parameters of the fft2 function, i.e. pupil4 = fftshift(fft2(ifftshift(psf3) , SAMPLING_X, SAMPLING_Y)); where SAMPLING_* is greater than the size of the original matrix, this will inevitably give the wrong phase. Does this mean that padding with zeros in one domain needs to be done manually to ensure function is still at the centre of matric before using fftshift?Stevenoreply@blogger.comtag:blogger.com,1999:blog-3115107570196929760.post-50328371606664517202010-06-07T13:50:35.000-07:002010-06-07T13:50:35.000-07:00@Steve,You are right. To increase the sampling rat...@Steve,<br>You are right. To increase the sampling rate in the pupil plane, we need to pad the psf with zeros. But the padding should be done so that the PSF is still centered, otherwise we have changed the phase in the space domain. Rather than using 2nd and 3rd arguments of fft2 to increase the sampling rate, I rely on padarray function with optional argument 'both', which pads the matrix on both sides by equal amount.Shalin Mehtanoreply@blogger.comtag:blogger.com,1999:blog-3115107570196929760.post-75709339211298640482010-06-07T14:24:35.000-07:002010-06-07T14:24:35.000-07:00padarray is new to me - thanks, this saves me some...padarray is new to me - thanks, this saves me some coding.Stevenoreply@blogger.comtag:blogger.com,1999:blog-3115107570196929760.post-51589744320704247962010-07-20T14:44:03.000-07:002010-07-20T14:44:03.000-07:00Hello everyone,I found an interesting discussion a...Hello everyone,<br><br>I found an interesting discussion at http://www.mathworks.com/matlabcentral/newsreader/view_thread/285244<br><br>apparently, if the size of the array is even,<br><br> H = fftshift( fft( h ) ); % most common usage, wrong result<br> H = fftshift( fft( ifftshift( h ) ) ); % uncommon usage, correct result<br><br>This is if we consider h already in the natural order....<br><br>What do you think? it should work even if the size of the matrix is odd.....<br><br>thanks<br>KavanKavannoreply@blogger.comtag:blogger.com,1999:blog-3115107570196929760.post-40006001385825858002010-07-29T15:15:43.000-07:002010-07-29T15:15:43.000-07:00Hi Kavan, Yes the expression H=fftshift( fft( ifft...Hi Kavan, Yes the expression H=fftshift( fft( ifftshift( h ) ) ); will give correct phase for both even and odd-length sequence.<br><br>A secondary but perhaps useful point is that for even length sequence fftshift(fftshift(x)) returns x, but for odd-length sequence it does not. The difference can be seen from the following:<br><br>xeven=zeros(1,20); x(11)=1;<br>xodd=zeros(1,21); x(11)=1;<br>subpot(221); stem(xeven); subplot(222); stem(fftshift(fftshift(xeven)));<br>subplot(223); stem(xodd); subplot(224); stem(fftshift(fftshift(xodd)));<br><br>If we apply fftshift twice on the odd sequence, The 11th (or center) value is shifted to 10th for odd-length sequence.Shalin Mehtanoreply@blogger.comtag:blogger.com,1999:blog-3115107570196929760.post-79443421350326204172011-05-19T14:05:22.000-07:002011-05-19T14:05:22.000-07:00Hi,I've run into a similar problem that you...Hi,<br><br>I've run into a similar problem that you've discussed here and I thought your description solved my problem, but apparently I'm still missing something.<br><br>Take the simplest real and symmetric function I can describe:<br>H = zeros(4,4);<br>H(2:3,2:3) = 1;<br><br>It's basically four 1's forming a square padded by one ring of zeros. It looks similar to the data you showed already.<br><br><br>Run the following and you do not get a zero imaginary part for the data I showed you (Despite it being real and even). Change the data such that you have a 3x3, or 5x5, or any rectangular aperture with odd samples, for any amount of padding, and you get zero phase.<br><br>I'm "stuck" with an even number of samples (via the hardware I'm using) so I need to find a solution in software. Any thoughts?<br><br>T = fftshift(ifft2(ifftshift(H)));<br><br>subplot(2,2,1), surf(real(T)/max(max(abs(T)))); colorbar; colormap gray; shading interp; title('Real Part'); <br>subplot(2,2,2), surf(imag(T)/max(max(abs(T)))); colorbar; colormap gray; shading interp; title('Imaginary Part');<br>subplot(2,2,3), surf(abs(T)/max(max(abs(T)))); colorbar; colormap gray; shading interp; title('Magnitude'); <br>subplot(2,2,4), surf(angle(T)); colorbar; colormap gray; view(2); shading interp; title('Phase');Jonhttp://n/anoreply@blogger.com