简体   繁体   English

如何生成不规则的时间间隔?

[英]How to generate irregular time intervals?

I intend to make a simulation of real-data ie produce data at irregular time intervals. 我打算对真实数据进行仿真,即以不规则的时间间隔生成数据。 Below is some prototype code I wrote in python to simulate irregular time(not data), but the results are that my loop runs too fast such that each "time"(dat) produced is produced at the same time stamp. 以下是我在python中编写的一些原型代码,用于模拟不规则时间(不是数据),但结果是我的循环运行得太快,以至于产生的每个“时间”(dat)都在同一时间戳下产生。 On top of this code, I intend to feed data set and pass in the data at these irregular time stamps. 在此代码之上,我打算提供数据集并在这些不规则的时间戳处传递数据。

import time,random

Tadd=0.1
start=time.time()
while time.time()<(start+Tadd):
     x=random.uniform(0,1)
     if x<0.5:
         dat=time.time()
         print dat
     else:
         pass

The output is something was this. 输出就是这样。

1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95
1482896418.95

So my question is: 所以我的问题是:

  1. Is it viable or "realistic" for me to code up this way? 以这种方式编写代码对我来说是可行的还是“现实的”? Or am I better off reading a text file line by line. 还是我最好逐行阅读文本文件。 eg read the 1st line process, read the 2nd line process, .... My intention is actually to read sensor data for robot localisation. 例如,读取第一行过程,读取第二行过程,...。我的意图实际上是读取传感器数据以进行机器人定位。 But, which is the best simulation. 但是,这是最好的模拟。 I was thinking that perhaps I can use an event to produce data from my mouse. 我当时在想,也许我可以使用一个事件从鼠标中产生数据。 Can someone provide insight and suggestions on how I can go about this? 有人可以提供我该如何做的见解和建议吗? Or am I better off just diving straight into the practical setting from a simulated setting and buy an ultrasound sensor. 或者我最好是直接从模拟设置中跳入实际设置并购买超声传感器。

  2. Or if I am on the right track, and this is a good enough simulation. 或者,如果我走在正确的轨道上,那么这已经足够好了。 How do I make my time intervals ,in this case, less sensitive so that each "time"(dat) is distinct from the other. 在这种情况下,如何使时间间隔不太敏感,以使每个“时间”(dat)彼此不同。

You simply produce a random number between and add it to the current time. 您只需在之间产生一个随机数,并将其添加到当前时间。 This is assuming you don't need time in the past only. 这是假设您不需要过去的时间。

Edit: Now the code will generate timestamps in increasing order, separated by an average of one second. 编辑:现在,代码将以递增顺序生成时间戳,平均间隔为一秒。

import time,random

Tadd=0.1
start=time.time()
init = 0
while time.time()<(start+Tadd):
     x=random.uniform(init,init+1)
     dat=time.time()+x
     init+=1;
     print dat

print start, start+Tadd

It shouldn't be hard to simulate data arriving at irregular intervals. 模拟不规则间隔的数据应该不难。 However, you aren't adding in any delays between data points (apart from code execution time), so you probably want to explicitly add them. 但是,您没有在数据点之间添加任何延迟(除了代码执行时间之外),因此您可能希望显式添加它们。 For example this code will run for 5 seconds and print the time at random intervals. 例如,此代码将运行5秒钟,并以随机间隔打印时间。 It shouldn't be hard to tweak this to match your data source. 对其进行微调以匹配您的数据源应该不难。

import time
import random

run_time = 5
start = time.time()
now = time.time()
while now < start + run_time:
    print now
    time.sleep(random.random())
    now = time.time()

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

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