Skip to content

Commit 49fda6a

Browse files
Add files via upload
1 parent 4a50211 commit 49fda6a

File tree

2 files changed

+95
-64
lines changed

2 files changed

+95
-64
lines changed

TN_code/fourier/fourier_coeff.py

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,65 @@
11
#!/usr/bin/env python
22

3-
from sympy import pi, cos, sin, sqrt, arg, exp, Abs, S
4-
from sympy import integrate, Rational, nfloat, conjugate, Symbol
5-
from sympy.functions.special.delta_functions import Piecewise
3+
import logging
64

7-
t = Symbol('t')
5+
try:
6+
from sympy import pi, cos, sin, sqrt, arg, exp, Abs, S
7+
from sympy import integrate, Rational, nfloat, conjugate, Symbol
8+
from sympy.functions.special.delta_functions import Piecewise
9+
except ImportError as e:
10+
print("sympy kon niet geïmporteerd worden")
11+
raise e
12+
13+
14+
t = Symbol("t")
815

916

1017
def Heaviside(arg):
11-
''' Heaviside die ook op arg=0 gedefinieerd is '''
18+
"""Heaviside die ook op arg=0 gedefinieerd is"""
1219
return Piecewise((0, arg < 0), (1, arg >= 0))
1320

1421

1522
# functies om de fourier-reeks coefficienten uit te rekenen
1623
def an(f, T, n, omega0):
17-
return 2/T*integrate(f*cos(n*omega0*t), (t, 0, T))
24+
return 2 / T * integrate(f * cos(n * omega0 * t), (t, 0, T)).nsimplify()
1825

1926

2027
def bn(f, T, n, omega0):
21-
return 2/T*integrate(f*sin(n*omega0*t), (t, 0, T))
28+
return 2 / T * integrate(f * sin(n * omega0 * t), (t, 0, T)).nsimplify()
2229

2330

24-
def a0(f, T, omega0):
25-
return 1/T*integrate(f, (t, 0, T)).nsimplify()
31+
def a0(f, T):
32+
return 1 / T * integrate(f, (t, 0, T)).nsimplify()
2633

2734

2835
def fourreeks(f, T, N):
36+
print("berekenen...")
2937
N += 1 # voor Maple
30-
a = (N-1)*[0]
31-
b = (N-1)*[0]
32-
omega0 = 2*pi/T
33-
som = a0(f, T, omega0)
38+
a = (N - 1) * [0]
39+
b = (N - 1) * [0]
40+
omega0 = 2 * pi / T
41+
som = a0(f, T)
42+
logging.info(f"a0 = {som}")
3443
for n in range(1, N):
35-
a[n-1] = an(f, T, n, omega0)
36-
b[n-1] = bn(f, T, n, omega0)
37-
som += (a[n-1]*cos(n*omega0*t) + b[n-1]*sin(n*omega0*t)).nsimplify()
44+
a[n - 1] = an(f, T, n, omega0)
45+
logging.info(f"a[{n - 1}] = {a[n - 1]}")
46+
b[n - 1] = bn(f, T, n, omega0)
47+
logging.info(f"b[{n - 1}] = {b[n - 1]}")
48+
som += (
49+
a[n - 1] * cos(n * omega0 * t) + b[n - 1] * sin(n * omega0 * t)
50+
).nsimplify()
3851
return som, a, b
3952

4053

41-
def spec(a, b, T):
42-
omega0 = 2*pi/T
43-
mag = [nfloat(sqrt(a[i]**2 + b[i]**2)) for i in range(len(a))]
44-
phi = len(a)*[0]
54+
def spec(f, a, b, T):
55+
mag = [nfloat(sqrt(a[i] ** 2 + b[i] ** 2)) for i in range(len(a))]
56+
phi = len(a) * [0]
4557
for i in range(len(b)):
4658
if a[i] == S.Zero:
4759
phi[i] = 0
4860
else:
49-
phi[i] = nfloat(arg(a[i] - S.ImaginaryUnit*b[i]))
50-
mag.insert(0, a0(f, T, omega0))
61+
phi[i] = nfloat(arg(a[i] - S.ImaginaryUnit * b[i]))
62+
mag.insert(0, a0(f, T))
5163
if mag[0] >= 0:
5264
phi.insert(0, 0)
5365
else:
@@ -58,36 +70,45 @@ def spec(a, b, T):
5870
def periodiek(f, T, N):
5971
som = S(0)
6072
for n in range(N):
61-
som += (Heaviside(t-n*T) - Heaviside(t-(n+1)*T))*f.replace(t, t-n*T)
73+
som += (Heaviside(t - n * T) - Heaviside(t - (n + 1) * T)) * f.replace(
74+
t, t - n * T
75+
)
6276
return som
6377

6478

6579
def plot_color(p):
66-
color = ['r', 'b', 'g', 'y', 'm']
80+
color = ["r", "b", "g", "y", "m"]
6781
Nc = len(color)
6882
for i, pc in enumerate(p):
6983
pc.line_color = color[i % Nc]
7084

7185

72-
if __name__ == '__main__':
86+
if __name__ == "__main__":
7387
from sympy.plotting import plot
7488
import matplotlib.pyplot as plt
7589

76-
f = Heaviside(t+1) - Heaviside(t-1)
90+
f = Heaviside(t + 1) - Heaviside(t - 1)
7791
T = 4
7892
N = 5
7993
teind = 10
8094
som, a, b = fourreeks(f, T, N)
81-
mag, phase = spec(a, b, T)
82-
plot(som, (t, 0, teind))
83-
p = plot(periodiek(f, T, int(teind/T)+1), som, (t, 0, teind),
84-
adaptive=False, show=False)
95+
p = plot(
96+
periodiek(f, T, teind),
97+
som,
98+
(t, 0, teind),
99+
adaptive=True,
100+
show=False,
101+
)
85102
plot_color(p)
86103
p.show()
87-
phase, mag = spec(a, b, T)
104+
phase, mag = spec(f, a, b, T)
105+
88106
plt.figure()
107+
89108
plt.subplot(2, 1, 1)
90109
plt.plot(range(len(phase)), phase)
110+
91111
plt.subplot(2, 1, 2)
92112
plt.plot(range(len(mag)), mag)
113+
93114
plt.show()

TN_code/fourier/fourier_complex.py

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,79 @@
66
from sympy.plotting import plot
77
import matplotlib.pyplot as plt
88

9-
t = Symbol('t', real=True) # anders complexe tijd!
9+
t = Symbol("t", real=True) # anders complexe tijd!
1010

1111

1212
def ac(f, T, n, omega0):
13-
return 1/T*integrate(f*exp(-S.ImaginaryUnit*n*omega0*t),
14-
(t, -T/2, T/2)).nsimplify()
13+
return (
14+
1
15+
/ T
16+
* integrate(
17+
f * exp(-S.ImaginaryUnit * n * omega0 * t), (t, -T / 2, T / 2)
18+
).nsimplify()
19+
)
1520

1621

1722
def fourreeks(f, T, N):
1823
N += 1 # voor Maple
19-
a = (N-1)*[0]
20-
b = (N-1)*[0]
21-
omega0 = 2*pi/T
24+
a = (N - 1) * [0]
25+
b = (N - 1) * [0]
26+
omega0 = 2 * pi / T
2227
som = a0(f, T, omega0)
2328
for n in range(1, N):
24-
a[n-1] = an(f, T, n, omega0)
25-
b[n-1] = bn(f, T, n, omega0)
26-
som += (a[n-1]*cos(n*omega0*t) + b[n-1]*sin(n*omega0*t)).nsimplify()
29+
a[n - 1] = an(f, T, n, omega0)
30+
b[n - 1] = bn(f, T, n, omega0)
31+
som += (
32+
a[n - 1] * cos(n * omega0 * t) + b[n - 1] * sin(n * omega0 * t)
33+
).nsimplify()
2734
return som, a, b
2835

2936

3037
def cfourreeks(f, T, N):
31-
''' Let op! Periode tussen -T/2 en T/2! '''
38+
"""Let op! Periode tussen -T/2 en T/2!"""
3239
N += 1
33-
omega0 = 2*pi/T
34-
cp = (N-1)*[0]
35-
cn = (N-1)*[0]
36-
mag = (2*N-1)*[0]
37-
phase = (2*N-1)*[0]
38-
c0 = 1/T*integrate(f, (t, -T/2, T/2)).nsimplify()
39-
mag[N-1] = Abs(nfloat(c0))
40-
phase[N-1] = 0
40+
omega0 = 2 * pi / T
41+
cp = (N - 1) * [0]
42+
cn = (N - 1) * [0]
43+
mag = (2 * N - 1) * [0]
44+
phase = (2 * N - 1) * [0]
45+
c0 = 1 / T * integrate(f, (t, -T / 2, T / 2)).nsimplify()
46+
mag[N - 1] = Abs(nfloat(c0))
47+
phase[N - 1] = 0
4148
som = c0
4249
for n in range(1, N):
43-
cp[n-1] = ac(f, T, n, omega0)
44-
cn[n-1] = conjugate(cp[n-1])
45-
mag[N-n-1] = 2*Abs(nfloat(cn[n-1]))
46-
mag[N+n-1] = 2*Abs(nfloat(cp[n-1]))
47-
phase[N-n-1] = arg(cn[n-1])
48-
phase[N+n-1] = arg(cp[n-1])
49-
som += (cp[n-1]*exp(S.ImaginaryUnit*n*omega0*t) +
50-
cn[n-1]*exp(-S.ImaginaryUnit*n*omega0*t))
50+
cp[n - 1] = ac(f, T, n, omega0)
51+
cn[n - 1] = conjugate(cp[n - 1])
52+
mag[N - n - 1] = 2 * Abs(nfloat(cn[n - 1]))
53+
mag[N + n - 1] = 2 * Abs(nfloat(cp[n - 1]))
54+
phase[N - n - 1] = arg(cn[n - 1])
55+
phase[N + n - 1] = arg(cp[n - 1])
56+
som += cp[n - 1] * exp(S.ImaginaryUnit * n * omega0 * t) + cn[n - 1] * exp(
57+
-S.ImaginaryUnit * n * omega0 * t
58+
)
5159
cp.insert(0, c0)
5260
return som, mag, phase
5361

5462

5563
def periodiek(f, T, N):
5664
som = S(0)
5765
for n in range(N):
58-
som += (Heaviside(t-n*T) - Heaviside(t-(n+1)*T))*f.replace(t, t-n*T)
66+
som += (Heaviside(t - n * T) - Heaviside(t - (n + 1) * T)) * f.replace(
67+
t, t - n * T
68+
)
5969
return som
6070

6171

62-
if __name__ == '__main__':
63-
f = (Heaviside(t+pi) - Heaviside(t))*(t+pi)
64-
T = 2*pi
72+
if __name__ == "__main__":
73+
f = (Heaviside(t + pi) - Heaviside(t)) * (t + pi)
74+
T = 2 * pi
6575
N = 5
6676
fr, mag, phase = cfourreeks(f, T, N)
67-
plot(f, (t, -2*pi, 2*pi), adaptive=False)
77+
plot(f, (t, -2 * pi, 2 * pi), adaptive=False)
6878
plt.show()
6979
plt.figure()
7080
plt.subplot(2, 1, 1)
71-
plt.stem(range(-N, N+1), mag, 'r')
81+
plt.stem(range(-N, N + 1), mag, "r")
7282
plt.subplot(2, 1, 2)
73-
plt.stem(range(-N, N+1), phase, 'b')
83+
plt.stem(range(-N, N + 1), phase, "b")
7484
plt.show()

0 commit comments

Comments
 (0)