使用 UliEngineering 在 Python 中轻松生成正弦/余弦波形数据
为了在 Python 中生成正弦测试数据,你可以使用 UliEngineering 库,它在 UliEngineering.SignalProcessing.Simulation 中提供了易于使用的函数:
首先,安装 UliEngineering。
基本示例
sine_cosine_example.py
from UliEngineering.SignalProcessing.Simulation import sine_wave
# 默认:生成 1 秒的数据,振幅 = 1.0(从 -1.0 ... 1.0 摆动)
sine = sine_wave(frequency=10.0, samplerate=10e3)
cosine = cosine_wave(frequency=10.0, samplerate=10e3)振幅和偏移
使用 amplitude=0.5 指定正弦波应在 -0.5 和 0.5 之间摆动。
使用 offset=2.0 指定正弦波应垂直居中于 2.0:
sine_with_offset.py
from UliEngineering.SignalProcessing.Simulation import sine_wave
data = sine_wave(frequency=10.0, samplerate=10e3, amplitude=0.5, offset=2.0)相移示例
你可以指定要使用的相移 - 要使用 180° 相移,只需使用 phaseshift=180.0
phaseshift_example.py
from UliEngineering.SignalProcessing.Simulation import sine_wave
original = sine_wave(frequency=10.0, samplerate=10e3)
shifted = sine_wave(frequency=10.0, samplerate=10e3, phaseshift=180.)时间延迟示例
虽然这在功能上等同于相位偏移,但通常以秒为单位指定时间延迟比以度为单位指定相移更方便:
timedelay_example.py
from UliEngineering.SignalProcessing.Simulation import sine_wave
original = sine_wave(frequency=10.0, samplerate=10e3)
# 偏移信号与原始信号相比延迟了 0.01s = 10 毫秒
shifted = sine_wave(frequency=10.0, samplerate=10e3, timedelay=0.01)注意在时域中,当你使用正的 timedelay 值时,信号似乎向后偏移。这符合延迟命名,意味着信号被延迟了该量。
你可以同时指定相移和时间延迟,这意味着两者都将被应用(偏移被添加)
绘图
如果你想可视化调试你的信号,这是在 Jupyter 中用于生成上面显示的图的代码:
plot_timedelay_jupyter.py
%matplotlib inline
from matplotlib import pyplot as plt
plt.style.use("ggplot")
# 生成数据
from UliEngineering.SignalProcessing.Simulation import sine_wave
original = sine_wave(frequency=10.0, samplerate=10e3)
shifted = sine_wave(frequency=10.0, samplerate=10e3, timedelay=0.01)
# set_size_inches(20, 10) 使其更大!
plt.gcf().set_size_inches(10, 5)
plt.plot(original, label="original")
plt.plot(shifted, label="shifted")
plt.savefig("/dev/shm/timedelay.svg")
plt.legend(loc=1) # 右上角Check out similar posts by category:
Data Science, Mathematics, Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow