Resource: Discrete Fourier transforms (scipy.fft)
from scipy import fft, ifft
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set()
import random as random
x = np.array([1.0, 2.0, 1.0, -1.0, 1.5])
print(x)
y = fft(x)
print(y)
yinv = ifft(y)
print(yinv)
[ 1. 2. 1. -1. 1.5] [ 4.5 +0.j 2.08155948-1.65109876j -1.83155948+1.60822041j -1.83155948-1.60822041j 2.08155948+1.65109876j] [ 1. +0.j 2. +0.j 1. +0.j -1. +0.j 1.5+0.j]
# Number of sample points
N = 600
# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y = np.sin(50.0 * 2.0*np.pi*x)+ \
0.5*np.sin(80.0 * 2.0*np.pi*x)+\
0.5*np.sin(100.0 * 2.0*np.pi*x)+\
0.5*np.sin(150.0 * 2.0*np.pi*x) +\
1.0*np.sin(200.0 * 2.0*np.pi*x) +\
0.5*np.sin(250.0 * 2.0*np.pi*x)
plt.figure(figsize=[15,4])
plt.plot(x,y)
plt.show()
yf = fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N//2)
plt.figure(figsize =[15,4])
plt.plot(xf, 2.0/N * np.abs(yf[0:N//2]))
plt.show()
# Number of sample points
N = 600
# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y = np.sin(50.0 * 2.0*np.pi*x)+ \
0.5*np.sin(80.0 * 2.0*np.pi*x)+\
0.5*np.sin(100.0 * 2.0*np.pi*x)+\
0.5*np.sin(150.0 * 2.0*np.pi*x) +\
1.0*np.sin(200.0 * 2.0*np.pi*x) +\
0.5*np.sin(250.0 * 2.0*np.pi*x) +\
np.array([random.uniform(0,1) for i in range(N)])
plt.figure(figsize=[15,4])
plt.plot(x,y)
plt.show()
yf = fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N//2)
plt.figure(figsize =[15,4])
plt.plot(xf, 2.0/N * np.abs(yf[0:N//2]))
plt.show()
from scipy import ifftn
import matplotlib.cm as cm
N = 30
f, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(2, 3, sharex='col', sharey='row')
xf = np.zeros((N,N))
xf[0, 5] = 1
xf[0, N-5] = 1
Z = ifftn(xf)
ax1.imshow(xf, cmap=cm.Reds)
ax4.imshow(np.real(Z), cmap=cm.gray)
xf = np.zeros((N, N))
xf[5, 0] = 1
xf[N-5, 0] = 1
Z = ifftn(xf)
ax2.imshow(xf, cmap=cm.Reds)
ax5.imshow(np.real(Z), cmap=cm.gray)
xf = np.zeros((N, N))
xf[5, 10] = 1
xf[N-5, N-10] = 1
Z = ifftn(xf)
ax3.imshow(xf, cmap=cm.Reds)
ax6.imshow(np.real(Z), cmap=cm.gray)
plt.show()
--------------------------------------------------------------------------- ImportError Traceback (most recent call last) <ipython-input-39-d5156ba37b68> in <module> ----> 1 from scipy import ifftn 2 import matplotlib.cm as cm 3 N = 30 4 f, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(2, 3, sharex='col', sharey='row') 5 xf = np.zeros((N,N)) ImportError: cannot import name 'ifftn' from 'scipy' (C:\ProgramData\Anaconda3\lib\site-packages\scipy\__init__.py)