Skip to content

Instantly share code, notes, and snippets.

@standarddeviant
Created January 3, 2020 15:37
Show Gist options
  • Save standarddeviant/154a0fccbe407a7b06360c5f45fa99c0 to your computer and use it in GitHub Desktop.
Save standarddeviant/154a0fccbe407a7b06360c5f45fa99c0 to your computer and use it in GitHub Desktop.
Simple script to calculate the error of polling multiple sample rates with Python `time.sleep`
import time
import numpy as np
fs_list = [120.0, 2.0]
dt_list = [1/fs for fs in fs_list]
prevt_list = [time.time(), ] * len(fs_list)
sample_times = [list() for fs in fs_list]
timeout_seconds = 10
sleep_seconds = min(dt_list) * 0.1
tstart = time.time()
while True:
tnow = time.time()
# this could be more efficient by removing the for loop and using
# numpy for element-wise operations on an array instead of a list
for cix in range(len(fs_list)):
if tnow - prevt_list[cix] > dt_list[cix]:
# FIXME: do something to trigger sampling on channel 'cix'
# update timing parameter
prevt_list[cix] += dt_list[cix]
sample_times[cix].append(tnow)
# end per channel timing check since that channel's last sample
if tnow - tstart > timeout_seconds:
break
time.sleep(sleep_seconds)
# end while True
print("two_timing.py : fs_list = {}".format(fs_list))
print("two_timing.py : dt_list = {}".format(dt_list))
print("two_timing.py : sleep_seconds = {}".format(sleep_seconds))
for cix, fs in enumerate(fs_list):
dts = np.diff(sample_times[cix])
print(" -> Channel = {}".format(cix))
print(" ...... expected dt = {}".format(dt_list[cix]))
print(" ...... mean of dt = {}".format(np.mean(dts)))
print(" ...... std of dt = {}".format(np.std(dts)))
print(" ...... std / mean of dt = {}".format(np.std(dts) / np.mean(dts)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment