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
| import sympy as sp
from IPython.display import *
import matplotlib.pyplot as plt
t = sp.symbols('t')
nmax = 10
#f=sp.Function('f')
#f=sp.Abs(t) # triangle
#f=t # dent de scie
eta=10/50 # rapport cyclique
#f=-sp.Heaviside(t-eta/2)+sp.Heaviside(eta/2+t) # Impulsion rectangulaire
#f=sp.Heaviside(t-eta/2)-2*sp.Heaviside(t)+sp.Heaviside(eta/2+t) # double Impulsion rectangulaire
#f=t*(0.5-sp.sqrt(t**2))
#f=t*(.5-sp.Abs(t))
f=t*(1-t) # ni paire, ni impaire
sp.plot(f,(t,-.5,.5))
y=sp.fourier_series(f, (t, -0.5, 0.5))
an=[0]*(nmax)
bn=[0]*(nmax)
cn=[0]*(nmax)
phi=[0]*(nmax)
nn=[0]*(nmax)
for i in range( 0,nmax ) :
g=y.truncate(i+1)-y.truncate(i) # le ie harmonique non nul
an[i]=g.subs(t,0) # le an
nn[i]=(sp.sqrt(-sp.diff(g , t, 2)/g)/(2*sp.pi)).subs(t,0) # n
if (nn[i]!=0) : # cas pour i = 0 et moyenne non nulle : a0!= 0
bn[i]=g.subs(t,1/(4*nn[i])) # le bn : quand le cos est nul.
else :
bn[i]=0
cn[i]=sp.sqrt(an[i]*an[i]+bn[i]*bn[i])
phi[i]=sp.arg(an[i]-sp.I*bn[i]) # le phin
maxn=nn[i]
if (nn[0]!=0) : # prendre en compte les signaux de moyenne nulle
an.insert(0,0)
bn.insert(0,0)
cn.insert(0,0)
phi.insert(0,0)
nn.insert(0,0)
plt.stem(nn,cn)
plt.xlabel("Harmonique")
plt.ylabel("Amplitude")
plt.xticks(np.arange(0,float(maxn),step=max(1,int(float(maxn)/10))))
plt.show()
plt.stem(nn,phi)
plt.xlabel("Harmonique")
plt.ylabel("Phase")
plt.xticks(np.arange(0,float(maxn),step=max(1,int(float(maxn)/10))))
plt.show()
maxa=0
mina=0
maxb=0
minb=0
for i in range(len(nn)) :
if (an[i]>maxa) :
maxa=an[i]
if (an[i]<mina) :
mina=an[i]
if (bn[i]>maxb) :
maxb=bn[i]
if (bn[i]<minb) :
minb=bn[i]
if (maxa!=mina and minb != maxb) : # 3D possible
fig=plt.figure()
ax = plt.axes(projection = '3d' )
ax.set_xlim([0,float(maxn)])
ax.set_ylim([float(mina),float(maxa)])
ax.set_zlim([float(minb),float(maxb)])
ax.plot([maxn,0],[0,0],[0,0],color='r')
for i in range(len(nn)) :
# start=[nn[i],0,0]
ax.plot([nn[i],nn[i]],[0,an[i]],[0,0],color='black')
ax.plot([nn[i],nn[i]],[an[i],an[i]],[0,bn[i]],color='black')
ax.quiver(nn[i],0,0,0,an[i],bn[i])
sp.plot(y.truncate(1),y.truncate(2)-y.truncate(1),y.truncate(3)-y.truncate(2),y.truncate(4)-y.truncate(3),(t,-.5,.5))
sp.plot(y.truncate(1),y.truncate(2),y.truncate(3),y.truncate(4),y.truncate(40),(t,-1.5,1.5))
sp.plot(f,y.truncate(40),y.truncate(100),y.truncate(400),(t,.235,.265),adaptative=False,nb_of_points=1000) |
Partager