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
1017def 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
1623def 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
2027def 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
2835def 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):
5870def 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
6579def 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 ()
0 commit comments