2022年 11月 16日

python电路仿真001

基础电路

Chap1 RC充电电路

电路

在这里插入图片描述

充电过程

公式

时间常数τ = R x C,R以欧姆为单位的电阻值,C以法拉为单位的电容值。
初始条件假设,电容C完全“放电”且开关S完全打开。当开关闭合时,时间从t=0开始,电流开始通过电阻器流入电容器。
电容电压为:

V

c

=

V

s

(

1

e

t

/

R

C

)

Vc=Vs (1-e^{-t/RC} )

Vc=Vs(1et/RC)
电容电流为:

I

c

=

V

s

/

R

×

e

t

R

C

Ic=Vs/R×e^{-t⁄RC}

Ic=Vs/R×etRC

电容电压电流值跟时间常数τ的关系:

τ 倍数 电容电压Vs*% 电容电流Vs/R*%
0.5 39.35% 60.65%
0.7 50.34% 49.66%
1 63.21% 36.79%
2 86.47% 13.53%
3 95.02% 4.98%
4 98.17% 1.83%
5 99.33% 0.67%

源代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt

# 解决中文显示问题
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False

x = [0,0.5,0.7,1,2,3,4,5]
volt = 1-1/np.exp(x)
curr = 1/np.exp(x)

plt.figure()
plt.subplot(121)
plt.plot(x,volt*100,'bd:')
for a, b in zip(x, np.round(volt*100,2)):
    plt.text(a, b, (a,b),ha='left', va='top')
plt.grid(linestyle = ':')
plt.xlabel('时间常数τ = RC')
plt.ylabel('电容电压 %')

plt.subplot(122)
plt.plot(x,curr*100,'rd:')
for a, b in zip(x, np.round(curr*100,2)):
    plt.text(a, b, (a,b),ha='left', va='top')
plt.grid(linestyle = ':')
plt.xlabel('时间常数τ = RC')
plt.ylabel('电容电流 %')

plt.show()
  • 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

仿真结果

在这里插入图片描述

频率响应

(

1

/

j

w

C

)

/

(

R

+

1

/

j

w

C

)

=

1

/

(

1

+

j

w

R

C

)

=

(

1

j

w

R

C

)

/

(

1

+

(

w

R

C

)

2

)

=

1

/

(

1

+

(

w

R

C

)

2

)

j

w

R

C

/

(

1

+

(

w

R

C

)

2

)

(1/jwC)/(R+1/jwC)=1/(1+jwRC)=(1-jwRC)/(1+(wRC)^2 )=1/(1+(wRC)^2 )-j wRC/(1+(wRC)^2 )

(1/jwC)/(R+1/jwC)=1/(1+jwRC)=(1jwRC)/(1+(wRC)2)=1/(1+(wRC)2)jwRC/(1+(wRC)2)

python源代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt

# 解决中文显示问题
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False

f = np.logspace(0,8,100)
w = 2*np.pi*f
R = 10E3
C = 10E-9
y=1/(1+np.square(w*R*C))-1j*(w*R*C)/(1+np.square(w*R*C))

plt.figure()
plt.subplot(121)
plt.plot(w,np.abs(y),'b.-')
plt.grid(linestyle = ':')
plt.xscale('log')
plt.title('幅频特性曲线')
plt.xlabel('频率 Hz')
plt.ylabel('幅度 %')
plt.yticks([1,0.7,0.5,0.3,0],['100', '70', '50', '30', '0'])
plt.subplot(122)
plt.plot(w,np.angle(y)/(2*np.pi)*360,'r.-')
plt.grid(linestyle = ':')
plt.xscale('log')
plt.title('相频特性曲线')
plt.xlabel('频率 Hz')
plt.ylabel('相位 度')

plt.show()
  • 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

仿真结果

幅频相频特性曲线

Chap2 同频不同相信号叠加

同幅同频不同相位的信号叠加之后的,时域和频域的信号有什么区别?

公式

s

i

n

(

ω

t

+

a

)

+

s

i

n

(

ω

t

+

b

)

=

2

s

i

n

[

ω

t

+

(

a

+

b

)

/

2

]

c

o

s

[

(

a

b

)

/

2

]

sin(ωt+a)+sin(ωt+b)=2*sin[ωt+(a+b)/2]*cos[(a-b)/2]

sin(ωt+a)+sin(ωt+b)=2sin[ωt+(a+b)/2]cos[(ab)/2]

c

o

s

(

ω

t

+

a

)

+

c

o

s

(

ω

t

+

b

)

=

2

c

o

s

[

ω

t

+

(

a

+

b

)

/

2

]

c

o

s

[

(

a

b

)

/

2

]

cos(ωt+a)+cos(ωt+b)=2*cos[ωt+(a+b)/2]*cos[(a-b)/2]

cos(ωt+a)+cos(ωt+b)=2cos[ωt+(a+b)/2]cos[(ab)/2]

python源代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt

# 解决中文显示问题
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False

t = np.linspace(0,1,100)
fb = 10
wb = 2*np.pi*fb

out0   = np.sin(wb*t) + np.sin(wb*t)
out45  = np.sin(wb*t) + np.sin(wb*t+np.pi/4)
out90  = np.sin(wb*t) + np.sin(wb*t+np.pi/2)
out135 = np.sin(wb*t) + np.sin(wb*t+np.pi/4*3)
out180 = np.sin(wb*t) + np.sin(wb*t+np.pi)

plt.figure()
plt.subplot(251)
plt.plot(out0,'b.:')
plt.grid(linestyle = ':')
plt.title('0相位差')
plt.ylim([-3,3])
plt.subplot(256)
plt.plot(np.abs(np.fft.fft(out0)),'r.:')
plt.ylim([0,100])

plt.subplot(252)
plt.plot(out45,'b.:')
plt.grid(linestyle = ':')
plt.title('45相位差')
plt.ylim([-3,3])
plt.subplot(257)
plt.plot(np.abs(np.fft.fft(out45)),'r.:')
plt.ylim([0,100])

plt.subplot(253)
plt.plot(out90,'b.:')
plt.grid(linestyle = ':')
plt.title('90相位差')
plt.ylim([-3,3])
plt.subplot(258)
plt.plot(np.abs(np.fft.fft(out90)),'r.:')
plt.ylim([0,100])

plt.subplot(254)
plt.plot(out135,'b.:')
plt.grid(linestyle = ':')
plt.title('135相位差')
plt.ylim([-3,3])
plt.subplot(259)
plt.plot(np.abs(np.fft.fft(out135)),'r.:')
plt.ylim([0,100])

plt.subplot(255)
plt.plot(out180,'b.:')
plt.grid(linestyle = ':')
plt.title('180相位差')
plt.ylim([-3,3])
plt.subplot(2,5,10)
plt.plot(np.abs(np.fft.fft(out180)),'r.:')
plt.ylim([0,100])

plt.show()
  • 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

仿真结果

在这里插入图片描述

Chap3 理解IQ信号

I/Q信号,时域和频域的信号是什么样子的

python源代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt

# 解决中文显示问题
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False

t = np.linspace(0,1,200)
fb = 5
wb = 2*np.pi*fb
fc = 35
wc = 2*np.pi*fc

# I路信号/Q路信号/时域基带信号/载波信号/调制输出信号
i = np.sin(wb*t)
q = np.cos(wb*t)
b = i + q
c = np.cos(wc*t)
out = b * c

plt.figure()
plt.subplot(251)
plt.plot(i,'b.:')
plt.grid(linestyle = ':')
plt.title('I路信号')
plt.ylabel('时域')
plt.subplot(256)
plt.plot(np.abs(np.fft.fft(i))[0:100],'r.:')
plt.ylabel('频域')

plt.subplot(252)
plt.plot(q,'b.:')
plt.grid(linestyle = ':')
plt.title('Q路信号')
plt.subplot(257)
plt.plot(np.abs(np.fft.fft(q))[0:100],'r.:')

plt.subplot(253)
plt.plot(b,'b.:')
plt.grid(linestyle = ':')
plt.title('I+Q时域输出信号')
plt.subplot(258)
plt.plot(np.abs(np.fft.fft(b))[0:100],'r.:')

plt.subplot(254)
plt.plot(c[0:50],'b.:')
plt.grid(linestyle = ':')
plt.title('载波信号')
plt.subplot(259)
plt.plot(np.abs(np.fft.fft(c))[0:100],'r.:')

plt.subplot(255)
plt.plot(out[0:50],'b.:')
plt.grid(linestyle = ':')
plt.title('调制信号')
plt.subplot(2,5,10)
plt.plot(np.abs(np.fft.fft(out))[0:100],'r.:')

plt.show()
  • 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

仿真结果

在这里插入图片描述

Chap4 行驻波

行驻波,动态波形

python源代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt

# 解决中文显示问题
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False

t = np.linspace(0,100,10001)    # 时间变量
z = np.linspace(0,20,201)   # 空间变量

w = 2*np.pi
tran = 2    # 传播常数
re = 0.5    # 反射系数,0为行波,1为驻波


while(1):
    plt.ion()
    for i in range(len(t)):
        plt.clf()
        # y  = np.real(np.exp(1j*(w*t[i]+tran*z)) + re*np.exp(1j*(w*t[i]-tran*z)))
        a = np.exp(1j*(w*t[i]+tran*z))
        b = np.exp(1j*(w*t[i]-tran*z))
        y = np.real(a + re*b)
        y0 = np.real(a + 0*b)
        y1 = np.real(a + 1*b)
        plt.plot(z,y,'b.:',z,y0,'r.:',z,y1,'g.:')
        plt.grid(linestyle = ':')
        plt.ylim([-2,2])
        plt.title('蓝:行驻波,红:行波,绿:驻波')
        plt.draw()
        plt.pause(0.01)
    plt.ioff()
    plt.show()
  • 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

仿真结果

在这里插入图片描述

Chap5 RC串并联电路

RC文氏桥的谐振

电路

在这里插入图片描述若没有C2,正弦信号Uo的电流由C1→R1→R2,通过R2上压降形成Uf输出电压。由于支路电流被电容C1移相超前Uo 90°,这超前相位的电流流过R2(电阻不产生相移!),使得输出电压Uf电压超前于Uo 90°。
在R2上并联C2,C2从R2取得电压,由于电容对电压的滞后作用,使得R2上电压也被强制滞后。(但不一定有90°,因为还有C1→R1→C2电流对C2上电压即Uf的影响,但在RC特征频率上,并联C2后Uf输出相位与Uo相同。)

python源代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt
# from scipy.fftpack import fft

# 解决中文显示问题
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False

f = np.logspace(0,8,100)
w = 2*np.pi*f
R1 = 100
R2 = 100
C1 = 100E-9
C2 = 100E-9
y1 = R2/(1/(1j*w*C1)+R1+R2)
y2 = (R2/(1+1j*w*R2*C2))/(1/(1j*w*C1)+R1+(R2/(1+1j*w*R2*C2)))

plt.figure()
plt.subplot(121)
plt.plot(w,np.abs(y1),'b.-',w,np.abs(y2),'r.-')
plt.grid(linestyle = ':')
plt.xscale('log')
plt.title('幅频特性曲线')
plt.xlabel('频率 Hz')
plt.ylabel('幅度 %')
plt.yticks([1,0.7,0.5,0.3,0],['100', '70', '50', '30', '0'])
plt.subplot(122)
plt.plot(w,np.angle(y1)/(2*np.pi)*360,'b.-',w,np.angle(y2)/(2*np.pi)*360,'r.-')
plt.grid(linestyle = ':')
plt.xscale('log')
plt.title('相频特性曲线')
plt.xlabel('频率 Hz')
plt.ylabel('相位 度')

plt.show()
  • 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

仿真结果

在这里插入图片描述蓝色:无C2时的幅频/相频曲线
红色:有C2时的幅频/相频曲线