Optimizing the 'drawnow' function for an oscillating circle

3 次查看(过去 30 天)
Hello,
I am using Matlab 7.10.0 to create an oscillating circle that moves horizontally across the screen. However the quality of the circle is quite poor probably due to the use of the 'drawnow' function being used to re-draw the circle as quickly as possible for every new position.
Is there any way that I would be able to optimize the draw function so that I can have a clean visual of the oscillating circle. Also would a computer with a higher processing speed and improved graphics card as well as a screen with a higher refresh rate improve the quality of the visual?
Here is a sample of the code that I am using. The code is set up as a function where I can call the specific type of visual that I want in this case it is a oscillation:
% --OSCILLATION SETUP
f = 0.5; % oscillation frequency Hz
A = 2.5; % oscillation amplitude
tmax = 10; % max time
t = 0:0.0001:tmax; % time array
x = A*sin(2*pi*f*t); % circle position array
% --VISUAL SETUP r = 1; % radius N = 100; % number of points on the cirle circle_colour = [0 0 0]; % circle colour RGB
% play visual
if strcmp(visual_options,'oscillate')
% plot moving circle
while toc < tmax
current_time = toc;
ind = find(t>toc, 1, 'first');
circle([x(ind) 0],r,N,circle_colour);
axis([-xlim xlim -ylim ylim])
axis off
drawnow()
end
Many thanks in advance,
Alan

采纳的回答

Daniel Shub
Daniel Shub 2012-5-24
To get the graphics really good you need to access the graphics card directly. Toolboxes like MGL and PsychToolbox help you do that.
  2 个评论
Alan Armstrong
Alan Armstrong 2012-5-25
Thanks! I've decided to use PsychToolbox for creating my visuals it's a much easier program to work with and the quality of the visuals are much better than what I can achieve using figures.

请先登录,再进行评论。

更多回答(3 个)

owr
owr 2012-5-24
What are you doing in your function "circle"?
If you are calling a function like "plot" over and over again, that is creating alot more overhead than you need. You should be able to call it once, grab a handle to the graphics object and just update the 'xdata' and 'ydata' properties. You shouldnt need to call "drawnow" or "axis" within the loop.
Something like this:
theta = 0:pi/20:2*pi;
x = cos(theta);
y = sin(theta);
h = plot(x,y);
axis([-1,7,-4,4]);
for i = 1:100
x = x + 0.05;
set(h,'xdata',x);
pause(0.1);
end
I used a for-loop and a pause state,emt, but agree with the other answers that suggested using a timer would be better.
  1 个评论
Jan
Jan 2012-5-25
+1:Exatcly. While PAUSE(0.1) might be useful or not, using set(h, 'xdata') is a good strategy as well as avoiding the repeated time-consuming AXIS command. A fast iteration requires to avoid all unnecessary function calls.

请先登录,再进行评论。


Sean de Wolski
Sean de Wolski 2012-5-24
No comment on the drawnow but I will say, using a timer object instead of a while-loop should really help stabilize your code.
doc timer
  1 个评论
Alan Armstrong
Alan Armstrong 2012-5-24
Thank you Sean I'll substitute in the timer for the while-loop and see if it helps.
Thanks again,
Alan

请先登录,再进行评论。


Stephen
Stephen 2012-5-24
throw a pause(0.02) in the while loop after the drawnow
  3 个评论
Jan
Jan 2012-5-25
Why, Sean? PAUSE(0.02) flushs accumulated Java events for reasons I don't know. I do not think that this is very helpful here, but I do not think that a warning demands for so many exclamation marks.
Daniel Shub
Daniel Shub 2012-5-25
I don't know why a timer was invented for a "single threaded" environment like MATLAB, but I am pretty sure it wasn't for working on the 20 ms time scale. As for PAUSE, if I want to flush the event queue and risk leaving my circle update function for an indeterminate amount of time, then I will flush the queue myself with DRAWNOW thank you very much. I would use a resource hogging loop that keeps checking toc (or etime) to get the best timing. Until MATLAB becomes truly multi-threaded, it should stay single threaded.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Timing and presenting 2D and 3D stimuli 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by