【问题标题】:How can I generate a square wave plot of a pulse train of multiple signals from the data in a csv file (in Linux)?如何从 csv 文件(在 Linux 中)中的数据生成多个信号脉冲序列的方波图?
【发布时间】:2020-05-23 18:37:22
【问题描述】:

例如,给定文本文件中的数据:

10:37:18.459 1     
10:37:18.659 0     
10:37:19.559 1 

这怎么能显示为一个看起来像方波的图像,可以正确表示高时间和低时间?我正在尝试 gnuplot 和 scipy。结果最终应该包括多个传感器,并且所有图都必须相互叠加以显示时间增量。

以下链接中的代码根据列出的公式创建方波, link to waveforms。如果它们在文件中(显示 200 ms 的高状态,然后显示 100 ms 的低状态,最后显示高状态),如何由上面的数字驱动较低的波形 (pwm)?

【问题讨论】:

  • 欢迎来到 SO!我认为您可以通过minimal reproducible example 提高获得答案的机会
  • 你能解释一下数据代表什么吗?那么 10:37:18.469 1 究竟代表什么?

标签: python-2.7 numpy matplotlib scipy gnuplot


【解决方案1】:

如果我正确理解了您的问题,您想根据时间数据绘制一个阶跃函数。为避免进一步猜测,请详细说明。 在 gnuplot 中有绘图风格with steps。检查help steps。

代码:

### display waveform as steps
reset sesion

$Data <<EOD
10:37:18.459 1
10:37:18.659 0
10:37:19.559 1 
10:37:19.789 0 
10:37:20.123 1 
10:37:20.456 0 
10:37:20.789 1 
EOD

set yrange [-0.05:1.2]
myTimeFmt = "%H:%M:%S"       # input time format
set format x "%M:%.1S" time  # output time format on x axis

plot $Data u (timecolumn(1,myTimeFmt)):2 w steps lc rgb "red" lw 2 ti "my square wave"

### end of code

结果:

【讨论】:

  • 感谢这个。我得试一试。我还复制了我最终使用 matplotlib 完成的内容。
【解决方案2】:

我最终得到的答案是:

    file_info = os.stat( self.__outfile)
    if file_info.st_size:
        x,y,z,a = np.genfromtxt( self.__outfile, delimiter=',',unpack=True )
        fig = plt.figure(self.__outfile)
        ax = fig.add_subplot(111)
        fig.canvas.draw()

        test_array = [(datetime.datetime.utcfromtimestamp(e2).strftime('%d_%H:%M:%S.%f')).rstrip('0') for e2 in x]
        plt.xticks(x, test_array)
        l1, = plt.plot(x,y, drawstyle='steps-post')
        l2, = plt.plot(x,a-2, drawstyle='steps-post')
        l3, = plt.plot(x,z-4, drawstyle='steps-post')

        ax.grid()
        ax.set_xlabel('Time (s)')
        ax.set_ylabel('HIGH/LOW')
        ax.set_ylim((-6.5,1.5))
        ax.set_title('Sensor Sequence')
        fig.autofmt_xdate()
        ax.legend([l1,l2, l3],['sprinkler','lights', 'alarm'], loc='lower left')
        plt.show()

我有一个输入文件,其中包含 convertDateToFloat 值。那是传递给这个函数的。该名称可能具有误导性(__outfile),但在前面的函数中,它是输出。

【讨论】:

  • 我的源文件看起来像:1577937845.307,0,0,0 1577965463.534,1,0,0 1577965463.735,1,1,0 1577965463.737,1,1,0 1577965463.835,1,1,1 1577965463.835,1,0,1 1577965465.134,1,1,1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-20
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
相关资源
最近更新 更多