scrolling plot speed and axis jitter
11 ビュー (過去 30 日間)
古いコメントを表示
I'd like to create auto-scrolling plots in which data scrolls by, automatically, as it gets collected (from a DAQ). Before worrying about adding data I wanted to first check how plots behave with a pre-defined data vector where I adjust the x-limits to simulate scrolling. Just to clarify, I don't intend to have any manual scrolling, just calls to adjust the axis limits.
This is the baseline code for plotting:
t = 0:1/1000:20;
s = rand(1,length(t)).*sin(0.67*2*pi*t);
ax(1) = subplot(3,1,1);
h1 = plot(t,s);
toc
ax(2) = subplot(3,1,2);
h2 = plot(t,s);
ax(3) = subplot(3,1,3);
h3 = plot(t,s);
Now I'd like to do something like the following:
update_tick = 0;
set(ax,'ylim',[-1 1])
tic
for t2 = 0:0.01:18
set(ax,'xlim',[t2 t2+2]) %Move the data view
update_tick = update_tick + 1;
%At some interval rerender the ticks
if mod(update_tick,5) == 0
xtick = t2:0.1:t2+2;
set(ax,'XTick',xtick)
end
drawnow
%pause(0.005)
end
toc
The code above changes to the x-limits to simulate scrolling/new data and occasionally updates the displayed ticks.
This code however is horribly slow. This takes about 50 seconds to run on my computer. It gets faster if the drawnow is replaced with pause (about 12s) but I have other things to do in the DAQ collection loop and I'd like this number to be closer to 1-2 seconds. For reference, Matlab 2017b documentation states that using the pause function is equivalent to the drawnow command, but I find I like this explanation better (basically a drawnow if possible within allotted time): https://www.mathworks.com/matlabcentral/answers/7309-is-pause-a-superset-of-drawnow#answer_192103
Additionally, as the plotting is occurring the axes are shaking just slightly which is really annoying.
Any suggestions on speeding up the code?
3 件のコメント
回答 (1 件)
Joshua Robinson
2019 年 4 月 29 日
Your main problem is that you're forcing matlab to recalculate and update the position and names of all the ticks every couple of iterations. So even if t2 changed by a little bit, every single tick is recalculated.
Instead it would make more sense to only update the tick values that actually change as t2 moves along (aka the first and last ticks).
The quickest way to achieve this is to round t2 to the nearest tick value and add a tick offset array to the rounded t2 value ala...
dtick = 0.1;
tickbase = (0:dtick:2);
% main loop
for t2=tmin:dt:tmax;
% BLAH BLAH BLAH
% calculate new ticks
nearest_tick = dtick*round(t2/dtick);
xtick = nearest_tick + tick_offset;
set(ax,'XTick',xtick)
% BLAH BLAH BLAH
end
So the final code should be something along the lines of...
clear all
close all
t = 0:1/1000:20;
s = rand(1,length(t)).*sin(0.67*2*pi*t);
ax(1) = subplot(3,1,1);
h1 = plot(t,s);
toc
ax(2) = subplot(3,1,2);
h2 = plot(t,s);
ax(3) = subplot(3,1,3);
h3 = plot(t,s);
% my edit
ax(1).XGrid = 'on';
ax(1).YGrid = 'on';
ax(2).XGrid = 'on';
ax(2).YGrid = 'on';
ax(3).XGrid = 'on';
ax(3).YGrid = 'on';
dtick = 0.1;
tick_offset = (0:dtick:2);
update_tick = 0;
set(ax,'ylim',[-1 1])
tic
for t2 = 0:dtick:18
set(ax,'xlim',[t2 t2+2]) %Move the data view
update_tick = update_tick + 1;
% %At some interval rerender the ticks
if mod(update_tick,5) == 0
nearest_tick = dtick*round(t2/dtick); % Code from above
xtick = nearest_tick + tick_offset;
set(ax,'XTick',xtick)
end
drawnow;
end
toc
3 件のコメント
Joshua Robinson
2019 年 5 月 6 日
Yeah sorry, I goofed. I thought I changed the step size back to what you had in the original code.
That being said, I'm curious if you actually need the step size to be 0.01. I don't know about your specific application but it seems like the end goal is to have the graph display one seconds worth of data for every second of real time. And as long as the animation is relatively smooth the exact step size doesn't matter, no?
For example, on my machine if I put the step size to 0.02 it displays the 20 seconds of data in roughly 20 seconds of real time. So you probably need to adjust how many images the loop attempts to draw per second. I think anywhere between 24-60 frames per second is smooth to the human eye.
You can also add in some logic that forces the main loop to pause unless the appropriate time per frame has passed. So that even if your step size is large (~0.1) the code won't rush to display all the information.
And of course the simpler the image is the faster Matlab can draw it. So having less ticks marks makes it so Matlab can push out images faster.
参考
カテゴリ
Help Center および File Exchange で Graphics Performance についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!