MATLAB: Fast fourier transform for even functions

fft

The Fourier transform of a real and even function should be real and even. Why the Fourier transform of the following vector is real but the second one is not?!!!
>> fft([0 1 2 3 4 5 6 7 6 5 4 3 2 1])'
ans =
49.0000
-20.1957
-0.0000
-2.5724
-0.0000
-1.2319
-0.0000
-1.0000
-0.0000
-1.2319
-0.0000
-2.5724
-0.0000
-20.1957
>> imag(ans)
ans =
0
0
0
0
0
0
0
0
0
0
0
0
0
0
The second one is :
>> fft([0 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1])'
ans =
81.0000 + 0.0000i
-33.1634 + 0.0000i
-0.0000 + 0.0000i
-4.0000 - 0.0000i
-0.0000 + 0.0000i
-1.7041 - 0.0000i
0.0000 - 0.0000i
-1.1325 - 0.0000i
0.0000 + 0.0000i
-1.0000 + 0.0000i
0.0000 - 0.0000i
-1.1325 + 0.0000i
0.0000 + 0.0000i
-1.7041 + 0.0000i
-0.0000 - 0.0000i
-4.0000 + 0.0000i
-0.0000 - 0.0000i
-33.1634 - 0.0000i
>> imag(ans)
ans =
1.0e-14 *
0
0.0888
0.1110
-0.0222
0.0056
-0.0056
-0.0222
-0.0111
0.0888
0
-0.0888
0.0111
0.0222
0.0056
-0.0056
0.0222
-0.1110
-0.0888
As seen the imaginary part is not zero anymore although it is in the order of e^-15 I would appreciate any immediate answer. Thanks

Best Answer

The first case was blind luck. Because of finite computer precision, you cannot expect positive and negative frequencies to cancel exactly. However, you can force a symmetric fft as follows
>> x=[0 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1];
>> conj(ifft(x,'symmetric')*numel(x)).'
ans =
81.0000
-33.1634
0
-4.0000
0
-1.7041
0
-1.1325
0
-1.0000
0
-1.1325
0
-1.7041
0
-4.0000
0
-33.1634