【问题标题】:Why is my MATLAB code printing every value within the for loop?为什么我的 MATLAB 代码会打印 for 循环中的每个值?
【发布时间】:2020-11-14 00:54:25
【问题描述】:

我有代码将声音文件分成 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');

【问题讨论】:

    标签: matlab audio acoustics


    【解决方案1】:

    您在[y,fs] = audioread(strcat(DirIn,'/',filelist(i).name)) 行的末尾错过了一个分号;。 它表示一行的结束并抑制代码行的输出。 here 上有一个不错的博客条目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      • 1970-01-01
      • 2023-01-16
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多