简体   繁体   English

如何在Python中绘制连续的正弦波?

[英]How to plot a continuous sine wave in Python?

I am trying to simulate the display of a sine wave one would generate from an oscilloscope using Python. 我试图模拟使用Python从示波器生成的正弦波的显示。 As I am trying to merely simulate it (and not pull the data from the oscilloscope), I was wondering how I would show a continuous sine wave. 当我试图仅仅模拟它(而不是从示波器中提取数据)时,我想知道如何显示连续的正弦波。 I have the sample rate of the device (200MHz - 1GS/s), the frequency of my wave (1MHz), and the amplitude (1V). 我有设备的采样率(200MHz - 1GS / s),我的波的频率(1MHz)和幅度(1V)。 The data would be viewed in microseconds. 数据将以微秒为单位进行查看。 I have read through various answers here on StackOverflow and have had problems with the plot having irregular waves or something of the sort. 我已经在StackOverflow上阅读了各种答案,并且遇到了有不规则波形或其他类似情节的问题。 Is there a way to have this data shown like below? 有没有办法让这些数据显示如下? 我希望模拟示波器数据输出,对于1MHz频率,100MS / s采样率,幅度为1V。

A secondary problem is the ability to plot this wave continuously. 第二个问题是能够连续绘制这个波。 For example, when using Matplotlib, if I zoom out it doesn't show the wave continuing past my interval. 例如,当使用Matplotlib时,如果我缩小它并不显示持续超过我的间隔的波。 Is there a way to have the wave continually represented? 有没有办法让波浪不断代表? I don't want to be tied down to Matplotlib, so I am looking for other solutions that continually creating (appending?) data in both directions. 我不想被束缚到Matplotlib,所以我正在寻找其他解决方案,这些解决方案在两个方向上不断创建(附加?)数据。 If this is not possible, is it possible to establish some sort of number of wavelengths in each direction? 如果这是不可能的,是否可以在每个方向上建立某种数量的波长?

Thank you so much! 非常感谢!

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()

You could use matplotlib.animation to achieve your goal. 您可以使用matplotlib.animation来实现您的目标。

I took an existing example which emulates an oscilloscope and adjusted it to your needs (eg sine wave & plotting continously). 我采用了一个模拟示波器现有示例,并根据您的需要进行调整(例如正弦波和连续绘图)。

Regarding the continous plotting: I set up a continous variable, where you can choose if you want to plot it continously (not able to zoom) or not (able to zoom). 关于连续绘图:我设置了一个continous变量,您可以选择是否要连续绘制(无法缩放)或不绘制(能够缩放)。 I wasn't able to combine both functionalities in one plot yet. 我还没能在一个情节中结合两种功能。 So just run the code once with continous = True and once with continous = False to see if it suits your needs. 因此,只需使用continous = True运行一次代码,使用continous = False运行一次,看看它是否符合您的需求。

But I think this could be a good start for plotting continous sine waves. 但我认为这可能是绘制连续正弦波的良好开端。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM