我正在Matlab中实现用于阶段检索的Gerchberg-Saxton算法,作为报告的一部分。当我没有获得Matlab许可的PC时,我想我可以继续使用Scilab。然而,结果明显不同。
Matlab代码:
function [ realphase ] = GerchSax( source, target, iterations )
[n, m] = size(target);
fourierspace = fft2(source);
for iter=1:iterations
fourierphase = angle(fourierspace);
fourierspace = sqrt(abs(target)) .* exp(1i * fourierphase);
realspace = ifft2(fourierspace);
realphase = angle(realspace);
realspace = sqrt(abs(source)) .* exp(1i * realphase);
fourierspace = fft2(realspace);
end
Scilab代码:
function [ res ] = angle(A)
res = atan(imag(A), real(A))
endfunction
function [ realphase ] = GerchSax( source, target, iterations )
fourierspace = fft(source);
for iter=1:iterations
fourierphase = angle(fourierspace);
fourierspace = sqrt(abs(target)) .* exp(%i * fourierphase);
realspace = ifft(fourierspace);
realphase = angle(realspace);
realspace = sqrt(abs(source)) .* exp(%i * realphase);
fourierspace = fft(realspace);
end
“角度”函数的实现直接来自于Scilab文档。据我所知,在matlab中,如果给出一个矩阵,fft
将独立地对每一列向量执行傅里叶变换(因此使用fft2
进行实际的二维傅里叶变换),而scilab已经用fft
进行了“实”2D变换。
我在两个程序中都使用了source = [1 1 1; 1 2 1; 1 1 1]
、target = [0 1 1; 0 1 1; 0 0 0]
和1000个迭代。Matlab返回以下相位分布:
-3.0589 -1.0472 0.9645
3.1416 0 3.1416
3.0589 1.0472 -0.9645
当我从Scilab获得这个阶段分布时:
2.0943951 - 1.0471976 2.0943951
3.1415927 - 1.977D-16 3.1415927
- 2.0943951 1.0471976 - 2.0943951
我是否犯了错误,将Matlab代码转换为其正确的Scilab等价物?在这些环境中,FFT的实现是否不同?
发布于 2015-10-27 06:29:53
fft在scilab和Matlab之间是不同的,下面的链接将向您展示如何在matlab或Scilab中实现fft的等效结果。
https://stackoverflow.com/questions/33369638
复制相似问题