How to Compute the Fourier Series in Python
Computing the Fourier Series in your Python program lets you break apart a signal into its frequencies. Scientific fields such as optics and wave motion utilize the Fourier transform process when making mathematical calculations. NumPy, an open source Python extension, provides the arrays and high-level mathematical functions necessary to compute Fourier Series in the Python programming language. Call the NumPy "fft" function to create a basic Fourier transform.
Instructions
-
-
1
Download the NumPy binary package and install it.
-
2
Open your source file in an editor, such as Windows Notepad.
-
-
3
Import the "matplotlib.pyplot" to enable access to the plotting functions by adding the following code at the top of your file:
import matplotlib.pyplot as pyplt
-
4
Create an array of numbers to use in the Fourier transform calculation by adding the following code:
arr = np.arange(128)
This will create the array [0, 1, 2, .. , 127].
-
5
Compute a one-dimensional discrete Fourier transform with the "fft" function by adding the following code at the top of your file:
ndft = np.fft.fft(np.sin(arr))
ffreq = np.fft.fftfreq(arr.shape[-1])The "fftfreq" function returns a float array containing the sample frequencies for the Discrete Fourier Transform.
-
6
Plot and graph the Fourier Series by adding the following commands:
pyplt.plot(freq, ndft.real, ffreq, ndft.imag)
pyplt.show() -
7
Save the file with a ".py" extension.
-
8
Compile and run the program to compute the Fourier Series.
-
1