python、绘图和 c api
我有一个小 C 程序,它不断获取数据流并通过 UDP 实时发送到另一台计算机。我最初要做的事情的基本框架已经奠定了。然而,此外,我还想实时可视化正在获取的数据。为此,我正在考虑使用 Python 及其各种绘图库。我的问题是让 Python 访问我的 C 程序的先进先出循环缓冲区会有多困难。为了具体起见,我们假设该缓冲区中有 1024 个样本。 “让 Python 持续查看动态 C 数组”的想法听起来合理/可能吗?如果不是,哪种绘图选项最适合这个问题?
谢谢。
I have a little C program that's continuously acquiring a stream of data and sending it via UDP, and in real time, to a different computer. The basic framework for what I originally set out to do has been laid. In addition, however, I'd like to visualize in real time the data that's being acquired. To that end, I was thinking of using Python and its various plotting libraries. My question is how difficult it would be to let Python have access to what is essentially a first in, first out circular buffer of my C program. For concreteness, let's assume there are 1024 samples in this buffer. Does the idea of "letting Python have a continuous peek at dynamic C array" even sound reasonable/possible? If not, what sort of plotting options are best suited to this problem?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用标准 socket 模块轻松监听 UDP 端口。 示例可用。
第一步,您的数据可以放入一个简单的 Python 列表中,因为列表针对附加数据进行了优化。删除第一个元素需要更多时间,因此您可能只想偶尔执行此操作,并且同时仅绘制列表的最后 1024 个(或其他)元素。
然后可以使用著名的 Matplotlib 绘图库方便地完成绘图:
matplotlib.pyplot.plot(data_list)< /代码>。由于您想要实时,您可能会发现动画示例很有用。
如果您需要优化数据采集速度,您可以让(也很著名)NumPy 数组操作库直接解释将流中的数据作为数字数组(Matplotlib 可以绘制此类数组),使用 numpy.frombuffer() 函数。
You can quite easily listen to your UDP port with the standard socket module. Examples are available.
As a first step, your data could go in a simple Python list, as lists are optimized for appending data. Removing the first elements takes much more time, so you might want to only do this from time to time, and only plot, in the mean time, the last 1024 (or whatever) elements of the list.
Plotting can then conveniently be done with the famous Matplotlib plotting library:
matplotlib.pyplot.plot(data_list)
. Since you want real time, you might find the animation examples useful.If you need to optimize the data acquisition speed, you can have the (also famous) NumPy array-manipulation library directly interpret the data from the stream as an array of numbers (Matplotlib can plot such arrays), with the
numpy.frombuffer()
function.这是可能的,但不太简单。
您应该了解一下 API,也许还可以看看一些实现。
如果您已经这样做了,您也许可以提供一个函数,它不仅可以让您查看原始数组,甚至可以将其重新组装成正确的顺序和长度(如果它是循环缓冲区)。这可能非常方便,因为您仍然必须复制数据。
It is possible, but not too simple.
You should inform yourself about the API and maybe have a look at some implementations.
If you have done so, you can maybe provide a function which not only gives you a peek at the raw array, but maybe even reassembles it into the right order and length (if it is a circular buffer). This might be very convenient as you nevertheless have to copy the data.