从相位和幅度获得傅里叶变换 - Matlab [英] Getting Fourier Transform from Phase and Magnitude - Matlab

查看:1617
本文介绍了从相位和幅度获得傅里叶变换 - Matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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)

Imaginary = Real*tan(Phase)

最后组合并反向fft2:

and finally combining and inverse fft2:

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

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

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;

非常感谢:)

推荐答案

你的功能是试图同时测试两件事:(1)FFT和逆FFT图像,(2)将复数分解为实部和虚部,转换为幅度和相位,然后再把它重新组合在一起。你应该分别测试这两个函数中的每一个,而不是一次尝试整个事情并且想知道它为什么不起作用。

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.

测试是否 ifft(fft(图像))返回原始图像,您可以删除或注释掉所有复数操作:

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;

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

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;

你必须要做 imagesc(abs(f))为了显示得到的逆变换图像,摆脱一个(几乎为零)虚部。

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);

希望这会有所帮助。

这篇关于从相位和幅度获得傅里叶变换 - Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆