Complex number and fft

67 ビュー (過去 30 日間)
Nmak
Nmak 2020 年 7 月 24 日
コメント済み: David Goodmanson 2020 年 7 月 25 日
So I noticed during processing some images in matlab, that the angle phase images after fft+ifft are not the same as the original anymore. When looking at the matrix, I saw that Matlab stores values sometimes as "0", sometimes as "0.000000000000000 + 0.000000000000000i". I was wondering, what is the difference? Isn´t the whole matrix supposed to contain complex numbers?
I also tried a simpler example myself
V = [0, 2];
W = [0, 2+3i, 5, 0, 3+1i];
fft_V = fft(V);
fft_W = fft(W);
vv = ifft(fft_V);
fs_V = fftshift(fft(V));
vvv = ifft(ifftshift(fs_V));
ww = ifft(fft_W);
fs_W = fftshift(fft_W);
www = ifft(ifftshift(fs_W));
w2 = ifft(fft(ww));
While for V everything is normal, 2 things about W make me wonder:
"www" is the same as W, but why are the signs in front of the "0"s inverted? (from + to -)
If one checks "w2 == ww", Matlab returns: "[0 1 1 1 1]", which seems weird. All 5 values are the same, but somehow, the first entry of the vector returns false?

採用された回答

David Goodmanson
David Goodmanson 2020 年 7 月 25 日
編集済み: David Goodmanson 2020 年 7 月 25 日
Hi Nmak,
you are just getting into standard numerical precision issues. The fft and ifft involove complex variable calculations. Getting things to agree in double precision after a bunch of such calculations doesn't always work exactly. For example
ww(2:end)-w2(2:end)
ans =
0 0 0 0
These elements are truly equal.
w(1)-w2(1)
ans =
0 + 8.8818e-17i
not quite equal. Hence the results of the ww==w2 test.
Sometimes what you see is the result of formatting,
>> W(1)
ans =
0
>> format long
>> W
W =
Columns 1 through 2
0.000000000000000 + 0.000000000000000i 2.000000000000000 + 3.000000000000000i
Columns 3 through 4
5.000000000000000 + 0.000000000000000i 0.000000000000000 + 0.000000000000000i
Column 5
3.000000000000000 + 1.000000000000000i
W(1) is still 0 of course, but since the some of the other elements of W are complex, W(1) is listed in a 0 + 0i format.
  2 件のコメント
Nmak
Nmak 2020 年 7 月 25 日
Thank you for your response. Is there an easy workaround to avoid these issues? And do you know, why Matlab sometimes stores values as "0", sometimes as "0.000000000000000 + 0.000000000000000i", despite being in the same Matrix?
David Goodmanson
David Goodmanson 2020 年 7 月 25 日
When you have a complex array, then two memory location are allocated for each element, whether that element has a a nonzero imaginary part or not. So:
A = 0:4
W = [0, 2+3i, 5, 0, 3+1i];
whos A W
Name Size Bytes Class Attributes
A 1x5 40 double
W 1x5 80 double complex
At eight bytes per real number for double precision, you can see two real numbers allocated for each element of W. Looking just at a single real element of W,
W(1)
ans = 0
there is no need to report out a zero imaginary part.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by