繁体   English   中英

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

[英]Why is my MATLAB code printing every value within the for loop?

我有将声音文件分解为 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))行的末尾。 它表示一行的结束并抑制代码行的输出。 在灯架上有一个不错的博客进入这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM