0

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. 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.

Justin Thong
  • 321
  • 3
  • 12
  • Irregular on what time scale? Minutes, Seconds, milliseconds, microseconds etc.. – wander95 Dec 28 '16 at 04:27
  • @wander95 I am not particularly sure but I would think microsecond. – Justin Thong Dec 28 '16 at 05:33
  • Use time.clock(), sometime need using float application clock for capturing non symmetrical patterns. Hardware only work <1ms . For micro or nanoseconds need using a micro controller. Read + Calculate and push data with in period. You chosen hard-way because external clock never returned real value, maybe data is correct but you grab data with latency. Can measure anything if have large resolution, samples not real value. An bug: How to calculate ECHO when changed medium/medium-patterns (echo is SUB-ECHO meaning Harmonic)? – dsgdfg Dec 28 '16 at 06:10
  • Do you want to run the code for 0.1 seconds or do you want to generate till timestamps smaller than start+0.1 seconds? – Mangat Rai Modi Dec 28 '16 at 09:55

2 Answers2

1

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
Mangat Rai Modi
  • 5,397
  • 8
  • 45
  • 75
0

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. 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()
101
  • 8,514
  • 6
  • 43
  • 69