Python Forum
Plotting periodic function with fft
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plotting periodic function with fft
#1
I want to plot a T-periodic function using a restricted set of Fourier coefficients. The function equals sin(x/tau) for 0<=x<(2*pi*tau) and 0 for (2*pi*tau)<=x<T (it is periodically repeated outside [0,T)). My attempt so far is that I have defined the function as follows:

def p(t,T,tau):
    n=np.floor(t/T)
    t=t-n*T
    if t<(2*np.pi*tau):
        p=np.sin(t/tau)
    else:
        p=0
    return p

tdata=np.linspace(-3*np.pi,3*np.pi,500)
pdata=[]

for i in tdata:
  pdata.append(p(i,3*np.pi,1))

plt.plot(tdata,np.array(pdata),label='$T=3\pi$, $\u03C4=1$')
plt.legend(loc='upper left')
The plotting is just to check that the function as defined is correct and it is (the specified values of the parameters make it easy to double check with a drawing by hand). Anyway, I would now like to plot the function I have plotted using a restricted set of Fourier coefficients. To obtain the coefficients, I would define coefficients=np.fft.fft(np.array(pdata)), but I am unsure how to use these further and plot the function. Appreciate any help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  "Plotting and Computation of Logistic Function by Using Taylor Series with Recursive canatilaa 1 1,830 May-13-2020, 04:01 PM
Last Post: jefsummers
  Periodic Fractions with Fraction or float_to_ratio? TimeMen 3 2,988 May-20-2018, 03:48 PM
Last Post: ljmetzger

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020