Real time plot of vibration data

15 ビュー (過去 30 日間)
Marius
Marius 2015 年 4 月 21 日
編集済み: Cindy Solomon 2015 年 4 月 22 日
Hi everybody
For a course at the university I need Matlab to compute a realtime plot of vibration data. Below you find the interesting part of the code. It's just an example, the full code is much more complex:
N = 500
dt=0.02
t_out = (0:dt:N*dt)'
y_out = rand(N+1,1);
global rt_time
global rt_theta
n_test=0;
rt_i = 1;
tic
while(n_test < length(t_out))
rt_theta(rt_i)=y_out(rt_i);
rt_time(rt_i)=t_out(rt_i);
plot(rt_time,rt_theta), axis([t_out(1) t_out(length(t_out)) min(y_out) max(y_out)])
grid on
rt_i = rt_i + 1;
n_test = n_test + 1;
pause(dt)
end
toc
So this gives me a realtime plot of the data. The problem is, that if I for example plot for N = 500 then the total time is 10 sec. If I use tic/toc to measure the time used for the animation I get about 15.6 sec. So it isn't "realtime". And in the complete Matlab implementation the influence is stronger because there, I use 7 Plots and N is about 3000. Then i searched the internet and read that i better should use the 'get' command to update 'XData' and 'YData' because the plot command is too slow. So I changed the code as posted below:
N = 500
dt=0.02
t_out =(0:dt:N*dt)
y_out = rand(N+1,1)';
n_test = 0;
rt_i = 1;
length(t_out)
test = plot(nan, nan,'-'), axis([t_out(1) t_out(length(t_out)) min(y_out) max(y_out)]);
grid on
tic
while(n_test < length(t_out))
rt_time = get(test,'XData')
rt_theta = get(test,'YData')
rt_time = [rt_time t_out(rt_i)]
rt_theta = [rt_theta y_out(rt_i)]
set(test,'XData',rt_time,'YData',rt_theta)
rt_i = rt_i + 1;
n_test = n_test + 1;
pause(dt)
end
toc
But now it's even slower. For N = 500 it needs now 16.9 sec.
Can you help me to change the code, so the animation is more "realtime"?

回答 (2 件)

Christiaan
Christiaan 2015 年 4 月 22 日
編集済み: Christiaan 2015 年 4 月 22 日
Dear Marius,
You could use a pause function together with toc for the next value. A example is shown below:
clc;clear;
frequency = 10;
t_min = 0; t_max = 10; N = (t_max-t_min)*frequency;
tic
for i=0:N
if i==0
t = t_min;
y = randn();
else
timestap = toc;
if timestap<i/frequency
pause(i/frequency-timestap);
t = [t i/frequency];
y = [y randn(1)];
figure(1);
plot(t,y);axis([t_min t_max -2 2]);
end
end
end
toc
Good luck! Christiaan
note: if your model has a higher compuational time then the 'real time', keep in mind that the 'real time figure' would work and you have to think about optimizing your model.

Cindy Solomon
Cindy Solomon 2015 年 4 月 22 日
編集済み: Cindy Solomon 2015 年 4 月 22 日
Hi Marius,
To add to this answer, I would also recommend looking into using a "timer" object such that you plot every T seconds. This might cause a few dropped frames (if, for example, you set the "BusyMode" on the Timer object to "drop"), but depending on how often the updates are, this may not be noticeable. For more information on those objects, refer to this doc link.
Similarly, this function can work in combination with a "drawnow" command. For example, if your timer had a period of 0.001 seconds, the timer is executing at a rate faster than the rate at which graphics can be rendered. In order to fix this, consider placing a "drawnow" command in the timer's callback. This will flush the graphics queue and ensure that the current frame is drawn. You could also consider increasing the timer's period to a rate at which MATLAB can reliably draw the plots. Without a real time operating system, there is no guarantee that a plot will be updated in "real time", but depending on the data itself, you can get reasonably close, however.
Hope this helps!

Community Treasure Hunt

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

Start Hunting!

Translated by