-
Notifications
You must be signed in to change notification settings - Fork 0
/
fourier.py
53 lines (40 loc) · 1.05 KB
/
fourier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def quadr(x, k):
'''
general term of the series
x : float time
k : int sum index
'''
return (4 / (k * np.pi)) * (np.sin((2 * np.pi * k * x)))
N = 30
y = np.linspace(-2, 2, 10000)
C = np.zeros(len(y))
A = np.zeros((len(y), N))
for i in range (0,N):
k = 2*i + 1
C = C + quadr(y,k)
A[:,i] = C
fig = plt.figure(1)
plt.title("Animation of fourier's series N=%d" %N)
plt.xlabel('time')
plt.ylabel('amplitude')
plt.xlim(np.min(y), np.max(y))
plt.ylim(np.min(C)-1/2, np.max(C)+1/2)
plt.grid()
line, = plt.plot([], [], 'b')
def animate(i):
line.set_data(y, A[:,i])
return line,
anim = animation.FuncAnimation(fig, animate, frames=N, interval=200, blit=True, repeat=True)
#anim.save('Fourier.mp4', fps=5, extra_args=['-vcodec', 'libx264'])
"""
plt.figure(2)
plt.title("square wave N=%d" %N)
plt.xlabel('time')
plt.ylabel('amplitude')
plt.grid()
plt.plot(y, C)
"""
plt.show()