Some examples of how to calculate and plot the Fourier transform using python and scipy fft
Table of contents
Cosinus function
import numpy as npimport matplotlib.pyplot as pltimport scipy.fftpack#----------------------------------------------------------------------------------------#N = 50000 # Number of samplepointsT = 1.0 / 1000.0 # sample spacingx = np.linspace(0.0, N*T, N)y = np.cos(2.0*np.pi*x)plt.plot(x,y)plt.xlim(0,3.0*np.pi)plt.title(r'$cos(2\pi \nu x)$ with $\nu=1$')plt.savefig('fourrier_transform_cosinus.png', bbox_inches='tight')plt.close()yf = scipy.fftpack.fft(y)yf = np.fft.fftshift(yf)xf = np.linspace(-1.0/(2.0*T), 1.0/(2.0*T), N)fig, ax = plt.subplots()ax.plot(xf, 1.0/N *np.abs(yf) )plt.xlim(-4,4)plt.title('FFT (cosinus function) power spectrum')plt.grid()plt.savefig('fourrier_transform_cosinus_abs.png', bbox_inches='tight')plt.close()
Sinus function
N = 50000 # Number of samplepointsT = 1.0 / 1000.0 # sample spacingx = np.linspace(0.0, N*T, N)y = np.sin(2.0*np.pi*x)plt.plot(x,y)plt.xlim(0,3.0*np.pi)plt.title(r'$sin(2\pi \nu x)$ with $\nu=1$')plt.savefig('fourrier_transform_sinus.png', bbox_inches='tight')plt.close()yf = scipy.fftpack.fft(y)yf = np.fft.fftshift(yf)xf = np.linspace(-1.0/(2.0*T), 1.0/(2.0*T), N)fig, ax = plt.subplots()ax.plot(xf, 1.0/N * np.abs(yf) )plt.xlim(-4,4)plt.title('FFT (sinus function) power spectrum')plt.grid()plt.savefig('fourrier_transform_sinus_abs.png', bbox_inches='tight')plt.close()
Rectangular function
N = 50000 # Number of samplepointsT = 1.0 / 1000.0 # sample spacingx = np.linspace(0.0, N*T, N)y = np.zeros(x.shape)for i in range(x.shape[0]):if x[i] > -0.5 and x[i] < 0.5:y[i] = 1.0plt.plot(x,y)plt.xlim(-2,2)plt.title(r'Rectangular function')plt.savefig('fourrier_transform_rectangular.png', bbox_inches='tight')plt.close()yf = scipy.fftpack.fft(y)yf = np.fft.fftshift(yf)xf = np.linspace(-1.0/(2.0*T), 1.0/(2.0*T), N)fig, ax = plt.subplots()ax.plot(xf, np.abs(yf) )plt.xlim(-10,10)plt.title('FFT (rectangular function) power spectrum')plt.grid()plt.savefig('fourrier_transform_rectangular_abs.png', bbox_inches='tight')plt.close()fig, ax = plt.subplots()ax.plot(xf, np.real(yf) )plt.xlim(-10,10)plt.title('FFT (rectangular function) real')plt.grid()plt.savefig('fourrier_transform_rectangular_real.png', bbox_inches='tight')plt.close()fig, ax = plt.subplots()ax.plot(xf, np.imag(yf) )plt.xlim(-10,10)plt.title('FFT (rectangular function) img')plt.grid()plt.savefig('fourrier_transform_rectangular_imag.png', bbox_inches='tight')plt.close()
References
| Links | Site |
|---|---|
| Transformation de Fourier | wikipedia |
| Plotting a Fast Fourier Transform in Python | stackoverflow |
| Transformation de Fourier | math.u-bordeaux.fr |
| Traitement du Signal | irisa.fr |
| Transformation de Fourier des fonctions usuelles | perso.univ-lemans.fr |
| scipy.fftpack.fft | docs.scipy |
| scipy.fftpack.fftshift | docs.scipy |
| Fourier Transforms (scipy.fftpack) | docs.scipy |
| Transform´ee de Fourier | umoncton.ca |
| La transformée de Fourier vue sous l’angle du calcul numérique | archives-ouvertes.fr |
| The Fourier Transform of the Box Function | thefouriertransform.com |
| Scientific Programming, Analysis and Visualization with Python | snowball.millersville.edu |
