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

后端 未结 2 1902
無奈伤痛
無奈伤痛 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: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()
    

提交回复
热议问题