我无法评估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
仅此更改在我的 PC 上需要 0.09 秒,而原始代码(没有 eval)需要 0.946 秒。
你也可以这样做,这应该更快:
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
可以按如下方式并行读取(基于小 Eitan T 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;
但瓶颈可能是从硬盘驱动器同时读取多个文件比读取一个文件要慢。也许更少的工人会更好。