Skip to content

Instantly share code, notes, and snippets.

@alg0trader
Created March 11, 2016 02:42
Show Gist options
  • Save alg0trader/de2b129fb41824f12a11 to your computer and use it in GitHub Desktop.
Save alg0trader/de2b129fb41824f12a11 to your computer and use it in GitHub Desktop.
"""
Animation of Fourier series square-wave.
author: Austin Schaller
email: schaller.austin@gmail.com
license: None
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
import seaborn as sns; sns.set()
w0 = 2*np.pi
fig = plt.figure()
ax = plt.axes(xlim=(0,2), ylim=(-2,2))
line, = ax.plot([], [], lw=2)
# initialization
def init():
line.set_data([], [])
return line,
# animation function, called sequentially
def animate(i):
x = np.linspace(0, 10, 10000)
y = np.sin(w0*x)
for j in range(3, i + 1):
if j % 2 == True:
y += np.sin(w0*j*x)/j
line.set_data(x, y)
return line,
# call the animator
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=150, interval=30, blit=True)
plt.title('Fourier Series Square-wave')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude (V)')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment