我想画一个延迟启动时间的正弦波。
例如,
我希望正弦波从t=1s开始,所以在我的情节中只有一个周期。
到目前为止我的代码是
a = 1
d = 5
phi = 0
f = 1 # frequency
dt = 0.01 # timestep
fs = 1/dt # sampling rate
T = 1/f # period
Ttot = 2 # total
N = int(Ttot/dt) # number of samples
t = np.linspace(0, Ttot, N) # time channel
signal = a*np.sin(2*np.pi*f*t + phi)+d # sine signal
plt.plot(t, signal)
plt.xlim(0, 2*T)
plt.xlabel('Time / s')
plt.ylabel('Amplitude')
plt.show()所以在我想要的情节中,t<1s,signal=d=const和t>1s,signal = one period of sine function。
有什么想法吗?在CFD模拟中,我必须建立一个边界条件的场函数。
发布于 2022-11-22 18:40:52
首先,对于正弦的延迟,您必须修改这一行。
t = np.linspace(0, Ttot, N)为此
t = np.linspace(1, Ttot, N)这将为计算信号提供样本,介于1和Ttot之间(此处为2)。
然后,可以在0和延迟之间添加一条水平线( d ),如下所示
plt.hlines(d, 0, 1)例如,当添加一个名为delay的变量时,整个代码如下所示
import matplotlib.pyplot as plt
import numpy as np
a = 1
d = 5
phi = 0
f = 1 # frequency
dt = 0.01 # timestep
fs = 1/dt # sampling rate
T = 1/f # period
Ttot = 2 # total
N = int(Ttot/dt) # number of samples
delay = 1
t = np.linspace(delay, Ttot, N) # time channel
signal = a*np.sin(2*np.pi*f*t + phi)+d # sine signal
plt.plot(t, signal)
plt.hlines(d, 0, delay)
plt.xlim(0, 2*T)
plt.xlabel('Time / s')
plt.ylabel('Amplitude')
plt.show()给出了下面的图

https://stackoverflow.com/questions/74536008
复制相似问题