python - Looping at a constant rate with high precision for signal sampling -
python - Looping at a constant rate with high precision for signal sampling -
i trying sample signal @ 10khz in python. there no problem when seek run code(at 1khz):
import sched, time = 0 def f(): # sampling function s.enter(0.001, 1, f, ()) global += 1 if == 1000: = 0 print "one second" s = sched.scheduler(time.time, time.sleep) s.enter(0.001, 1, f, ()) s.run()
when seek create time less, starts exceed 1 second(in computer, 1.66s @ 10e-6). it possible run sampling function @ specific frequency in python?
you didn't business relationship code's overhead. each iteration, error adds , skews "clock".
i'd suggest utilize loop time.sleep()
instead (see comments http://stackoverflow.com/a/14813874/648265) , count time sleep next reference moment inevitable error doesn't add together up:
period=0.001 t=time.time() while true: t+=period <...> time.sleep(max(0,t-time.time())) #max needed in windows due #sleep's behaviour negative argument
note os scheduling not allow reach precisions beyond level since other processes have preempt yours time time. in case, you'll need utilize os-specific facilities multimedia applications or work out solution doesn't need level of accuracy.
python sampling
Comments
Post a Comment