pause function gives inconsistent timing when used inside a loop.

조회 수: 13 (최근 30일)
Milad Omidi
Milad Omidi 2019년 8월 9일
편집: Adam Danz 2020년 8월 26일
Hello,
I am trying to create an animated plot. The pause command inside an infinite loop is supposed to created the timing for me:
%%
figure; clf;
t = 0:1/16000:1;
y = sin(2*pi*4*t);
h = plot(t,y);
xlim([ 0 t(end)]);
ylim([ -1 1]);
h.XDataSource = 't';
h.YDataSource = 'y';
rate = 10;
while 1
tic
pause(1/rate);
%java.lang.Thread.sleep(1000 * (1/rate))
toc
y = sin(2*pi*freq*t);
refreshdata(h);
drawnow % needed?
freq = freq + 0.1;
if freq > 40
freq = 4;
end
end
When I try this tic toc shows some inconsistent timing:
Elapsed time is 0.100041 seconds.
Elapsed time is 0.599817 seconds.
Elapsed time is 0.100195 seconds.
Elapsed time is 0.100100 seconds.
Elapsed time is 0.499797 seconds.
Elapsed time is 0.100046 seconds.
If I remove everything from the loop and leave just the pause command it behaves the same.
I tried using java's sleep command but that does not allow the plots to be updated until the loop is exited which is useless.
Update:
The results shown above happens whn I run this on Linux. I tried running the same code on Windows and it works fine. Not sure what could be the reason.

채택된 답변

Adam Danz
Adam Danz 2020년 8월 25일
편집: Adam Danz 2020년 8월 26일
Careful using tic/toc. As summarized in the documentation, "If your code is faster than 1/10 second, consider measuring it running in a loop, and then average to find the time for a single run". Even with larger durations, there is unavoidable variability (shown in this comment within this thread).
Alternatively, use timeit to determine how long a process takes (not to control real-time processes). Unlike tic and toc, the timeit function calls your code multiple times, and, therefore, considers first-time costs.
Check out this comparison of timing pause(0.1) using toc/toc and timeit. Notice the difference in variability. For a bit more info on tic/toc timing within a loop, see this answer.
Additionally, the pause() function is susceptible to variations due to your OS and concurrent system activity. The finer the temporal resolution, the higher the relative error. For more info on limitations to the pause function, scroll down to the tips section in this link.
f = @()waitasec;
t = zeros(1,100);
for i = 1:100
t(i) = timeit(f);
end
t2 = zeros(1,100);
for i = 1:100
tic
pause(0.1)
t2(i) = toc;
end
clf
hold on
h = plot(t, 'DisplayName', 'timeit')
h2 = plot(t2, 'DisplayName', 'tictoc')
legend()
function waitasec
pause(0.1)
end
If you're trying to control the speed of animation, consider using one of the animation techniques, some of which provide examples of controlling animation speed. This blog post uses a timer object to control animation speed. I have not read or vetted the article but keep in mind that Matlab recommends not using a timer for real-time applications.

추가 답변 (1개)

jmore
jmore 2020년 8월 26일
편집: jmore 2020년 8월 26일
I have encountered a similar issue on my Linux operating system. Mr. Adam Danz has suggested using timeit, the the following approach is a little easier to understand for me:
rate = 10
tic
while toc < 1/rate
end
toc
This will replace to faulty pause command that varies from 0.1 to 0.5 seconds:
tic
pause(1/rate)
toc
  댓글 수: 4
Bruno Luong
Bruno Luong 2020년 8월 26일
@Adam: Why there is an horizontal dispersion in the plot?I mean in your code the xaxis is plotted with 1:numel(rate) yet I see the points are dispersed horizontally. Odd.
Adam Danz
Adam Danz 2020년 8월 26일
I must have added that after copying the code to the comment. The comment states, "Results are jittered along the x axis to prevent overlap ", otherwise many of the points would lay on top of eachother.
To jitter the points,
xJitter = (1:numel(rate))' + (rand(numel(rate),nReps)-.5)/2;
plot(xJitter, dur, 'bo', 'DisplayName', 'all iterations')
I'll fix that in my comment. Thanks for pointing that out.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by