discrete fourier series coefficients in python

To obtain the Fourier coefficients of a discrete signal in Python, one can use the numpy.fft.fft function. Here's a simple example:

main.py
import numpy as np

# Define a discrete signal with 100 samples
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

# Compute the Fourier coefficients
coeffs = np.fft.fft(y)

# Plot the magnitude of the Fourier coefficients
import matplotlib.pyplot as plt
plt.plot(np.abs(coeffs))
plt.show()
288 chars
14 lines

In this example, we first define a discrete signal y that consists of 100 samples of a sine wave. We then compute the Fourier coefficients of this signal using the numpy.fft.fft function. Finally, we plot the magnitude of the Fourier coefficients using matplotlib.pyplot.plot.

Note that the output of numpy.fft.fft is an array of complex numbers. The magnitude of each complex number corresponds to the amplitude of the corresponding Fourier coefficient, while the argument (angle) corresponds to the phase shift.

gistlibby LogSnag