如何绘制带延迟起始时间的正弦波?
带延迟起始的正弦波实现(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()
关键修改说明
- 时间偏移处理:正弦波部分使用
t - 1作为输入,确保t=1s时相位为phi=0,刚好从正弦波的起始点开始,符合"从t=1s处开始"的要求。 - 分段信号生成:用
np.where()实现条件赋值,简洁高效地处理两个时间区间的信号:- 当
t < 1时,信号值为常数d=5 - 当
t ≥ 1时,生成偏移后的正弦波并叠加直流分量d
- 当
- 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
相关产品推荐
相关产品推荐

