-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFourierSeries.py
78 lines (65 loc) · 1.62 KB
/
FourierSeries.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
import numpy as np
import matplotlib.pyplot as plt
plt.style.use("ggplot")
# Setup
x_ = np.linspace(-20,20,10000)
T = 8
#for a more accurate wave approximation increase armonics (number of haronics)
armonics = 100
def squareWave(x):
global T
lowerBoundLeft = (-T/2)
lowerBoundRight = 0
upperBoundLeft = 0
upperBoundRight = (T/2)
one = 1
negativeOne = -1
while True:
if (x >= lowerBoundLeft) and (x <= lowerBoundRight):
return negativeOne
elif (x >= upperBoundLeft) and (x <= upperBoundRight):
return one
else:
lowerBoundLeft -= T/2
lowerBoundRight -= T/2
upperBoundLeft += T/2
upperBoundRight += T/2
if one == 1:
one = -1
negativeOne = 1
else:
one = 1
negativeOne = -1
# Bn coefficients
def bn(n):
n = int(n)
if (n%2 != 0):
return 4/(np.pi*n)
else:
return 0
# Wn
def wn(n):
global T
wn = (2*np.pi*n)/T
return wn
# Fourier Series function
def fourierSeries(n_max,x):
a0 = 0
partialSums = a0
for n in range(1,n_max):
try:
partialSums = partialSums + bn(n)*np.sin(wn(n)*x)
except:
print("pass")
pass
return partialSums
y = []
f = []
for i in x_:
y.append(squareWave(i))
f.append(fourierSeries(armonics,i))
plt.plot(x_,y,color="blue",label="Signal")
plt.plot(x_,f,color="red",label="Fourier series approximation")
plt.title("Fourier Series approximation number of armonics: "+str(armonics))
plt.legend()
plt.show()