How to apply a numerical Fourier transform for a simple function using python ?

Published: March 17, 2019

DMCA.com Protection Status

Some examples of how to calculate and plot the Fourier transform using python and scipy fft

Cosinus function

How to apply a numerical Fourier transform for a simple function using python ? How to apply a numerical Fourier transform for a simple function using python ?
How to apply a numerical Fourier transform for a simple function using python ?

import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack

#----------------------------------------------------------------------------------------#

N = 50000 # Number of samplepoints

T = 1.0 / 1000.0 # sample spacing

x = 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

How to apply a numerical Fourier transform for a simple function using python ? How to apply a numerical Fourier transform for a simple function using python ?
How to apply a numerical Fourier transform for a simple function using python ?

N = 50000 # Number of samplepoints

T = 1.0 / 1000.0 # sample spacing

x = 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

How to apply a numerical Fourier transform for a simple function using python ? How to apply a numerical Fourier transform for a simple function using python ?
How to apply a numerical Fourier transform for a simple function using python ? How to apply a numerical Fourier transform for a simple function using python ?
How to apply a numerical Fourier transform for a simple function using python ?

N = 50000 # Number of samplepoints

T = 1.0 / 1000.0 # sample spacing

x = 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.0

plt.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