如何在Matlab中从同一文件夹中逐个读取wav文件
我正在尝试编写一个程序,在这个程序中,我必须读取一个wav文件,从中提取一些特征并保存它们,然后去选择下一个文件,重复相同的过程。要拾取的波形文件数超过100个。谁能帮我一个接一个地读WAV文件吗。(假设文件名为e1.wav、e2.wav等等)。有人请帮帮我这里的如何在Matlab中从同一文件夹中逐个读取wav文件,matlab,file-io,wav,Matlab,File Io,Wav,我正在尝试编写一个程序,在这个程序中,我必须读取一个wav文件,从中提取一些特征并保存它们,然后去选择下一个文件,重复相同的过程。要拾取的波形文件数超过100个。谁能帮我一个接一个地读WAV文件吗。(假设文件名为e1.wav、e2.wav等等)。有人请帮帮我这里的dir命令非常有用。它或者显示目录的全部内容,但是您也可以指定一个glob来只返回文件的子集,例如dir('*.wav')。这将返回一个包含文件信息的结构数组,如名称,日期,字节,isdir等 要开始,请尝试以下操作: filelist
dir
命令非常有用。它或者显示目录的全部内容,但是您也可以指定一个glob来只返回文件的子集,例如dir('*.wav')
。这将返回一个包含文件信息的结构数组,如名称
,日期
,字节
,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:将双引号更改为单引号(thx为)
编辑2(建议):如果必须为每个文件存储处理结果,
我经常使用以下模式。我通常预先分配数组、结构数组或
与文件列表大小相同的单元格数组。然后我使用整数索引进行迭代
在文件列表上,我也可以用它来写输出。如果信息是
如果存储的是同构的(例如,每个文件一个标量),请使用数组或结构数组。
但是,如果文件之间的信息不同(例如,不同大小的向量或矩阵),请使用单元格数组
使用普通数组的示例:
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.
dir
命令在这里非常有用。它或者显示目录的全部内容,但是您也可以指定一个glob来只返回文件的子集,例如dir('*.wav')
。这将返回一个包含文件信息的结构数组,如名称
,日期
,字节
,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:将双引号更改为单引号(thx为)
编辑2(建议):如果必须为每个文件存储处理结果,
我经常使用以下模式。我通常预先分配数组、结构数组或
与文件列表大小相同的单元格数组。然后我使用整数索引进行迭代
在文件列表上,我也可以用它来写输出。如果信息是
如果存储的是同构的(例如,每个文件一个标量),请使用数组或结构数组。
但是,如果文件之间的信息不同(例如,不同大小的向量或矩阵),请使用单元格数组
使用普通数组的示例:
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.
除了在fprintf语句中必须使用单引号而不是双引号外,它还可以工作。。谢谢你,Mehrwolf修正了报价。谢谢你的提示!也许您可以使用
wavread
打开wav文件:。另请参见wavswrite
:@H.Muster:我的示例只是打算从一系列文件开始。我认为您将wavread
与dir
命令一起使用的方法与WAV文件非常相似。@Mehrwolf:您还应该提到初始化矩阵或单元格数组以存储从每个文件提取的特征。除了在fprintf中必须使用单引号而不是双引号外,这是有效的陈述谢谢你,Mehrwolf修正了报价。谢谢你的提示!也许您可以使用wavread
打开wav文件:。另请参见wavswrite
:@H.Muster:我的示例只是打算从一系列文件开始。我认为您将wavread
与dir
命令一起使用的方法与WAV文件非常相似。@Mehrwolf:您还应该提到初始化矩阵或单元格数组以存储从每个文件提取的特征。