필터 지우기
필터 지우기

How to Increase the Refresh Rate of a Continuously Updating Plot

조회 수: 5 (최근 30일)
Michael
Michael 2013년 7월 29일
I'm working on creating some audio processing software and I wanted to have it display a plot of the audio data with a moving marker on the plot while the audio plays. I'm using slightly modified code that I took from this page on Mathworks. However, the marker only seems to update every second or so no matter what refresh rate I tell the audioplayer object to use. I modified the code to use line objects instead of plotting a set of points for the marker and used set axis limits which I heard should decrease the amount of internal operations Matlab has to do for updating the plot. Neither my CPU or RAM is capping out so I'm not sure what the problem is. Does anyone have any ideas of why my plot is slow to update? Here's the relevant section of code (for clarification, AC is an audio clip class I wrote):
T = length(Samples)/AC.SampleRate;
t = linspace(0,T,AC.Length);
figure; hold on;
plot(t,AC.Data(:,1));
title(AC.Name);
xlabel('Time (s)')
ylimits = [ -1 1 ];
xlim ([ 0 T ]); ylim([ -1 1 ]);
hline = line( [ 0 0 ], ylimits, 'Color', 'r');
player = audioplayer(Samples,AC.SampleRate);
player.TimerFcn = {@plotMarker, player, gcf, ylimits};
player.TimerPeriod = 0.01;
play(player)
%**************************************************************
function plotMarker(obj,eventdata,player,figHandle,plotdata)
if strcmp(player.Running, 'on')
hMarker = findobj(figHandle, 'Color', 'r');
delete(hMarker)
x = player.CurrentSample/player.SampleRate;
line([ x x ], plotdata, 'Color', 'r');
end

답변 (2개)

Jan
Jan 2013년 7월 29일
1. findobj gets slower the more handles it has to compare. So using a flat comparison is recommended:
hMarker = findobj(figHandle, 'flat', 'Color', 'r');
2. findobj can be omitted completely, if you store the handles locally e.g. in the timer's UserData.
3. Deleteing and recreating objects wastes time, better reset the data only:
UD = get(Obj.UserData);
hMarker = UD.hMarker;
x = player.CurrentSample / player.SampleRate;
set(xMarker, 'XData', [x, x], 'YData', plotdata);
  댓글 수: 2
Rodney Thomson
Rodney Thomson 2013년 7월 29일
I completely agree with everything in this answer as general advice. But I don't think it is the answer to your problem.
I was about to suggest an example I have on the File Exchange (<http://iheartmatlab.blogspot.com.au/2008/07/sound-card-spectral-analyser-gui.html>) as an example of efficient plotting of real-time data as it does similar to what you are requesting but acquiring data from a soundcard instead of a file.
So I downloaded to see what load it was using, and to my suprise I am seeing the same behaviour you are. That is updates maybe once every 1-2 seconds! Despite the timer being fired ~10 times per second.
Do what I did and run the MATLAB Profiler. It showed almost all of the time was spent in audiorecorder.pause() (called when the one-shot timer ends and calls stop() on the audiorecorder). In particular on the obj.Channel.close() method.
This is behaviour that has changed since MATLAB 2008a (version I was running when I wrote it) as it used to update in real time no worries.
So, sorry but I don't have a fix. But I think how audioplayer is being used might be the issue (although I cannot see why)
Michael
Michael 2013년 7월 30일
Thanks for the information, Jan and Rodney. I'll try and incorporate what you suggested and maybe try having the plot marker update independently of the audioplayer object. I'll also take a look at the MATLAB Profiler.

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


Iain
Iain 2013년 7월 29일
Try adding a "drawnow" statement after your plotting command.

카테고리

Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by