【发布时间】:2012-07-19 14:32:32
【问题描述】:
我正在尝试编写一个程序,我必须读取一个 wav 文件,从中提取一些特征并保存它们,然后去选择下一个文件重复相同的过程。要选择的波形文件数量超过100。有人可以帮我如何读取一个接一个的波形文件。 (比如说文件被命名为 e1.wav、e2.wav 等等)。有人请帮帮我
【问题讨论】:
我正在尝试编写一个程序,我必须读取一个 wav 文件,从中提取一些特征并保存它们,然后去选择下一个文件重复相同的过程。要选择的波形文件数量超过100。有人可以帮我如何读取一个接一个的波形文件。 (比如说文件被命名为 e1.wav、e2.wav 等等)。有人请帮帮我
【问题讨论】:
dir 命令在这里很有帮助。它要么显示目录的全部内容,但您也可以指定一个 glob 来仅返回文件的子集,例如dir('*.wav')。这将返回一个包含文件信息的结构数组,例如name、date、bytes、isdir 等。
要开始使用,请尝试以下操作:
filelist = dir('*.wav');
for file = filelist
fprintf('Processing %s\n', file.name);
fid = fopen(file.name);
% Do something here with your file.
fclose(fid);
end
编辑 1: 将双引号更改为单引号(感谢 user1540393)。
Edit 2(amro建议):如果必须按文件存储处理结果, 我经常使用以下模式。我通常预先分配一个数组、一个结构数组或 与文件列表大小相同的元胞数组。然后我使用整数索引进行迭代 在文件列表上,我也可以用它来写输出。如果信息是 存储是同质的(例如每个文件一个标量),使用数组或结构数组。 但是,如果文件之间的信息不同(例如不同大小的向量或矩阵),请改用元胞数组。
一个使用普通数组的例子:
filelist = dir('*.wav');
% Pre-allocate an array to store some per-file information.
result = zeros(size(filelist));
for index = 1 : length(filelist)
fprintf('Processing %s\n', filelist(index).name);
% Read the sample rate Fs and store it.
[y, Fs] = wavread(filelist(index).name);
result(index) = Fs;
end
% result(1) .. result(N) contain the sample rates of each file.
使用元胞数组的示例:
filelist = dir('*.wav');
% Pre-allocate a cell array to store some per-file information.
result = cell(size(filelist));
for index = 1 : length(filelist)
fprintf('Processing %s\n', filelist(index).name);
% Read the data of the WAV file and store it.
y = wavread(filelist(index).name);
result{index} = y;
end
% result{1} .. result{N} contain the data of the WAV files.
【讨论】:
wavread打开wav文件:mathworks.de/help/techdoc/ref/wavread.html。另见wavwrite:mathworks.de/help/techdoc/ref/wavwrite.html
wavread 与dir 命令一起使用的方法几乎是处理WAV 文件的方法。