我不能評價eval
,因爲我沒有這些波形文件,但沒有EVAL線,你可以做如下:
for i=1:22
istr = int2str(i);
for j=1:54
jstr = int2str(j);
name1= ['wave',jstr, jstr ];
add = ['F:\MIT Corpus\train\f', istr, '\phrase0',jstr,'_16k.wav'];
% eval([ sprintf(name1) '=wavread(add)']);
end
end
只是這種變化需要0.09s我的電腦上,相比於0.946s的原始代碼(沒有評估)。
和U也很可能做到這一點應該是更快:
for i=1:22
istr = int2str(i);
for j=1:54
jstr = int2str(j);
add = ['wave',jstr, jstr, '=wavread(F:\MIT Corpus\train\f', istr,'\phrase0',jstr,'_16k.wav)'];
% eval(add);
end
end
並行讀取可以做如下(小基於埃坦牛逼anwser):
% close matlabpool if exists
if matlabpool('size') > 0
matlabpool close;
end
% no of wave files to read
NO_OF_FILES = 22*54;
% preallocate waveCell for files to be readed into
waveCell = cell(1, NO_OF_FILES);
% make ii and jj wave string names to be used
waveNameCell = cell(1, NO_OF_FILES);
idx = 1;
for ii = 1:22
for jj = 1:54
waveNameCell{idx} = {int2str(ii), int2str(jj)};
idx = idx + 1;
end
end
% create 6 matlab workers (6 is max default in 2013a),
% if u want more, matlab will give en error
% but will also tell how to change it to more, very easy.
% Off course, its best to how no more workers than cores/threads/cpus, etc.
matlabpool(6);
% execute this loop in parallel using matlab workers
% each worker will read one wave file
parfor wavei = 1:NO_OF_FILES
waveName = waveNameCell{wavei};
ii = waveName{1};
jj = waveName{2};
str = sprintf('F:\\MIT Corpus\\train\\f%d\\phrase0\\%d_16k.wav', ii, jj);
waveCell{wavei} = wavread(str);;
end
matlabpool close;
但瓶頸可能是從硬盤讀取多個文件的速度比讀取一個文件要慢。也許少工人會更好。
非常感謝。它工作:D –
@amirnemat樂於幫助:) –