-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfourier.py
executable file
·140 lines (111 loc) · 3.27 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/python3
"""
Description: Display a Fourier series
"""
from common import *
# Import math Library
import math
x_min, x_max = -2, 2
y_min, y_max = -2, 2
T = 1
M = 1000 # partitionsfeinheit
h = (x_max - x_min)/M
xs = [ x_min + i * h for i in range(0,M+1) ]
ys = [ x_min + i * h for i in range(0,M+1) ]
def get_coefficients_squarewave( counter ):
a_0 = 0
a = [ 0 for m in range(0,counter) ]
b = [ 0 for m in range(0,counter) ]
for m in range(0,counter):
n = m+1
a[m] = 0
if n % 2 == 0 : continue
b[m] = 4 / math.pi * 1/(n)
return a_0, a, b
def get_coefficients_sawtooth( counter ):
a_0 = 0.5
a = [ 0 for m in range(0,counter) ]
b = [ 0 for m in range(0,counter) ]
for m in range(0,counter):
n = m+1
a[m] = 0
b[m] = -1/(math.pi*(n))
return a_0, a, b
def get_coefficients_triangle( counter ):
a_0 = 1 / 2
a = [ 0 for m in range(0,counter) ]
b = [ 0 for m in range(0,counter) ]
for m in range(0,counter):
n = m+1
if n % 2 == 0 : continue
a[m] = -4 / ( math.pi * math.pi * n*n)
b[m] = 0
return a_0, a, b
def get_coefficients_diraccomb( counter ):
a_0 = 0
a = [ 0 for m in range(0,counter) ]
b = [ 0 for m in range(0,counter) ]
for m in range(0,counter):
n = m+1
a[m] = 1. / ( math.pi )
b[m] = 0
return a_0, a, b
def get_coefficients_exercise1( counter ):
a_0 = 0
a = [ 0 for m in range(0,counter) ]
b = [ 0 for m in range(0,counter) ]
for m in range(0,counter):
n = m+1
if n == 1 or n % 2 == 0: continue
a[m] = 0.
b[m] = 1. / ( n*n )
return a_0, a, b
def get_coefficients_exercise2( counter ):
a_0 = 0
a = [ 0 for m in range(0,counter) ]
b = [ 0 for m in range(0,counter) ]
for m in range(0,counter):
n = m+1
if n % 2 == 0: continue
a[m] = (-1.)**((n+1)/2) / ( n*n*n )
b[m] = 0
return a_0, a, b
def get_coefficients_exercise3( counter ):
a_0 = 0
a = [ 0 for m in range(0,counter) ]
b = [ 0 for m in range(0,counter) ]
for m in range(0,counter):
n = m+1
a[m] = (-1.)**(n+1) / ( n*n )
b[m] = 0
return a_0, a, b
plt.xlim(x_min,x_max)
plt.ylim(y_min,y_max)
N = 60
counter = 0
while counter < N:
a_0, a, b = get_coefficients_sawtooth( counter )
a_0, a, b = get_coefficients_squarewave( counter )
a_0, a, b = get_coefficients_triangle( counter )
a_0, a, b = get_coefficients_exercise3( counter )
a_0, a, b = get_coefficients_exercise2( counter )
# a_0, a, b = get_coefficients_diraccomb( counter )
# print( a_0, a, b )
for i in range(0,len(ys)):
value = a_0
for m in range(0,counter):
n = m+1
value = value + a[m] * math.cos( 2 * math.pi * n * xs[i] / T )
value = value + b[m] * math.sin( 2 * math.pi * n * xs[i] / T )
ys[i] = value
line = plt.plot( xs, ys, color='blue', label='y', visible=True )
fig = plt.gcf()
# fig.set_size_inches(16, 9)
plt.xlim(x_min,x_max)
plt.ylim(y_min,y_max)
plt.pause(1.00)
if counter == 0: plt.pause(5.00)
plt.title ( "N = {}".format(counter) )
line.pop(0).remove()
counter = counter + 1
plt.show()