【问题标题】:How to plot a continuous sine wave in Python?如何在 Python 中绘制连续的正弦波?
【发布时间】:2018-04-11 15:22:38
【问题描述】:

我正在尝试模拟使用 Python 从示波器生成的正弦波的显示。当我试图仅仅模拟它(而不是从示波器中提取数据)时,我想知道如何显示一个连续的正弦波。我有设备的采样率 (200MHz - 1GS/s)、我的波的频率 (1MHz) 和幅度 (1V)。数据将在微秒内查看。我已经阅读了 StackOverflow 上的各种答案,并且在情节中遇到了不规则波浪或类似问题的问题。有没有办法让这些数据显示如下?

次要问题是连续绘制此波的能力。例如,当使用 Matplotlib 时,如果我缩小它不会显示波浪继续超过我​​的间隔。有没有办法让波浪不断地表现出来?我不想被 Matplotlib 束缚,所以我正在寻找其他解决方案,可以在两个方向上不断创建(附加?)数据。如果这不可能,是否可以在每个方向上建立某种数量的波长?

非常感谢!

import numpy as np
from scipy import signal
import matplotlib.pyplot as plt

Fs = 20E3
f = 1E6
sample = Fs/f
print(sample)
x = np.arange(sample)

y = 100*np.sin(2 * np.pi * f * x / Fs)

plt.plot(x, y)
plt.show()

【问题讨论】:

  • 你能展示你的代码吗?
  • 您使用的是 Windows 还是 Linux?
  • 你想要一张图片还是动态显示?
  • @Imas 没关系,提供代码(即使它没有按照您的意愿执行),允许其他用户更轻松地开发适合您在以你能理解的方式,它也表明你在来 SO 之前已经对这个问题进行了一些思考和研究
  • 鉴于np.arange(20e3/1e6) 将返回array([ 0. ]),您上面的代码不会绘制任何内容。看起来您希望 f 是您的波频率,而 Fs 是您的采样频率。为什么你的采样频率低于你的波频率?如果你这样做,你的波形看起来不会很好。

标签: python python-3.x matplotlib waveform


【解决方案1】:

您可以使用matplotlib.animation 来实现您的目标。

我使用了existing example which emulates an oscilloscope 并根据您的需要对其进行了调整(例如,正弦波和连续绘图)。

关于连续绘图:我设置了一个continous 变量,您可以在其中选择是否要连续绘制(不能缩放)或不(可以缩放)。我还不能在一个情节中结合这两种功能。因此,只需使用continous = True 运行一次代码,然后使用continous = False 运行一次代码,看看它是否适合您的需求。

但我认为这可能是绘制连续正弦波的良好开端。

import numpy as np
from matplotlib.lines import Line2D
import matplotlib.pyplot as plt
import matplotlib.animation as animation

# Your Parameters
amp = 1         # 1V        (Amplitude)
f = 1000        # 1kHz      (Frequency)
fs = 200000     # 200kHz    (Sample Rate)
T = 1/f
Ts = 1/fs

# Select if you want to display the sine as a continous wave
#  True = Continous (not able to zoom in x-direction)
#  False = Non-Continous  (able to zoom)
continous  = True

x = np.arange(fs)
y = [ amp*np.sin(2*np.pi*f * (i/fs)) for i in x]


class Scope(object):
    def __init__(self, ax, maxt=2*T, dt=Ts):
        self.ax = ax
        self.dt = dt
        self.maxt = maxt
        self.tdata = [0]
        self.ydata = [0]
        self.line = Line2D(self.tdata, self.ydata)
        self.ax.add_line(self.line)
        self.ax.set_ylim(-amp, amp)
        self.ax.set_xlim(0, self.maxt)

    def update(self, y):
        lastt = self.tdata[-1]
        if continous :
            if lastt > self.tdata[0] + self.maxt:
                self.ax.set_xlim(lastt-self.maxt, lastt)

        t = self.tdata[-1] + self.dt
        self.tdata.append(t)
        self.ydata.append(y)
        self.line.set_data(self.tdata, self.ydata)
        return self.line,


def sineEmitter():
    for i in x:
        yield y[i]


fig, ax = plt.subplots()
scope = Scope(ax)

# pass a generator in "sineEmitter" to produce data for the update func
ani = animation.FuncAnimation(fig, scope.update, sineEmitter, interval=10,
                              blit=True)

plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多