Clear Filters
Clear Filters

round() function does not work as expected

4 views (last 30 days)
I have the following array as the output of my function: [16.9900000000000 + 0.00000000000000i 9.49188888257731 + 1.38777878078145e-16i 1.55759614170397 - 1.11022302462516e-16i 3.03346810679636 - 2.77555756156289e-16i 8.03389530551741 + 4.16333634234434e-17i], whereas the correct output is [17 9.50000000000000 1.60000000000000 3 8]. I expected by using X = round(real(X), 1); I could get the outputs to match, but instead it gives [16.9000000000000 7.90000000000000 2.90000000000000 5.40000000000000 8.10000000000000]. What causes this? How can I fix this?

Accepted Answer

John D'Errico
John D'Errico on 3 Dec 2023
Edited: John D'Errico on 3 Dec 2023
Sorry, but it is often the case that your vector X is not what you thought it was, or you have written a function named round.
X = [16.9900000000000 + 0.00000000000000i 9.49188888257731 + 1.38777878078145e-16i 1.55759614170397 - 1.11022302462516e-16i 3.03346810679636 - 2.77555756156289e-16i 8.03389530551741 + 4.16333634234434e-17i]
X =
16.9900 + 0.0000i 9.4919 + 0.0000i 1.5576 - 0.0000i 3.0335 - 0.0000i 8.0339 + 0.0000i
Y = round(real(X),1)
Y = 1×5
17.0000 9.5000 1.6000 3.0000 8.0000
If you got something else, then perhaps you have written a function round of your own that you have forgotten about, or the vector X is not what you claim it to be. There are essentially no other options. (Ok, maybe you have downloaded a function by that name, but I would put that under second case, where you have your own round function.) You can check for the existence of a different round function by doing this:
which round -all
If you have your own function named round on top of the search path, then rename it, as round is a terribly useful tool.
  2 Comments
Voss
Voss on 3 Dec 2023
The rounded output is equal to the expected output.
x_in = [5.84000000000000 -0.0700000000000000 -0.730000000000000 1.18000000000000 0.490000000000000 -0.170000000000000 0.550000000000000 0.120000000000000 -0.0900000000000000 0.740000000000000 0.570000000000000 0.420000000000000 -0.520000000000000 0.0700000000000000 0.830000000000000 0.360000000000000 0.420000000000000 0.860000000000000 0.460000000000000 -0.0600000000000000 0.0800000000000000 -0.220000000000000 -0.0600000000000000 0.580000000000000 -0.510000000000000 0.110000000000000 0.460000000000000 -0.510000000000000 0.140000000000000 -1.10000000000000 0.800000000000000 0.440000000000000 -0.130000000000000 0.440000000000000 0.800000000000000 -1.10000000000000 0.140000000000000 -0.510000000000000 0.460000000000000 0.110000000000000 -0.510000000000000 0.580000000000000 -0.0600000000000000 -0.220000000000000 0.0800000000000000 -0.0600000000000000 0.460000000000000 0.860000000000000 0.420000000000000 0.360000000000000 0.830000000000000 0.0700000000000000 -0.520000000000000 0.420000000000000 0.570000000000000 0.740000000000000 -0.0900000000000000 0.120000000000000 0.550000000000000 -0.170000000000000 0.490000000000000 1.18000000000000 -0.730000000000000 -0.0700000000000000];
X_out_expected = [17 9.50000000000000 1.60000000000000 3 8 2.50000000000000 3 3.50000000000000 10 0 2 2 7 7 8.50000000000000 8 0 9.50000000000000 8 15 9 8 8.50000000000000 13.5000000000000 0 8.50000000000000 0 3.50000000000000 7 2 7.50000000000000 0 6 0 7.50000000000000 2 7 3.50000000000000 0 8.50000000000000 0 13.5000000000000 8.50000000000000 8 9 15 8 9.50000000000000 0 8 8.50000000000000 7 7 2 2 0 10 3.50000000000000 3 2.50000000000000 8 3 1.60000000000000 9.50000000000000];
X_out = FFT(x_in);
X_out_rounded = round(X_out,1);
isequal(X_out_rounded, X_out_expected)
ans = logical
1
function X = FFT(x)
N = length(x);
if N <= 1
X = x;
return;
end
X_even = FFT(x(1:2:end));
X_odd = FFT(x(2:2:end));
factor = exp(-2j * pi * (0:N/2-1) / N);
X = [X_even + factor .* X_odd , X_even - factor .* X_odd];
end
John D'Errico
John D'Errico on 3 Dec 2023
Edited: John D'Errico on 3 Dec 2023
Surely you could have tested this yourself right here?
x = [5.84000000000000 -0.0700000000000000 -0.730000000000000 1.18000000000000 0.490000000000000 -0.170000000000000 0.550000000000000 0.120000000000000 -0.0900000000000000 0.740000000000000 0.570000000000000 0.420000000000000 -0.520000000000000 0.0700000000000000 0.830000000000000 0.360000000000000 0.420000000000000 0.860000000000000 0.460000000000000 -0.0600000000000000 0.0800000000000000 -0.220000000000000 -0.0600000000000000 0.580000000000000 -0.510000000000000 0.110000000000000 0.460000000000000 -0.510000000000000 0.140000000000000 -1.10000000000000 0.800000000000000 0.440000000000000 -0.130000000000000 0.440000000000000 0.800000000000000 -1.10000000000000 0.140000000000000 -0.510000000000000 0.460000000000000 0.110000000000000 -0.510000000000000 0.580000000000000 -0.0600000000000000 -0.220000000000000 0.0800000000000000 -0.0600000000000000 0.460000000000000 0.860000000000000 0.420000000000000 0.360000000000000 0.830000000000000 0.0700000000000000 -0.520000000000000 0.420000000000000 0.570000000000000 0.740000000000000 -0.0900000000000000 0.120000000000000 0.550000000000000 -0.170000000000000 0.490000000000000 1.18000000000000 -0.730000000000000 -0.0700000000000000];
X = FFT(x)
X =
Columns 1 through 10 16.9900 + 0.0000i 9.4919 + 0.0000i 1.5576 - 0.0000i 3.0335 - 0.0000i 8.0339 + 0.0000i 2.4931 - 0.0000i 2.9957 - 0.0000i 3.5097 - 0.0000i 9.9622 + 0.0000i -0.0012 - 0.0000i Columns 11 through 20 1.9767 - 0.0000i 2.0239 - 0.0000i 6.9537 - 0.0000i 6.9714 + 0.0000i 8.5201 + 0.0000i 8.0053 + 0.0000i -0.0300 + 0.0000i 9.5119 + 0.0000i 7.9895 + 0.0000i 14.9864 + 0.0000i Columns 21 through 30 8.9989 + 0.0000i 8.0060 - 0.0000i 8.4522 + 0.0000i 13.5044 + 0.0000i -0.0222 - 0.0000i 8.4930 + 0.0000i 0.0027 - 0.0000i 3.4749 - 0.0000i 7.0136 - 0.0000i 2.0190 + 0.0000i Columns 31 through 40 7.4657 + 0.0000i -0.0033 - 0.0000i 5.9900 + 0.0000i -0.0033 - 0.0000i 7.4657 + 0.0000i 2.0190 - 0.0000i 7.0136 - 0.0000i 3.4749 + 0.0000i 0.0027 - 0.0000i 8.4930 + 0.0000i Columns 41 through 50 -0.0222 - 0.0000i 13.5044 + 0.0000i 8.4522 + 0.0000i 8.0060 + 0.0000i 8.9989 + 0.0000i 14.9864 - 0.0000i 7.9895 + 0.0000i 9.5119 + 0.0000i -0.0300 + 0.0000i 8.0053 + 0.0000i Columns 51 through 60 8.5201 + 0.0000i 6.9714 - 0.0000i 6.9537 - 0.0000i 2.0239 - 0.0000i 1.9767 - 0.0000i -0.0012 - 0.0000i 9.9622 + 0.0000i 3.5097 - 0.0000i 2.9957 - 0.0000i 2.4931 - 0.0000i Columns 61 through 64 8.0339 + 0.0000i 3.0335 + 0.0000i 1.5576 - 0.0000i 9.4919 + 0.0000i
Xr = round(real(X),1)
Xr = 1×64
17.0000 9.5000 1.6000 3.0000 8.0000 2.5000 3.0000 3.5000 10.0000 0 2.0000 2.0000 7.0000 7.0000 8.5000 8.0000 0 9.5000 8.0000 15.0000 9.0000 8.0000 8.5000 13.5000 0 8.5000 0 3.5000 7.0000 2.0000
function X = FFT(x)
N = length(x);
if N <= 1
X = x;
return;
end
X_even = FFT(x(1:2:end));
X_odd = FFT(x(2:2:end));
factor = exp(-2j * pi * (0:N/2-1) / N);
X = [X_even + factor .* X_odd , X_even - factor .* X_odd];
end
If you have gotten something different using that exact code, then you are not using the same input vector that you think you are using, or your code is not what you claim it to be.
I have even seen people swear literally on a stack of bibles (and this was just recently) that they did not have a function by some specific name, when in fact they had written one, squirreled away on their search path. So I would check again. As well, I would verify that the function FFT is exactly as you have shown here. Be very careful, as some people save their own functions in the MATLAB supplied search path itself. (A terribly bad idea.) Changes to those functions are not seen until you force MATLAB to rehash the file cache. That does not happen usually until you restart MATLAB.

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!