フィルターのクリア

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 Center および File ExchangeAudio and Video Data についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by