Home > other >  The sawtooth wave decomposition
The sawtooth wave decomposition

Time:11-22

 import numpy as np 
The import matplotlib. Pyplot as PLT

PI=np. PI
AA=4 # amplitude
OMIGA=PI # frequency
Num=500 # higher harmonic number

X=np. Linspace (0, 10, 1000)
Y=np. Zeros ((Num, x.s hape [0]))
For I in range (1, Num + 1) :
[I - 1)=y (1) * * (I + 1)/(I * PI) * AA * np in sin (I * OMIGA * x)
Z=np. Sum (y, axis=0)
FIG, ax=PLT. Subplots (2, 2)
PLT. Subplot (221)
PLT. The plot (x, y [0], '-' b)
PLT. Subplot (222)
PLT. The plot (x, y [1], '-' b)
PLT. Subplot (223)
PLT. The plot (x, y [2], '-' b)
PLT. Subplot (224)
PLT. The plot (x, z, '-' c)
PLT. The show ()


Powerful, beautiful picture.

CodePudding user response:

The improved version.
 
The import numpy as np
The import matplotlib. Pyplot as PLT

PI=np. PI
Num=5000 # harmonic number
C0=0; C1=1; Coefficient of C3=4 # constant term, series, the third parameter

Def SGN (n) :
Return 1 if n % 2==0 else - 1

Def FTCS (n) : # fc (n) * cos (tc) (n) * x + fs (n) * sin (n) * x (ts) - & gt; (fc, tc, fs, ts)
# # return 0, 0, SGN (n + 1) * C3/(n * PI), n # sawtooth
# # return 0, 0, SGN (n + 1) * C3/(n * PI), 0 if n % 2==0 # else n square wave
Return 1/(2 * n - 1) * * 2, (2 * n - 1)/C3 * PI, 0, 0 # triangle wave

X=np. Linspace (1000-5 * PI, 5 * PI,)
Tick=np. Arange (5 * PI, PI + 1, 5 * PI)
Label=np. Array ([5 "PI", "- 4 PI", "- 3 PI", "- the two PI", "1 PI", "0", "PI", "two PI", "3 PI", "four PI", "PI" 5])
Y=np. Zeros ((Num + 1, x.s hape [0]))
Y [0]=C0
For I in range (1, Num + 1) :
A, b, c, d=FTCS (I)
[I] y=C1 * (a * np. Cos (b) * x + c * np in sin (d * x))
FIG, ax=PLT. Subplots (2, 2)
For I in range (4) :
The pattern, arr=(' - 'b, y + 1] [I) if I!=3 else (c -, np. The sum (y axis=0))
PLT. Subplot (2, 2, I + 1)
If I!=3:
PLT. Xlabel (n=% d "" % (I + 1))
The else:
PLT. Xlabel (" Total ")
PLT. Xticks (tick, label)
PLT. The plot (x, arr, pattern)
PLT. The show ()
  • Related