How to increase plot frequency of gnuplot with c++ real-time data?

时间:2019-01-07 14:06:38

标签: c++ gnuplot real-time iostream

I'm trying to visualize sensor data in real time using C++. The sensor has an output of up to 1kHz, but gnuplot is only plotting the data at about 10Hz.

I'm using gnuplot-iostream (http://stahlke.org/dan/gnuplot-iostream/) to pipe the data to gnuplot from my C++ script, which is simple and easy. But it seems like the plotting process is slow, and takes 1/10th of a second to update the plot. Is there any way of increasing this frequency?

EDIT: Here's an example of a simple code

#include <vector>
#include <utility>
#include <gnuplot-iostream/gnuplot-iostream.h>

typedef std::pair<double, double> Point;

int main() {
  std::vector<Point> data;

  double x = 0.0;
  double y = 0.0;
  double c = 0.0;

  Gnuplot gp;
  gp << "set terminal wxt size 800, 400\n";

  while (x < 10000) {
    x += 0.01;
    y = sin(x);
    c += 0.01;
    data.push_back(Point(x,y));
    //std::cout <<  x << std::endl;
    if (c > 0.1) {
      gp << "plot '-' with lines title 'sin(x)'\n";
      gp.send1d(data);
      c = 0.0;
    }
  }

  return 0;
}

1 个答案:

答案 0 :(得分:6)

如果传感器以1 kHz的采样率输出数据,那绝对不意味着您应该以该频率进行绘制。太疯狂了!如果您的眼睛看不到以该频率绘制数据,那么绘制数据的重点是什么?

您应该像每隔0.1秒一样对要绘制的点进行分组,然后将它们与所有数据一起绘制。要清楚:

  1. 收集一些数据,将其放入要绘制的数组中
  2. 绘制数组的数据
  3. 在0.1秒(或0.2或0.5,或也许每100个样本中收集更多数据;这是您的要求)
  4. 将其添加到要绘制的数据数组中
  5. 可选:如果数组太大,则从前面修剪数据
  6. 绘制数据
  7. 回到3