5
votes

I did fft in matlab and in java using jtransforms library but the results are slightly different.

Matlab results:
-0.0530528652679544
-0.00775535711930750 + 0.0281791646147104i
-0.0304104457750988 - 0.209776156064443i
0.266945753193636 + 0.200338044445226i

Jtransforms results:
-0.05305448436232618
-0.007755593801247046 + 0.028180024600812384
-0.03041137385657606 -0.20978255812004887
0.26695389998013486 + 0.20034415846373468

Are the results different or Matlab is just rounding the values?

2
More than slightly different. Is one library using 32-bit floating point and the other 64-bit?Peter Lawrey

2 Answers

7
votes

There are several different algorithms for doing FFT. In principle they're all equal, but in practice, combined with floating-point arithmetic, the results will be slightly different. Even if the basic FFT algorithm is the same, implementation details such as order of summation can cause differences. Many modern processors do this even if you don't do anything special, depending on the optimization flags.

In your results, the differences are at about 5th significant digit. It's a reasonably small difference. You could try inverse transforming the results (using both Matlab and JTransforms IFTs) to see whether one or the other of the transforms is clearly more accurate.

1
votes

The differences appear to be larger than normal numerical precision issues for double precision floating point arithmetic. It looks more like one of the FFTs is using some short float arithmetic or data instead, and printing the result out as doubles (which makes all those extra digits a lot of nonsense).