Remidies for changed values in a vector as a result of fftshift?

1 次查看(过去 30 天)
Hi,
While rewriting a script, I noticed that the sum of a vector changes if ifftshift/fftshift has been carried out. Take the following example:
t=-10:.1:10-.1; y=exp(-t.^2/2); s1=sum(y); s2=sum(ifftshift(y)); s3=sum(fftshift(y)); d1=vpa(s1-s2); d2=vpa(s1-s3);
I get the following: d1=d2=0.0000000000000035527136788005009293556213378906
I also found that I get the same difference if I carry out the shift manually like:
y2=[y(101:200),y(1:100)]; s4=sum(y2); s4-s2=0
So my question is, why does the sum of the vector change if the vector is shifted in this manner and are there any ways to make sure that the sum stays the same without having to evaluate both of the sums and then re-scaling the shifted vector?
Thanks in advance, Robert

采纳的回答

Star Strider
Star Strider 2017-2-16
I believe you intend ‘d1-d2’ here:
d1=d2=0.0000000000000035527136788005009293556213378906
In any event, that value, 3.6E-15, is on the order of floating point approximation error. See: Why is 0.3 - 0.2 - 0.1 (or similar) not equal to zero? for a thorough discussion.
  2 个评论
Robert
Robert 2017-2-16
Hi,
I meant that d1 and d2 gives the same result, but thank you for your answer, do you know if there are any ways of adjusting the floating point approximation? Like using more significant digits or something like that?
Star Strider
Star Strider 2017-2-16
My pleasure.
You can use single precision instead of the default double, but I would not recommend that. You’ve already discovered the Symbolic Math Toolbox and its extended-precision capabilities.
Another option is John D’Errico’s HPF - a big decimal class. I’ve no experience with it, since I rarely need precision beyond the MATLAB default. If you want extended-precision computations, it would definitely be worth exploring.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by