Réflexion et transmission des ondes sonores

FondamentalDiaporama du micro-contenu (rafraichir la page si redimensionnée)

SimulationProgrammes Python

1
# -*- coding: utf-8 -*-
2
"""
3
Son Monochromatique
4
O. Thual, 21/08/2021
5
"""
6
7
#  clear all
8
for iglob in list(globals().keys()):
9
    if(iglob[0] != '_'):
10
        exec('del {}'.format(iglob))
11
# import libraries
12
import numpy as np
13
import matplotlib.pyplot as plt
14
import os
15
16
17
def inifig(xaxe=0,yaxe=0,xlab='x',ylab='y'):
18
    plt.figure(2)
19
    plt.axvline(xaxe)
20
    plt.axhline(yaxe)
21
    plt.xticks(fontsize=12)
22
    plt.yticks(fontsize=12)
23
    plt.xlabel(xlab,fontsize=16 )
24
    plt.ylabel(ylab,fontsize=16)
25
    
26
def zfi(x,le=2):
27
    miss=le-len(str(x))
28
    a='0'*miss+str(x)
29
    return a
30
31
def animation(name):
32
    a=np.linspace(0,L,N+1);
33
    acont=np.linspace(0,L,501);
34
    t=0; 
35
    dt=Time/Nt
36
    for i in range(0,Nt):
37
        title=name+" i="+zfi(i)
38
        titlefig=name
39
        print(title)
40
        fig=plt.figure(i,figsize=(7,4))
41
        plt.xlabel(r'$x$',fontsize=16 )
42
        plt.ylabel(r'Pression $\widetilde p$',fontsize=16)
43
        plt.title(titlefig,fontsize=16)
44
        # signal
45
        t=dt*i;
46
        xi=signal(a,t)
47
        pcont=pression(acont,t)
48
        for dup in duprange:
49
            plt.scatter(a+sc*xi,dup+0*xi,marker='o',color='blue',s=40)
50
            amid=a[int(N/2)]; ximid=xi[int(N/2)];
51
            plt.scatter(amid+sc*ximid,dup+0*ximid,marker='o',color='red',s=80)
52
            #plt.scatter(a,dup+0*xi,marker='o',color='red',s=20)
53
        plt.plot(acont,pcont,color='black',linewidth=3)
54
        #plt.scatter(a,xi,marker='o',color='black',s=40)
55
        plt.xlim(0,L)
56
        plt.ylim(ymin,ymax)
57
        namei=name+zfi(i)+'.png';
58
        plt.grid(color='black', axis='y', linestyle='-', linewidth=1)        
59
        plt.grid(color='black', axis='x', linestyle='-', linewidth=1)        
60
        plt.savefig(namei)
61
        plt.show()
62
        plt.close()
63
    gifanim="Anim"+name+".gif"
64
    #os.system('/opt/local/bin/convert -set delay '+delay+' '+name+'* '+gifanim);
65
    os.system('/usr/local/bin/convert -set delay '+delay+' '+name+'* '+gifanim);
66
    os.system('rm '+name+'*');   
67
68
# Main 
69
F=False; T=True
70
71
 # Progressive-cos
72
if T: 
73
    N=20; L=10; c=1; 
74
    Nt=20; sc=1.5*L/N # scaling 
75
    k=np.pi/L; omega=k*c; 
76
    ymin=-1; ymax=1; Time=2*np.pi/omega
77
    duprange=np.linspace(-1,1,9)
78
    delay="40"
79
    # signal 
80
    def signal(a,t):
81
        xi=np.cos(n*k*(a-c*t))
82
        return xi
83
    def pression(a,t):
84
        p=np.sin(n*k*(a-c*t))
85
        return p
86
    for m in range(20,30,10):
87
        n=m/10; T=2*L/(n*c); dt=T/Nt; T=T-dt;
88
        print("Progressive-cos-10xn="+zfi(m))
89
        animation("Onde-Pression-sinus-10xn="+zfi(m)) 
90
91
 
92
1
# -*- coding: utf-8 -*-
2
"""
3
Debit son pulse
4
O. Thual, 21/08/2021
5
"""
6
7
#  clear all
8
for iglob in list(globals().keys()):
9
    if(iglob[0] != '_'):
10
        exec('del {}'.format(iglob))
11
# import libraries
12
import numpy as np
13
import matplotlib.pyplot as plt
14
import os
15
16
17
def inifig(xaxe=0,yaxe=0,xlab='x',ylab='y'):
18
    plt.figure(2)
19
    plt.axvline(xaxe)
20
    plt.axhline(yaxe)
21
    plt.xticks(fontsize=12)
22
    plt.yticks(fontsize=12)
23
    plt.xlabel(xlab,fontsize=16 )
24
    plt.ylabel(ylab,fontsize=16)
25
    
26
def zfi(x,le=2):
27
    miss=le-len(str(x))
28
    a='0'*miss+str(x)
29
    return a
30
31
def animationboth(name):
32
    a=np.linspace(0,L,N+1);
33
    acont=np.linspace(0,L,501);
34
    t=0; 
35
    dt=T/Nt
36
    for i in range(0,Nt+1):
37
        title=name+" i="+zfi(i)
38
        titlefig=name
39
        print(title)
40
        fig=plt.figure(N,figsize=(7,4))
41
        plt.xlabel('x',fontsize=16 )
42
        plt.ylabel(r'$p$',fontsize=16)
43
        plt.title(titlefig,fontsize=16)
44
        t=dt*i;
45
        # discret
46
        xi=depla(a,t)
47
        for dup in duprange:
48
            plt.scatter(a+sc*xi,dup+0*xi,marker='o',color='blue',s=40)
49
            amid=a[int(N/4)]; ximid=xi[int(N/4)];
50
            plt.scatter(amid+sc*ximid,dup+0*ximid,marker='o',color='red',s=80)
51
            amid=a[int(3*N/4)]; ximid=xi[int(3*N/4)];
52
            plt.scatter(amid+sc*ximid,dup+0*ximid,marker='o',color='green',s=80)
53
           #plt.scatter(a,dup+0*xi,marker='o',color='red',s=20)
54
        # continu 
55
        xicont=depla(acont,t)
56
        pcont=signal(acont,t)
57
        pleft=signalleft(acont,t)
58
        pright=signalright(acont,t)
59
        #plt.plot(acont,xicont,color='blue',linewidth=3)
60
        plt.plot(acont+sc*xicont,pcont,color='black',linewidth=3)
61
        plt.plot(acont+sc*xicont,pleft,color='green',linewidth=3)
62
        plt.plot(acont+sc*xicont,pright,color='red',linewidth=3)
63
        plt.xlim(0,L)
64
        plt.ylim(ymin,ymax)
65
        namei=name+zfi(i)+'.png';
66
        plt.grid(color='black', axis='y', linestyle='-', linewidth=1)        
67
        plt.grid(color='black', axis='x', linestyle='-', linewidth=1)        
68
        plt.savefig(namei)
69
        plt.show()
70
        plt.close()
71
    gifanim="Anim"+name+".gif"
72
    #os.system('/opt/local/bin/convert -set delay '+delay+' '+name+'* '+gifanim);
73
    os.system('/usr/local/bin/convert -set delay '+delay+' '+name+'* '+gifanim);
74
    if flagp: os.system('rm '+name+'*');  
75
76
77
78
79
# Main 
80
F=False; T=True
81
flagp=T; flagall=F;
82
83
def pulse(a,d):
84
    al=0*a[a<-d]; ac=a[np.abs(a)<=d]; ar=0*a[a>d];
85
    k=np.pi/d; fc=.5*(1+np.cos(k*ac));
86
    f=np.concatenate((al,fc,ar))
87
    return f
88
89
def pulsem(a,d):
90
    al=0*a[a<-d]; ac=a[np.abs(a)<=d]; ar=0*a[a>d];
91
    k=np.pi/d; fc=ac-np.pi/k+np.sin(k*ac)/k
92
    f=np.concatenate((al-2*np.pi/k,fc,ar))
93
    return f
94
95
def pulsep(a,d):
96
    al=0*a[a<-d]; ac=a[np.abs(a)<=d]; ar=0*a[a>d];
97
    k=np.pi/d; fc=ac+np.pi/k+np.sin(k*ac)/k
98
    f=np.concatenate((al,fc,ar+2*np.pi/k))
99
    return f
100
101
102
# Deux pulses inegaux
103
if T or flagall: 
104
    N=30; L=10; d=2; c=1; 
105
    T=(L+2*d)/c; Nt=20; sc=.5*L/N # scaling 
106
    k=np.pi/d;
107
    Al=.4; Ar=.6
108
    #Al=0; Ar=.3; d=1
109
    ymin=-.1; ymax=1;
110
    #ymin=-2; ymax=2;
111
    duprange=np.linspace(-.2,1,7)
112
    delay="80"
113
    # signal 
114
    def depla(a,t):
115
        xi=-Ar*pulsem(a+d-c*t,d)-Al*pulsep(a-L-d+c*t,d)
116
        return xi
117
    def signal(a,t):
118
        am=a-c*t;ap=a+c*t;
119
        p=Ar*pulse(a+d-c*t,d)+Al*pulse(a-L-d+c*t,d)
120
        return p
121
    def signalleft(a,t):
122
        p=Al*pulse(a-L-d+c*t,d)
123
        return p
124
    def signalright(a,t):
125
        p=Ar*pulse(a+d-c*t,d)
126
        return p
127
    animationboth("Deux-pulses-inegaux") 
128
129
130
131
132
133
1
# -*- coding: utf-8 -*-
2
"""
3
Reflexion Transmission
4
O. Thual, 21/08/2021
5
"""
6
7
#  clear all
8
for iglob in list(globals().keys()):
9
    if(iglob[0] != '_'):
10
        exec('del {}'.format(iglob))
11
# import libraries
12
import numpy as np
13
import matplotlib.pyplot as plt
14
import os
15
16
17
def inifig(xaxe=0,yaxe=0,xlab='x',ylab='y'):
18
    plt.figure(2)
19
    plt.axvline(xaxe)
20
    plt.axhline(yaxe)
21
    plt.xticks(fontsize=12)
22
    plt.yticks(fontsize=12)
23
    plt.xlabel(xlab,fontsize=16 )
24
    plt.ylabel(ylab,fontsize=16)
25
    
26
def zfi(x,le=2):
27
    miss=le-len(str(x))
28
    a='0'*miss+str(x)
29
    return a
30
31
32
def animatrois(name):
33
    N=20; a=np.linspace(-L,L,N+1);
34
    acont=np.linspace(-L,L,501);
35
    t=0; 
36
    dt=Time/Nt
37
    for i in range(0,Nt+1):
38
        title=name+" i="+zfi(i)
39
        titlefig=name
40
        print(title)
41
        fig=plt.figure(i,figsize=(7,4))
42
        plt.xlabel(r'$x$',fontsize=16 )
43
        plt.ylabel(r'$f$',fontsize=16)
44
        plt.title(titlefig,fontsize=16)
45
        # signal
46
        t=dt*i;
47
        fin=finc(acont,t)
48
        fre=fref(acont,t)
49
        ftr=ftra(acont,t)
50
        ftot=fin+fre+ftr
51
        plt.plot(acont,fin,color='red',linewidth=3)
52
        plt.plot(acont,fre,color='green',linewidth=3)
53
        plt.plot(acont,ftr,color='red',linewidth=3)
54
        plt.plot(acont,ftot,color='black',linewidth=3)
55
        plt.plot([0,0],[ymin,ymax],color='blue',linewidth=3)
56
        plt.xlim(-L,L2)
57
        plt.ylim(ymin,ymax)
58
        namei=name+zfi(i)+'.png';
59
        plt.grid(color='black', axis='y', linestyle='-', linewidth=1)        
60
        plt.savefig(namei)
61
        plt.show()
62
        plt.close()
63
    gifanim="Anim"+name+".gif"
64
    #os.system('/opt/local/bin/convert -set delay '+delay+' '+name+'* '+gifanim);
65
    os.system('/usr/local/bin/convert -set delay '+delay+' '+name+'* '+gifanim);
66
    if flagp: os.system('rm '+name+'*');  
67
68
69
# Main 
70
F=False; T=True
71
flagp=F; flagall=F;
72
73
def pulse(a,d):
74
    al=0*a[a<-d]; ac=a[np.abs(a)<=d]; ar=0*a[a>d];
75
    k=np.pi/d; fc=.5*(1+np.cos(k*ac));
76
    f=np.concatenate((al,fc,ar))
77
    return f
78
79
80
def leftsin(a,k):
81
    al=a[a<=0]; ar=0*a[a>0];
82
    fl=-np.sin(k*al);
83
    f=np.concatenate((fl,ar))
84
    return f
85
86
def rightsin(a,k):
87
    al=0*a[a<=0]; ar=a[a>0];
88
    fr=np.sin(k*ar);
89
    f=np.concatenate((al,fr))
90
    return f
91
92
93
# Reflexion Transmission harmonique
94
if T or flagall:
95
    L=10; d=5; A=1; rho=1; c1=1;c2=1;
96
    Re=.8; ymin=-2; ymax=2;
97
    Re=-.8; ymin=-2; ymax=2;
98
    Re=.5; ymin=-1.5; ymax=1.5;
99
    Re=1; ymin=-2; ymax=2;
100
    Re=-1; ymin=-2; ymax=2;
101
    Tr=1-Re;
102
    print("c1=",c1,"c2=",c2,"Re=",Re)
103
    k=np.pi/d; k2=c2/c1*k; L2=min(L,L*c2/c1);
104
    Time=4*L/c1; Nt=40;
105
    delay="40"
106
    
107
    # signal 
108
    def finc(a,t):
109
        al=a[a<=0]; ar=0*a[a>0];
110
        fl=leftsin(L+al-c1*t,k)
111
        f=np.concatenate((fl,ar))
112
        return f
113
    def fref(a,t):
114
        al=a[a<=0]; ar=0*a[a>0];
115
        fl=-Re*rightsin(-L+al+c1*t,k)
116
        f=np.concatenate((fl,ar))
117
        return f
118
    def ftra(a,t):
119
        al=0*a[a<=0]; ar=a[a>0];
120
        fr=Tr*leftsin(L+c1/c2*(ar-c2*t),k)
121
        f=np.concatenate((al,fr))
122
        return f
123
    
124
    animatrois("Reflexion-Transmission-Harm-R="+zfi(Re)) 
125
    print("c1=",c1,"c2=",c2)
126
    print("Re=",Re,"Tr=",Tr)
127
128
# Reflexion Transmission pulse
129
if F or flagall:
130
    L=10; d=5; A=1; rho=1; c1=1; c2=1; d=5; 
131
    Re=.8; ymin=-1; ymax=1;
132
    Re=-.8; ymin=-.2; ymax=2;
133
    #Re=.5; ymin=-.5; ymax=1;
134
    Tr=1-Re;
135
    print("c1=",c1,"c2=",c2,"Re=",Re)
136
    L2=min(L,L*c2/c1);
137
    Time=3*L/c1; Nt=40;
138
    delay="40"
139
   
140
    # signal 
141
    def finc(a,t):
142
        al=a[a<=0]; ar=0*a[a>0];
143
        fl=pulse(L+d+al-c1*t,d)
144
        f=np.concatenate((fl,ar))
145
        return f
146
    def fref(a,t):
147
        al=a[a<=0]; ar=0*a[a>0];
148
        fl=-Re*pulse(-L-d+al+c1*t,d)
149
        f=np.concatenate((fl,ar))
150
        return f
151
    def ftra(a,t):
152
        al=0*a[a<=0]; ar=a[a>0];
153
        fr=Tr*pulse(L+d+c1/c2*(ar-c2*t),d)
154
        f=np.concatenate((al,fr))
155
        return f
156
157
    animatrois("Reflexion-Transmission-Pulse-R="+zfi(Re))
158
    print("c1=",c1,"c2=",c2)
159
    print("Re=",Re,"Tr=",Tr)
160
161
162
163
164
165
166
167
168