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
댓글 수: 0
채택된 답변
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
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 Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!