【问题标题】:how to read wav files one after other from the same folder in Matlab如何从Matlab中的同一文件夹中依次读取wav文件
【发布时间】:2012-07-19 14:32:32
【问题描述】:

我正在尝试编写一个程序,我必须读取一个 wav 文件,从中提取一些特征并保存它们,然后去选择下一个文件重复相同的过程。要选择的波形文件数量超过100。有人可以帮我如何读取一个接一个的波形文件。 (比如说文件被命名为 e1.wav、e2.wav 等等)。有人请帮帮我

【问题讨论】:

    标签: matlab file-io wav


    【解决方案1】:

    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.
    

    【讨论】:

    • 除了 fprintf 语句中必须使用单引号而不是双引号之外,它的工作原理.. 谢谢 Mehrwolf
    • 修正了引号。感谢您的提示!
    • 也许你可以使用wavread打开wav文件:mathworks.de/help/techdoc/ref/wavread.html。另见wavwrite:mathworks.de/help/techdoc/ref/wavwrite.html
    • @H.Muster:我的示例只是打算从一系列文件开始。我认为您将wavread 与dir 命令一起使用的方法几乎是处理WAV 文件的方法。
    • @Mehrwolf:您还应该提到初始化矩阵或元胞数组以存储从每个文件中提取的特征。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多