从相位和幅度获得傅里叶变换 - Matlab

发布于 2024-11-15 19:00:40 字数 934 浏览 3 评论 0原文

傅里叶变换 F 的幅度和相位定义为:

Mag = sqrt(Real(F)^2 + Imaginary(F)^2)

Phase = arctan(Imaginary(F)/Real(F))

尝试编写 matlab 代码,该代码接受灰度图像矩阵,对矩阵执行 fft2(),然后根据变换计算幅度和相位。然后我希望计算傅里叶变换的虚部和实部。这是通过将前两个方程重新排列为:

Real = Mag/sqrt(1 + tan(Phase)^2)

Imaginary = Real*tan(Phase)

最后组合和逆 fft2 来完成的:

F = Real + i*Imaginary
image = ifft2(F)

我希望看到与输入相同的图像,但我得到了垃圾。我的数学错了吗?我的matlab mfile代码如下:

function y = forwardBackwardFFT(image)

F = fft2(image);
mag = sqrt(real(F).^2 + imag(F).^2);
phase = atan(imag(F)./real(F));

re = sqrt((mag.^2)./(1 + tan(phase).^2));
im = re.*tan(phase);
F = re + i*im;
f = ifft2(F);

subplot(1,2,1);
imshow(image);
Title('Original Image');

subplot(1,2,2);
imshow(f);
Title('Image after forward and backward FFT');
y = f;

非常感谢:)

The magnitude and phase of a fourier transform F are defined as:

Mag = sqrt(Real(F)^2 + Imaginary(F)^2)

and

Phase = arctan(Imaginary(F)/Real(F))

Ive tried to write matlab code that takes in a grayscale image matrix, performs fft2() on the matrix and then calculates the magnitude and phase from the transform. I then wish to calculate the imaginary and real parts of the fourier transform. This is done by rearranging the first two equations into:

Real = Mag/sqrt(1 + tan(Phase)^2)

and

Imaginary = Real*tan(Phase)

and finally combining and inverse fft2:

F = Real + i*Imaginary
image = ifft2(F)

I'd expect to see the same image as the input, but i get garbage. Is my maths wrong? My matlab mfile code is as follows:

function y = forwardBackwardFFT(image)

F = fft2(image);
mag = sqrt(real(F).^2 + imag(F).^2);
phase = atan(imag(F)./real(F));

re = sqrt((mag.^2)./(1 + tan(phase).^2));
im = re.*tan(phase);
F = re + i*im;
f = ifft2(F);

subplot(1,2,1);
imshow(image);
Title('Original Image');

subplot(1,2,2);
imshow(f);
Title('Image after forward and backward FFT');
y = f;

thanks a lot :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薔薇婲 2024-11-22 19:00:40

您的函数试图同时测试两件事:(1) 对图像进行 FFT 和逆 FFT,以及 (2) 将复数分解为实部和虚部,转换为幅度和相位,然后再次将其组合在一起。您应该分别测试这两个功能,而不是立即尝试整个功能并想知道为什么它不起作用。

要测试 ifft(fft(image)) 是否返回原始图像,您只需删除或注释掉所有复杂的数字操作:

function y = forwardBackwardFFT(image)

F = fft2(image);
%# stuff removed
f = ifft2(F);

subplot(1,2,1);
imshow(image);
title('Original Image');

subplot(1,2,2);
imshow(f, []);
title('Image after forward and backward FFT');
y = f;

有效。所以问题出在你的复杂数字操作上。考虑当phase=0 或phase=pi/2 时会发生什么。 0 的正切为 0,导致被零除; tan(pi/2) 是无穷大。

以下是一些有效的代码:

mag =  sqrt(real(F).^2 + imag(F).^2);
phase = atan2(imag(F),real(F));

re = mag .* cos(phase);
im = mag .* sin(phase);
F = re + 1i*im;

您必须执行 imagesc(abs(f)) 才能显示生成的逆变换图像,以消除(几乎为零)虚部。

获取复数的幅度和相位的更惯用的方法是简单地执行以下操作:

mag = abs(F);
phase = angle(F);

希望这会有所帮助。

Your function is trying to test two things at once: (1) FFT and inverse FFT an image, and (2) disassemble a complex number into real and imaginary parts, transform to amplitude and phase, and then put it back together again. Instead of trying the whole thing at once and wondering why it doesn't work, you should test each of these two functions separately.

To test whether ifft(fft(image)) gives back the original image, you can just remove or comment out all the complex number manipulations:

function y = forwardBackwardFFT(image)

F = fft2(image);
%# stuff removed
f = ifft2(F);

subplot(1,2,1);
imshow(image);
title('Original Image');

subplot(1,2,2);
imshow(f, []);
title('Image after forward and backward FFT');
y = f;

This works. So the problem is with your complex number manipulations. Consider what happens when phase=0 or phase=pi/2. The tangent of 0 is 0, leading to a division by zero; and tan(pi/2) is infinite.

Here is some code that works:

mag =  sqrt(real(F).^2 + imag(F).^2);
phase = atan2(imag(F),real(F));

re = mag .* cos(phase);
im = mag .* sin(phase);
F = re + 1i*im;

You will have to do imagesc(abs(f)) in order to show the resulting inverse-transformed image, to get rid of a (nearly zero) imaginary component.

A more idiomatic way to get the magnitude and phase of a complex number is to simply do:

mag = abs(F);
phase = angle(F);

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文