0
$\begingroup$

I'm quite puzzled by Mathematica lack of precision with Fourier and InverseFourier. I tried the following code

x = Table[Sin[n], {n, 20}];
X = Fourier[x];
x1 = InverseFourier[X]
x - Abs[x1]

And this is the output

{0.841471 - 7.88128*10^-19 I, 0.909297 - 1.9237*10^-17 I, 
 0.14112 - 6.60657*10^-18 I, -0.756802 + 
  3.35168*10^-17 I, -0.958924 + 3.82771*10^-17 I, -0.279415 + 
  1.49703*10^-16 I, 0.656987 + 4.01726*10^-17 I, 
 0.989358 + 1.91002*10^-17 I, 
 0.412118 + 4.61974*10^-17 I, -0.544021 - 
  2.58484*10^-17 I, -0.99999 + 6.82983*10^-18 I, -0.536573 - 
  1.51056*10^-16 I, 0.420167 - 2.67625*10^-17 I, 
 0.990607 - 5.50323*10^-17 I, 
 0.650288 + 2.48665*10^-17 I, -0.287903 + 
  3.78263*10^-17 I, -0.961397 - 7.97744*10^-17 I, -0.750987 + 
  1.02483*10^-16 I, 0.149877 - 3.62319*10^-17 I, 
 0.912945 - 9.76356*10^-17 I}

{-1.11022*10^-16, -1.11022*10^-16, 0., -1.5136, -1.91785, -0.558831, \
-1.11022*10^-16, 
 2.22045*10^-16, 0., -1.08804, -1.99998, -1.07315, 0., \
-2.22045*10^-16, -1.11022*10^-16, -0.575807, -1.92279, -1.50197, 
 3.05311*10^-16, 1.11022*10^-16}

Am I doing something wrong?

$\endgroup$
4
  • $\begingroup$ Try x - Chop[x1] to remove the small imaginary components rather than taking the magnitude, discarding the sign of x1! $\endgroup$ Commented Dec 12, 2014 at 17:18
  • $\begingroup$ Chop[x1 - Abs[x1]] gives {0, 0, 0, -1.5136, -1.91785, -0.558831, 0, 0, 0, -1.08804, -1.99998, -1.07315, 0, 0, 0, -0.575807, -1.92279, -1.50197, 0, 0} $\endgroup$
    – Nasser
    Commented Dec 12, 2014 at 17:35
  • $\begingroup$ Aww, Chop is the secret! Thanks so much! And yes, my bad for using Abs. For some reason I thought I was dealing with non-negative signals... $\endgroup$
    – Wolfy
    Commented Dec 12, 2014 at 17:55
  • $\begingroup$ They default to machine precision when input has infinite precision. $\endgroup$ Commented Dec 12, 2014 at 19:18

1 Answer 1

0
$\begingroup$

Chop[] is the key here (thanks to MikeLimaOscar and Nasser). And using Abs was obviously wrong, since I'm dealing with a function with positive and negative values. Here's the correct code:

x = Table[Sin[n], {n, 16}];
X = Chop[Fourier[x]];
x1 = Chop[InverseFourier[X]]
x - x1
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.