如何在播放时绘制wav文件

时间:2015-10-26 17:18:02

标签: matlab plot wav

如何在播放wav文件时在matlab上绘制波形文件。我想绘制幅度与时间的关系。我尝试使用以下代码尝试此操作:

[y,Fs] = audioread('test.wav');
sound(y,Fs);
clear y Fs

1 个答案:

答案 0 :(得分:2)

使用sound您没有真正的机会这样做,但使用audioplayer课程可以做到这一点:

function syncPlayerDemo()
%some example music
load handel;
%set up audio player
player = audioplayer(y, Fs);
[samples,channels]=size(y);
%calculate timeline
t=linspace(0,1/Fs*(samples-1),samples);
%initialize full plot, update will only move the visible area using xlim
h=plot(t,y);
%set up callback to update every <TimerPeriod> s
player.TimerFcn=@timerFcn;
player.TimerPeriod=0.1;
player.playblocking()
end

function timerFcn(source,data)
%an area of length <area> s will be visible
area=1;
position=(source.CurrentSample-1)/source.SampleRate;
%move visible area, current position is in the center
set(gca,'XLim',[position-area/2,position+area/2]);
%used a waitbar for testing, might be commented in
%waitbar(source.CurrentSample/source.TotalSamples);
end

使用仅timerFcn重新同步的图表,可以使用自动移动到一侧的绘图进一步提高此绘图的质量。