Unexpected behavior of numpy.fft.fft with high precision numbers

后端 未结 2 1903
無奈伤痛
無奈伤痛 2020-12-12 04:01

I have the following code...Note the two lines under # generate sine curve. One uses a higher precision value for 2pi than the other, they should still give near identical

相关标签:
2条回答
  • 2020-12-12 04:44

    This is totally expected behavior. Computers use floating-point computations, which are inherently imprecise.

    Note the y-axis for your real result. If no numerical inaccuracy existed, the real component would be identically 0. With your "higher precision" result, the real part is almost identical to 0 (1e-14 is very close to the precision of double-precision floats). With a lower precision, the real part becomes much larger (though still much, much smaller than the imaginary part). Because of the larger numbers, there is more structure as well (i.e. the error is not given by rounding errors, but by an actual feature of your input data, a period that is slightly shorter than ideal).

    0 讨论(0)
  • 2020-12-12 04:45

    As @Cris Luengo said you need to look at the scale of the y-axis to accurately compare two plots. Another way to do this is to plot both of the things you're trying to compare on the same figure, as I've done below.

    The magnitude of the FFT is displayed, using a log scale, and it's quite evident that using fewer significant figures of pi does indeed result in a lower accuracy result. Most of the values aren't exactly zero, as is to be expected when using floating point numbers, but using more significant figures gives many orders of magnitude improvement, which is not immediately apparent when the FFTs are plotted separately.

    code used:

    import numpy as np
    import matplotlib.pyplot as plt
    
    
    t1 = np.arange(0., 1., .01)
    
    values = {
    'low':6.28318,
    'higher':6.283185307179586,
    'highest':2*numpy.pi,
    }
    styles = {
    'low':'-',
    'higher':'-',
    'highest':'.-'
    }
    
    fig, ax_list = plt.subplots(3,1)
    for name, tau in values.items():
        y1 = np.sin(tau*5.*t1)
        ffty = np.fft.fft(y1)
    
        ax_list[0].plot(t1,y1, styles[name], label=name)
        ax_list[1].plot(abs(ffty.real), styles[name],label=name)
        ax_list[2].plot(abs(ffty.imag), styles[name], label=name)
    
    [ax.legend() for ax in ax_list]
    ax_list[0].set_title('time domain')
    ax_list[1].set_title('real part')
    ax_list[2].set_title('imaginary part')
    ax_list[1].set_yscale('log')
    ax_list[2].set_yscale('log')
    plt.draw()
    
    0 讨论(0)
提交回复
热议问题