# How can I improve the accuracy of my fft processing?

I completed an on-line Digital Signal Processing Course without doing the optional programming assignments which I am attempting now after the course is over. Several test inputs are provided as well as the expected outputs. My problem is that for all test cases my output figures match the expected values exactly up to several decimal places. After that they don't match! For the first value for example:

``````9.065305906994007046e+01
I get:
9.065305906729875574e+01
``````

And for the last (257th):

``````1.717718363464842923e+01
I get:
1.717715652594918652e+01
``````

The correspondence at the beginning of the list is 9 decimal places. By the end of the list it is down to 5 places. I have tried different versions of fft implementations without improvement. It looks so close that there must be a cumulative error trick I am missing? This is the function I used (python, numpy, scipy).:

``````    def scaled_fft_db(x):
# a) a 512-point Hann window - weigh the input data.
N = x.size
window = numpy.hanning(N)
xx = x*window
# b) Compute the DFT of the weighed input - normalized
A = numpy.fft(xx)/N
# c) Return the first 257 values of the normalized spectrum
XX = abs(A[:257])
# take the magnitude in dBs and
# normalize so that the maximum value is 96dB.
X = 20*numpy.log10(XX)
X = X + (96 - max(X))
return X
``````

• fft is pretty accurate. Given white noise, `x = np.random.rand(512)`, then `np.linalg.norm(np.fft.ifft(np.fft.fft(x)) - x) == 7.1510584008774975e-15`. Gaussian white noise `x = np.random.randn(512)` is slightly worse with `norm == 2.3548886213717641e-14`. May 17, 2016 at 5:29