Oscillatory behavior of the Fourier series around a jump discontinuity.
import numpy as np
from numpy.fft import fft,ifft,fftshift
import matplotlib.pyplot as plt
N = 1000
L = 2
x = np.arange(-N, N)*L/N
f = np.heaviside(np.sin(np.pi*x), 0.0)
f_hat = fft(f)
plt.scatter(x, f)
<matplotlib.collections.PathCollection at 0x7ff6f51dd0a0>
def Gibbs(fun_hat, M):
g_hat = fun_hat.copy()
g_hat[M+1:2*N-M] = 0.0
fun = np.real(ifft(g_hat))
return fun
#fig = plt.figure(figsize=plt.figaspect(0.1))
fig = plt.figure(figsize=(20, 4))
ax = fig.add_subplot(1, 3, 1)
ax.plot(x, Gibbs(f_hat, 10))
ax.plot(x, f, lw=5, alpha=0.5)
ax = fig.add_subplot(1, 3, 2)
ax.plot(x, Gibbs(f_hat, 30))
ax.plot(x, f, lw=5, alpha=0.5)
ax = fig.add_subplot(1, 3, 3)
ax.plot(x, Gibbs(f_hat, 200))
ax.plot(x, f, lw=5, alpha=0.5)
plt.show()
#fig = plt.figure(figsize=plt.figaspect(0.1))
fig = plt.figure(figsize=(20, 4))
ax = fig.add_subplot(1, 3, 1)
ax.plot(x, Gibbs(f_hat, 10))
ax.plot(x, f, lw=5, alpha=0.5)
plt.xlim(-0.2, 0.2)
ax = fig.add_subplot(1, 3, 2)
ax.plot(x, Gibbs(f_hat, 30))
ax.plot(x, f, lw=5, alpha=0.5)
plt.xlim(-0.2, 0.2)
ax = fig.add_subplot(1, 3, 3)
ax.plot(x, Gibbs(f_hat, 200))
ax.plot(x, f, lw=5, alpha=0.5)
plt.xlim(-0.2, 0.2)
plt.show()