在Matlab中批量读取wav文件以创建神经网络训练集矩阵

3
我正在进行一个小型神经网络项目,对Matlab非常陌生。
我有大约400个短的wav文件,需要读取,然后组合成一个矩阵数据集。我找不到任何关于如何将所有wav文件加载到Matlab中以便存储每个文件为不同名称的信息。
我的问题是:
  • 是否可以在Matlab中批处理wav文件,使得每个向量都存储为单独的数据?
  • 如果它们的维度(长度)不同,如何使用处理后的wav文件向量填充矩阵?
1个回答

3

这个解决方案利用了单元数组{...},可以处理不同维度、大小甚至类型的数据。在这里,Y将存储目录中所有音频文件的.wav采样数据,而FS则是它们的采样率。

% create some data (write waves)
load handel.mat;                  %predifined sound in matlab stored in .mat
audiowrite('handel1.wav',y,Fs);   %write the first wave file
audiowrite('handel2.wav',y,Fs);   %write the second
clear y Fs                        %clear the data


% reading section
filedir = dir('*.wav');           %list the current folder content for .wav file
Y = cell(1,length(filedir));      %pre-allocate Y in memory (edit from @ Werner)
FS = Y;                           %pre-allocate FS in memory (edit from @ Werner)
for ii = 1:length(filedir)        %loop through the file names

    %read the .wav file and store them in cell arrays
    [Y{ii,1}, FS{ii,1}] = audioread(filedir(ii).name);  

end

您可以通过以下方法访问数据:

for ind_wav = 1:length(Y)
    wav_data = Y{ind_wav,1};
end

2
还要告诉他预分配单元格数组:Y = cell(1,length(filedir)); FS = Y; - Werner
非常感谢!现在我有一个wav_data文件,其中包含所有信号值,但是我不确定下一步该怎么做: 我需要创建一个矩阵,它由最长音频文件的向量大小组成的400行(音频文件数量)。找到最长音频文件的大小不是问题,但在wav_data中,我们有一个大向量,我需要以某种方式将其重新包装成上述矩阵。 - nanachan
1
请参见https://dev59.com/-lfUa4cB1Zd3GeqPLtJG。 - marsei
@Magla - 非常感谢您提供的链接!现在我已经完成了这部分所需的内容! - nanachan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接