2015-09-29 36 views
-1

我需要使用fftw庫在C++中執行兩步ifft3d。fft3d,交換切片,需要轉置?

1st step: a one dimensional ifft across third dimension, creating a "hybrid_kspace" 
... 
[some operations with hybrid_kspace] 
... 
2nd step: a two dimensional ifft over horizontal slices of hybrid_kspace. 

根據這個(https://cmb.ornl.gov/members/z8g/csproject-report.pdf)我必須執行某種置操作的,但我真的不明白這一點。

所以我試圖在Matlab中研究這個問題,併發生了一些相當奇怪的事情(見代碼)。第一張和最後一張Clown3D圖像被交換,我不明白爲什麼。

load clown; 
Z=5; 
Clown3D=repmat(X,[1,1,Z]); 
white=max(Clown3D(:)); 
Clown3D(100,:,1)=white; %1 white row 
Clown3D([70,130],:,2)=white; %2 white row 
Clown3D([50 100 150],:,3)=white; %3 white row 
Clown3D([40 80 120 160],:,4)=white; %4 white row 
Clown3D([30 60 90 120 150],:,5)=white; %5 white row 

%Original 3d image 
for ii=1:Z 
    figure, imagesc(Clown3D(:,:,ii)), colormap gray 
end 

%into fourier space 
Kspace=fftshift(fftn(fftshift(Clown3D)));  

%2-step ifft3d 
K_hybrid3D = fftshift(ifft(fftshift(Kspace,3),[],3),3); 

ReconClown3D=zeros(size(Clown3D)); 
for ii=1:Z 
    B=squeeze(K_hybrid3D(:,:,ii)); 
    A = fftshift(ifftn(fftshift(B))); 
    ReconClown3D(:,:,ii)=A; 
end 

for ii=1:Z 
    figure, imagesc(abs(ReconClown3D(:,:,ii))), colormap gray 
end 

%classic ifft3d 
ReconClown3DCalssic=fftshift(ifftn(fftshift(Kspace))); 
for ii=1:Z 
    figure, imagesc(abs(ReconClown3DCalssic(:,:,ii))), colormap gray 
end 
+0

你是什麼意思「交換」? –

+0

I ment「交換」 – Giorgio

回答

1

的交換,是因爲你使用的fftshift和缺乏ifftshift的發生。我建議您閱讀fftshift documentation hereifftshift documentation hereifftshiftfftshift的反向移位操作。如果用fftshift移動向量或矩陣,則應該使用ifftshift來取消它或它的輸出ifft,反之亦然。在this MathWorks thread中有一個很好的討論正確的方法。

每當您撥打ifft時,您應該先使用fftshift來移動參數,然後使用ifftshift來移位輸出以獲得正確的結果。當使用fft時,您應該使用ifftshift來切換輸入,並使用fftshift來切換輸出。

如果輸入是偶數大小,這種區別並不重要,因爲fftshiftifftshift是他們自己的逆。但是,您的輸入的維數爲5,這是導致問題的原因。

由於fft的不同維度的混合,對於實施「兩步法」ifft3d的部分而言,這會有點冒險。它看起來像是在依次執行3D fft,1D ifft和2D ifft。我在代碼中包含了一個「修復」,並且不符合之前的建議。相反,它使用fftshift作爲3D fft的輸入和輸出。

要修復您的代碼,您應該修改前面所述的涉及移位的行。由此產生的代碼如下:

load clown; 
Z=5; 
Clown3D=repmat(X,[1,1,Z]); 
white=max(Clown3D(:)); 
Clown3D(100,:,1)=white; %1 white row 
Clown3D([70,130],:,2)=white; %2 white row 
Clown3D([50 100 150],:,3)=white; %3 white row 
Clown3D([40 80 120 160],:,4)=white; %4 white row 
Clown3D([30 60 90 120 150],:,5)=white; %5 white row 

%Original 3d image 
for ii=1:Z 
    figure, imagesc(Clown3D(:,:,ii)), colormap gray 
end 

%into fourier space 
%this line is kept as is; modifying it causes swapping. 
Kspace=fftshift(fftn(fftshift(Clown3D))); 

%2-step ifft3d 
%Modified to include ifftshift, although it shouldn't matter here 
K_hybrid3D = ifftshift(ifft(fftshift(Kspace,3),[],3),3); 

ReconClown3D=zeros(size(Clown3D)); 
for ii=1:Z 
    B=squeeze(K_hybrid3D(:,:,ii)); 
    %Modified to include ifftshift, although it shouldn't matter here 
    A = ifftshift(ifftn(fftshift(B))); 
    ReconClown3D(:,:,ii)=A; 
end 

for ii=1:Z 
    figure, imagesc(abs(ReconClown3D(:,:,ii))), colormap gray 
end 

%classic ifft3d 
%Modified to include ifftshift, and it DOES matter here. 
ReconClown3DCalssic=ifftshift(ifftn(fftshift(Kspace))); 
for ii=1:Z 
    figure, imagesc(abs(ReconClown3DCalssic(:,:,ii))), colormap gray 
end 
+0

非常感謝。非常清楚。這個關於fftshift和ifftshift的東西太暗了......:D – Giorgio

+0

@ user2835487如果這個答案對你有幫助,考慮接受它:https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer -工作 –