You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何绘制带延迟起始时间的正弦波?

带延迟起始的正弦波实现(CFD边界条件用)

需求回顾

  • 正弦波频率:1Hz
  • 总时长:2s
  • t<1s时信号为常数d=5
  • 1s≤t≤2s时为一个周期的正弦函数(从t=1s开始,仅显示一个周期)

修改后的代码

import numpy as np
import matplotlib.pyplot as plt

a = 1
d = 5
phi = 0

f = 1                 # 频率
dt = 0.01             # 时间步长
fs = 1/dt             # 采样率
T = 1/f               # 周期
Ttot = 2              # 总时长
N = int(Ttot/dt)      # 采样点数

t = np.linspace(0, Ttot, N)         # 时间序列

# 分区间生成信号
signal = np.where(t < 1, d, a*np.sin(2*np.pi*f*(t - 1) + phi) + d)

plt.plot(t, signal)
plt.xlim(0, Ttot)
plt.xlabel('Time / s')
plt.ylabel('Amplitude')
plt.grid(True)
plt.show()

关键修改说明

  1. 时间偏移处理:正弦波部分使用t - 1作为输入,确保t=1s时相位为phi=0,刚好从正弦波的起始点开始,符合"从t=1s处开始"的要求。
  2. 分段信号生成:用np.where()实现条件赋值,简洁高效地处理两个时间区间的信号:
    • 当t < 1时,信号值为常数d=5
    • 当t ≥ 1时,生成偏移后的正弦波并叠加直流分量d
  3. x轴范围调整:将plt.xlim(0, 2*T)改为plt.xlim(0, Ttot),确保显示完整的2s时长信号。

适配CFD边界条件的注意点

如果要将该函数集成到CFD仿真的边界条件中,只需保留核心的信号生成逻辑:

def boundary_signal(t, a=1, d=5, f=1, phi=0):
    if t < 1:
        return d
    elif 1 <= t <= 2:
        return a*np.sin(2*np.pi*f*(t - 1) + phi) + d
    else:
        # 若仿真时长超过2s,可根据需求补充逻辑,比如回到常数d或截断
        return d

内容的提问来源于stack exchange,提问作者Jonas1993

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 11:20:29