【问题标题】:Splitting an audio file in Matlab在 Matlab 中分割音频文件
【发布时间】:2015-04-27 20:49:52
【问题描述】:

我正在尝试使用 Matlab 将音频文件拆分为 30 毫秒的不相交间隔。我目前有以下代码:

clear all
close all

% load the audio file and get its sampling rate
[y, fs] = audioread('JFK_ES156.wav');

for m = 1 : 6000
    [t(m), fs] = audioread('JFK_ES156.wav', [(m*(0.03)*fs) ((m+1)*(0.03)*fs)]);
end

但问题是我收到以下错误:

In an assignment  A(I) = B, the number of elements in B and I
must be the same.

Error in splitting (line 12)
    [t(m), fs] = audioread('JFK_ES156.wav', [(m*(0.03)*fs)
    ((m+1)*(0.03)*fs)]);

我不明白为什么 B 和 I 中的元素数量不匹配以及如何解决这个问题。我怎样才能克服这个错误?还是有一种更简单的方法来分割音频文件(可能是我不知道的另一个功能或什么的)?

【问题讨论】:

    标签: matlab audio frequency sampling


    【解决方案1】:

    我认为分割音频最简单的方法是加载它并使用 vec2mat 函数。所以你会有这样的东西;

    [X,Fs] = audioread('JFK_ES156.wav');
    
    %Calculate how many samples you need to capture 30ms of audio
    matSize = Fs*0.3;
    
    %Pay attention to that apostrophe. Makes sure samples are stored in columns
    %rather than rows.
    output = vec2mat(x,matSize)';
    
    %You can now have your audio split up into the different columns of your matrix. 
    %You can call them by using the column calling command for matrices.
    
    %Plot first 30ms of audio 
    plot(output(:,1));
    
    %You can join the audio back together using this command. 
    output = output(:);
    

    希望对您有所帮助。这种方法的另一个好处是它可以将所有数据保存在一个地方!

    编辑:我想到的一件事,取决于你的矢量大小,你可能会遇到这个问题。但我认为 vec2mat 实际上 zeroPads 你的向量。没什么大不了的,但是如果您在两者之间来回移动,那么使用另一个变量来存储信号的原始长度可能是个好主意。

    【讨论】:

    • vec2mat 进行零填充。它也是通信系统工具箱的一部分。
    【解决方案2】:

    您应该只使用变量 y 并对其进行整形以形成您的分割音频。例如,

    chunk_size = fs*0.03;
    y_chunks = reshape(y, chunk_size, 6000);
    

    这将为您提供一个矩阵,每列有一个 30 毫秒的块。这段代码也将比从文件中循环读取小段更快。

    按照 hiandbaii 的建议,您也可以使用元胞数组。确保在此之前清除现有变量。未清除数组 t 可能是您收到错误“单元格内容分配给非单元格数组对象”的原因。

    您最初的错误是因为您无法分配带有标量索引的向量。也就是说,“m”是一个标量,但您的 audioread 调用返回一个向量。这就是错误所说的 I 和 B 大小不匹配的内容。您也可以通过将 t 设为二维数组并使用类似的赋值来解决此问题

    [t(m,:), fs] = 
    

    【讨论】:

      【解决方案3】:

      似乎每个 30 ms 段不等于一个样本。那将是您的代码有效的唯一情况。即 0.03*fs != 1.

      您可以尝试改用单元格。即将 t(m) 替换为 t{m}

      【讨论】:

      • 我尝试这样做时收到以下错误:“将单元格内容分配给非单元格数组对象。”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-29
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多