import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
f = np.ones(101)
g = np.ones(101)/10
#y = np.linspace(1001)
for i in xrange(len(f)):
if i > 11:
f[i] = 0
g[i] = 0
plt.subplot(131)
plt.plot(f)
plt.subplot(132)
plt.plot(g)
plt.subplot(133)
plt.plot(f+g)
[<matplotlib.lines.Line2D at 0xb14f9d8c>]
plt.subplot(131)
plt.plot(np.fft.fft(f))
plt.subplot(132)
plt.plot(np.fft.fft(g))
plt.subplot(133)
plt.plot(np.fft.fft(f+g))
/usr/lib/python2.7/dist-packages/numpy/core/numeric.py:460: ComplexWarning: Casting complex values to real discards the imaginary part return array(a, dtype, copy=False, order=order)
[<matplotlib.lines.Line2D at 0xb14a39ec>]
Here we see how changing the width of a square wave or a top-hat function will change it's fourier spectrum.
the function g has double the width of the function f and the fourier spectrum of the function g is thinner than that of the function f
f = np.ones(101)
g = np.ones(101)
#y = np.linspace(1001)
for i in xrange(len(f)):
if i > 11:
f[i] = 0
if i > 21:
g[i] = 0
plt.subplot(141)
plt.plot(f)
plt.subplot(142)
plt.plot(g)
plt.subplot(143)
plt.plot(np.fft.fftshift(np.fft.fft(f)))
plt.subplot(144)
plt.plot(np.fft.fftshift(np.fft.fft(g)))
[<matplotlib.lines.Line2D at 0xb1452e4c>]
st = np.zeros(101) # saw tooth function
for i in xrange(len(st)):
st[i] = i%9
tr = np.zeros(101) # triangular function
for i in xrange(len(tr)):
if i%20 < 10:
tr[i] = i%10
if i%20 > 9:
tr[i] = 10-i%10
plt.subplot(121)
plt.plot(st)
plt.subplot(122)
plt.plot(tr)
[<matplotlib.lines.Line2D at 0xaecf768c>]
plt.subplot(121)
plt.plot(np.fft.fftshift(np.fft.fft(st)))
plt.subplot(122)
plt.plot(np.fft.fftshift(np.fft.fft(tr)))
[<matplotlib.lines.Line2D at 0xaec02dac>]
x = np.linspace(-5,5,501)
a = 0
b = 1
plt.subplot(121)
plt.plot(np.exp(-(x-a)**2/(2*b**2)))
plt.subplot(122)
plt.plot(np.fft.fftshift(np.fft.fft(np.exp(-(x-a)**2/(2*b**2)))))
plt.xlim([240,260])
(240, 260)
from ipywidgets import StaticInteract, RangeWidget, RadioWidget
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
#var = 1
def plot(var):
fig, ax = plt.subplots(figsize=(4, 3),subplot_kw={'axisbg':'#EEEEEE','axisbelow':True})
ax.grid(color='w', linewidth=2, linestyle='solid')
#ax.legend(loc='upper right')
x = np.linspace(-2, 2, 201)
y = (1./(var*(2*np.pi)**(1./2)))*np.exp(-x**2/(2*var**2))
ax.plot(x, y, lw=5, alpha=0.4,label=var)
return fig
StaticInteract(plot,var=RangeWidget(0.01, 0.1, 0.01))
from ipywidgets import StaticInteract, RangeWidget, RadioWidget
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
#var = 1
def plot(var):
fig, ax = plt.subplots(figsize=(4, 3),subplot_kw={'axisbg':'#EEEEEE','axisbelow':True})
ax.grid(color='w', linewidth=2, linestyle='solid')
#ax.legend(loc='upper right')
x = np.linspace(-2, 2, 201)
y = np.fft.fftshift(np.fft.fft((1./(var*(2*np.pi)**(1./2)))*np.exp(-x**2/(2*var**2))))
ax.plot(y, lw=5, alpha=0.4,label=var)
#ax.set_xlim(150,200)
return fig
StaticInteract(plot,var=RangeWidget(0.01, 0.1, 0.01))