Error in complex multiplication
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Prashant Sharma
am 20 Mär. 2017
Kommentiert: Prashant Sharma
am 23 Mär. 2017
I am trying to load information on just one subcarrier while doing an IFFT. Then, I am trying to bring the subcarrier to DC by multiplying it with a complex exponential of corresponding frequency. In doing so I am able to extract the DC from the real component, but my imaginary component is getting garbage values and thus gives a randomly increasing phase.Is it a MATLAB computational error? Is there any way to fix this?
clear all
ifft_size = 128;
ifft_in = zeros(ifft_size,1);
% Load subcarrier number 16 with data
ifft_in(17,1) = 1;
% Compute IFFT
ifft_out = ifft(ifft_in,ifft_size);
x = ifft_out(:);
% Bring the signal to DC
num_samp = numel(x);
y = x.*exp(-1i*2*pi*16*(0:num_samp-1)/ifft_size).';
figure;plot(real(y));
figure;plot(imag(y));
xlabel('Subcarrier index');ylabel('Imag(y)');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/161918/image.jpeg)
0 Kommentare
Akzeptierte Antwort
Alain Kuchta
am 23 Mär. 2017
I understand that you are seeing unexpected results when multiplying two arrays of complex doubles.
The garbage data that you are seeing is the result of the error inherent in floating point arithmetic on a modern computer. Rounding error accumulates over a series of floating point operations. In this case, operations which analytically should result in a value of 0 result instead in a very small number. You can see this illustrated by performing the multiplication of two of the complex numbers in question manually.
As you pointed out, the numbers later in y are farther from the expected value. So we will work out the multiplication of the 126th element.
x = ifft_out(:);
yexp = exp(-1i*2*pi*16*(0:num_samp-1)/ifft_size).';
a = real(x(126));
b = imag(x(126));
c = real(yexp(126));
d = imag(yexp(126));
ad = a * d;
bc = b * c;
By increasing the number of decimal places printed, we see that bc and ad are not exactly equal, even though analytically we would expect them to be.
>> imag_part_126 = ad + bc
imag_part_126 =
7.762887554996212e-17
>> fprintf('%+12.20f\n', bc)
+0.00390625000000003900
>> fprintf('%+12.20f\n', ad)
-0.00390624999999996140
If some further calculation requires that the imaginary part of y be exactly 0, you may consider instead comparing the result to 0 within a tolerance as described by this MATLAB Answers question:
This documentation further explains the issues inherent in floating point operations:
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Bartlett finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!