AnyMath 文档
Notebook

不稳定多谐振荡器的仿真

本示例将演示不稳定多谐振荡器的仿真。

不稳定多谐振荡器是一种能够产生连续方波脉冲的自振荡电路。它没有稳态,会在两个晶体管之间自动切换,从而产生周期性信号。

多谐振荡器可用于在各种系统中生成时钟信号。

数学描述

脉冲和间歇的持续时间由晶体管基极电路中的RC电路决定。

每个晶体管的“导通”状态持续时间:

总振荡周期(对称电路情况下):

信号频率:

模型电路图:

НОВАТОР1.2--1741865268001.png

定义用于加载和运行模型的函数:

In [ ]:
function start_model_engee()
    try
        engee.close("npn_transistor", force=true) # 模型关闭
        catch err # 如果没有需要关闭的模型,且 engee.close() 未被调用,则会在 catch 块之后执行该模型的加载操作
            m = engee.load("$(@__DIR__)/astable_multivibrator.engee") # 加载模型
        end;

    try
        engee.run(m, verbose=true) # 运行模型
        catch err # 如果模型未加载且未执行 engee.run(),则会在 catch 块之后执行最后两行代码
            m = engee.load("$(@__DIR__)/astable_multivibrator.engee") # 加载模型
            engee.run(m, verbose=true) # 运行模型
        end
end
Out[0]:
start_model_engee (generic function with 1 method)

运行仿真

In [ ]:
start_model_engee();
Building...
Progress 0%
Progress 5%
Progress 10%
Progress 15%
Progress 20%
Progress 26%
Progress 31%
Progress 36%
Progress 41%
Progress 46%
Progress 51%
Progress 56%
Progress 62%
Progress 67%
Progress 72%
Progress 78%
Progress 83%
Progress 89%
Progress 94%
Progress 100%
Progress 100%

将仿真数据写入变量:

In [ ]:
t = simout["astable_multivibrator/右侧"].time[:]
V_right = simout["astable_multivibrator/右侧"].value[:]
V_left = simout["astable_multivibrator/左侧"].value[:]
I_R1 = simout["Resistor 1.i"].value[:]
I_R4 = simout["Resistor 4.i"].value[:]
Out[0]:
WorkspaceArray{Float64}("Resistor 4.i").value

数据可视化

In [ ]:
using Plots

可视化两个晶体管的集电极-发射极电压:

In [ ]:
plot(t, V_left, linewidth=2)
plot!(t, V_right, linewidth=2)
Out[0]:

可视化连接到晶体管集电极的电阻上的电流:

In [ ]:
plot(t, I_R1, linewidth=2)
plot!(t, I_R4, linewidth=2)
Out[0]:

处理仿真结果:

启动统计库:

In [ ]:
using Statistics

定义用于计算电压振荡频率和周期的函数:

In [ ]:
function calculate_oscillation_properties(time::Vector{T}, x::Vector{T}) where T <: Real
    # 向量长度的验证
    if length(time) != length(x)
        error("时间和信号向量必须具有相同的长度")
    end
    n = length(time)
    n < 2 && return (NaN, NaN)  # 数据不足

    # 求信号的平均值
    avg = sum(x) / n

    # 寻找均值自下而上被穿透的时刻
    up_crossings = T[]
    for i in 2:n
        if x[i-1] < avg && x[i] >= avg
            # 用于精确确定交叉时间的线性插值
            t = time[i-1] + (time[i] - time[i-1]) * (avg - x[i-1]) / (x[i] - x[i-1])
            push!(up_crossings, t)
        end
    end

    # 检查交点数量是否充足
    length(up_crossings) < 2 && return (NaN, NaN)

    # 周期的计算(以时间单位为单位)
    periods = diff(up_crossings)
    mean_period = mean(periods)
    frequency = 1 / mean_period

    return "振动频率:$frequency 赫兹。振动周期:$mean_period 秒。"
end
Out[0]:
calculate_oscillation_properties (generic function with 1 method)

将该函数应用于电压值向量(可代入任意信号):

In [ ]:
calculate_oscillation_properties(collect(t)[:,1], collect(V_left)[:,1])
Out[0]:
"振动频率:461.82838941765783 赫兹。振动周期:0.0021653064707887475 秒。"

结论:

在本例中,我们探讨了一个不稳定多谐振荡器的模型,其中决定振荡频率和周期的关键元件是基极电阻(R2、R3)以及构成定时RC电路的电容器。 集电极电阻(R1、R4)对信号频率没有影响——它们的作用仅限于限制流过晶体管的电流并确定输出电压的幅值。