Plot periodic function with Fourier coefficients

Click For Summary
SUMMARY

This discussion focuses on plotting a periodic function using Fourier coefficients in Python. The user successfully plotted a function with a period of T=15 and tau=T/30 using NumPy and Matplotlib. To approximate this function using Fourier coefficients, the user is advised to perform a Fast Fourier Transform (FFT) to identify the amplitude and phase of the harmonics present in the signal. Key considerations include ensuring the data record contains an integer number of full cycles and using a sample size that is a power of two, such as 64, 256, or 1024 samples.

PREREQUISITES
  • Proficiency in Python programming
  • Familiarity with NumPy for numerical operations
  • Experience with Matplotlib for data visualization
  • Understanding of Fast Fourier Transform (FFT) concepts
NEXT STEPS
  • Learn how to implement Fast Fourier Transform (FFT) in Python using NumPy
  • Explore techniques for calibrating an analyzer with known sine and cosine waves
  • Research methods for generating periodic functions with specific sampling rates
  • Study the effects of sampling rates on FFT results and signal analysis
USEFUL FOR

Data scientists, signal processing engineers, and Python developers interested in Fourier analysis and periodic function approximation.

schniefen
Messages
177
Reaction score
4
Homework Statement
Consider the function ##p(t)=\sin{(t/\tau)}## for ##0\leq t <2\pi \tau## and ##p(t)=0## for ##2\pi \tau \leq t < T##, which is periodically repeated outside the interval ##[0,T)## with period ##T##. Plot this function for ##\tau=T/30## using a restricted set of Fourier coefficients.
Relevant Equations
The complex Fourier series: ##\sum_{j=-\infty}^{\infty} a_j e^{i2\pi jt/T}##. Since the ##p(t)## is real-valued, we have ##a_{-j}=\overline{a_j}##.
I have plotted the function for ##T=15## and ##\tau=T/30## below with the following code in Python:

Code:
import numpy as np
import matplotlib.pyplot as plt

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(-5*np.pi,5*np.pi,500)
pdata=[]
for i in tdata:
  pdata.append(p(i,15,1/2))

plt.plot(tdata,np.array(pdata),label='$T=15$, $\u03C4=1/2$')
plt.legend(loc='lower right')
tick_pos= [-5*np.pi,-4*np.pi,-3*np.pi, -2*np.pi , -np.pi, 0, np.pi , 2*np.pi,3*np.pi,4*np.pi,5*np.pi]
labels = ['$-5\pi$','$-4\pi$','$-3\pi$','$-2\pi$','$-\pi$','0', '$\pi$', '$2\pi$','$3\pi$','$4\pi$','$5\pi$']
plt.xticks(tick_pos, labels)
plt.xlabel('$t$')
plt.ylabel('$p(t)$');

Output:
1668101019939.png

I would now like to plot an approximation to this function using a restricted set of Fourier coefficients. How can I do that in python?
 
Physics news on Phys.org
Perform an FFT to identify the amplitude and phase of the harmonics present in the signal.
 
  • Like
Likes   Reactions: schniefen
Baluncore said:
Perform an FFT to identify the amplitude and phase of the harmonics present in the signal.
Yes, I was thinking along those lines, that is, to find the coefficients through FFT. I am very new to this, so I have had little experience with FFT.
 
Take a data record that includes an integer number of full cycles, or exactly one full cycle of the waveform.
Avoid a step where the ends of the waveform wrap around.
An FFT works best on 2^n samples, so generate the function with a sampling rate that results in maybe 64, 256 or 1024 samples.
Calibrate your analyser with some known sine and cosine waves.
 
  • Like
Likes   Reactions: schniefen

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 0 ·
Replies
0
Views
2K
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K