简体   繁体   English

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

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

I have code that breaks up a sound file into 1 sec chunks, calculates the RMS of the chunk, then plots all chunks.我有将声音文件分解为 1 秒块的代码,计算块的 RMS,然后绘制所有块。 It works fine until I edited it to make it read through a folder rather than one user loaded file at at time.它工作正常,直到我编辑它以使其读取一个文件夹而不是一个用户一次加载的文件。 Now it prints every value of fs (all 32k) which obviously massively slows down the script.现在它打印 fs 的每个值(全部为 32k),这显然大大减慢了脚本的速度。 Here is the new script:这是新脚本:

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

For reference, here's the original that works:作为参考,这是有效的原件:

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');

You missed a semicolon ;你错过了一个分号; at the end of line [y,fs] = audioread(strcat(DirIn,'/',filelist(i).name)) .[y,fs] = audioread(strcat(DirIn,'/',filelist(i).name))行的末尾。 It signifies the end of a row and suppress an output of the code line.它表示一行的结束并抑制代码行的输出。 There is a nice blog-entry on this here .在灯架上有一个不错的博客进入这里

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

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