簡體   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