Python Forum

Full Version: Plotting periodic function with fft
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.