How to calculate a Fourier series in Numpy?

前端 未结 5 1307
遇见更好的自我
遇见更好的自我 2020-11-30 01:42

I have a periodic function of period T and would like to know how to obtain the list of the Fourier coefficients. I tried using fft module from numpy but it seems more dedic

5条回答
  •  星月不相逢
    2020-11-30 02:32

    In the end, the most simple thing (calculating the coefficient with a riemann sum) was the most portable/efficient/robust way to solve my problem:

    import numpy as np
    def cn(n):
       c = y*np.exp(-1j*2*n*np.pi*time/period)
       return c.sum()/c.size
    
    def f(x, Nh):
       f = np.array([2*cn(i)*np.exp(1j*2*i*np.pi*x/period) for i in range(1,Nh+1)])
       return f.sum()
    
    y2 = np.array([f(t,50).real for t in time])
    
    plot(time, y)
    plot(time, y2)
    

    gives me: alt text

提交回复
热议问题