Discrete fourier transformation from a list of x-y points

后端 未结 3 1632
走了就别回头了
走了就别回头了 2021-01-01 02:46

What I\'m trying to do is, from a list of x-y points that has a periodic pattern, calculate the period. With my limited mathematics knowledge I know that Fourier Transformat

3条回答
  •  余生分开走
    2021-01-01 03:26

    For samples that are not evenly spaced, you can use scipy.signal.lombscargle to compute the Lomb-Scargle periodogram. Here's an example, with a signal whose dominant frequency is 2.5 rad/s.

    from __future__ import division
    
    import numpy as np
    from scipy.signal import lombscargle
    import matplotlib.pyplot as plt
    
    
    np.random.seed(12345)
    
    n = 100
    x = np.sort(10*np.random.rand(n))
    # Dominant periodic signal
    y = np.sin(2.5*x)  
    # Add some smaller periodic components
    y += 0.15*np.cos(0.75*x) + 0.2*np.sin(4*x+.1)
    # Add some noise
    y += 0.2*np.random.randn(x.size)
    
    plt.figure(1)
    plt.plot(x, y, 'b')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.grid()
    
    dxmin = np.diff(x).min()
    duration = x.ptp()
    freqs = np.linspace(1/duration, n/duration, 5*n)
    periodogram = lombscargle(x, y, freqs)
    
    kmax = periodogram.argmax()
    print("%8.3f" % (freqs[kmax],))
    
    plt.figure(2)
    plt.plot(freqs, np.sqrt(4*periodogram/(5*n)))
    plt.xlabel('Frequency (rad/s)')
    plt.grid()
    plt.axvline(freqs[kmax], color='r', alpha=0.25)
    plt.show()
    

    The script prints 2.497 and generates the following plots:

提交回复
热议问题