Discrete fourier transformation from a list of x-y points

后端 未结 3 1622
走了就别回头了
走了就别回头了 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:

    0 讨论(0)
  • 2021-01-01 03:28

    As starting point:

    • (I assume all coordinates are positive and integer, otherwise map them to reasonable range like 0..4095)
    • find max coordinates xMax, yMax in list
    • make 2D array with dimensions yMax, xMax
    • fill it with zeros
    • walk through you list, set array elements, corresponding to coordinates, to 1
    • make 2D Fourier transform
    • look for peculiarities (peaks) in FT result
    0 讨论(0)
  • 2021-01-01 03:47

    This page from Scipy shows you basic knowledge of how Discrete Fourier Transform works: http://docs.scipy.org/doc/numpy-1.10.0/reference/routines.fft.html

    They also provide API for using DFT. For your case, you should look at how to use fft2.

    0 讨论(0)
提交回复
热议问题