为什么我的MATLAB代码要打印for循环中的每个值



我有一段代码,可以将声音文件分解为1秒的块,计算块的RMS,然后绘制所有块。它运行良好,直到我编辑它,使其一次读取一个文件夹,而不是一个用户加载的文件。现在它打印fs的每一个值(全部为32k(,这显然大大减慢了脚本的速度。这是新的脚本:

DirIn = 'D:Trial'
eval(['filelist=dir(''' DirIn '/*.wav'')']) 

for i = 1:length(filelist)
[y,fs] = audioread(strcat(DirIn,'/',filelist(i).name))
npts = length(y);
chunkDur = 1; % length of chunk to analyze in seconds
systemCal = 0; % this should be whatever dB corresponds to an amplitude of 1 in wav file

chunkPts = fs * chunkDur;
rms = [];
for i = 1:chunkPts:npts-chunkPts
chunkRMS = 20 * log10(std(y(i: i + chunkPts))) + systemCal; % rms of chunk in dB
rms = [rms; chunkRMS];
end
t = [0: length(rms) - 1] * chunkDur; % time scale
plot(t, rms)
xlabel('Time (s)');
ylabel('RMS dB');
end

作为参考,以下是有效的原件:

npts = length(data);
chunkDur = 1; % length of chunk to analyze in seconds
systemCal = 0; % this should be whatever dB corresponds to an amplitude of 1 in wav file

chunkPts = fs * chunkDur;
rms = [];
for i = 1:chunkPts:npts-chunkPts
chunkRMS = 20 * log10(std(data(i: i + chunkPts))) + systemCal; % rms of chunk in dB
rms = [rms; chunkRMS];
end
t = [0: length(rms) - 1] * chunkDur; % time scale
plot(t, rms)
xlabel('Time (s)');
ylabel('RMS dB');

您在第[y,fs] = audioread(strcat(DirIn,'/',filelist(i).name))行末尾遗漏了分号;。它表示一行的结束,并抑制代码行的输出。这里有一个很好的博客条目。

最新更新