在 Matlab 中拆分音频文件
Splitting an audio file in Matlab
我正在尝试使用 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 中的元素数量不匹配以及如何解决这个问题。我怎样才能克服这个错误?或者有没有更简单的方法来分割音频文件(也许是我不知道的另一个功能或其他东西)?
看来每个 30 毫秒的片段不等于一个样本。那将是您的代码有效的唯一情况。即 0.03*fs != 1.
您可以尝试使用单元格代替..即将 t(m) 替换为 t{m}
您应该只使用变量 y 并重塑它以形成您的分割音频。例如,
chunk_size = fs*0.03;
y_chunks = reshape(y, chunk_size, 6000);
这将为您提供一个矩阵,每列一个 30 毫秒的块。此代码也将比循环读取文件中的小段更快。
正如 hiandbaii 所建议的,您也可以使用元胞数组。确保在此之前清除现有变量。未清除数组 t 可能是您收到错误 "Cell contents assignment to a non-cell array object."
的原因
你原来的错误是因为你不能用标量索引分配一个向量。也就是说,'m' 是一个标量,但您的 audioread 调用返回一个向量。这就是关于 I 和 B 大小不匹配的错误。您也可以通过将 t 设为二维数组并使用
之类的赋值来解决该问题
[t(m,:), fs] =
我认为分割音频最简单的方法是加载它并使用 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 你的向量。没什么大不了的,但是如果您在两者之间来回移动,那么最好有另一个变量来存储信号的原始长度。
我正在尝试使用 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 中的元素数量不匹配以及如何解决这个问题。我怎样才能克服这个错误?或者有没有更简单的方法来分割音频文件(也许是我不知道的另一个功能或其他东西)?
看来每个 30 毫秒的片段不等于一个样本。那将是您的代码有效的唯一情况。即 0.03*fs != 1.
您可以尝试使用单元格代替..即将 t(m) 替换为 t{m}
您应该只使用变量 y 并重塑它以形成您的分割音频。例如,
chunk_size = fs*0.03;
y_chunks = reshape(y, chunk_size, 6000);
这将为您提供一个矩阵,每列一个 30 毫秒的块。此代码也将比循环读取文件中的小段更快。
正如 hiandbaii 所建议的,您也可以使用元胞数组。确保在此之前清除现有变量。未清除数组 t 可能是您收到错误 "Cell contents assignment to a non-cell array object."
的原因你原来的错误是因为你不能用标量索引分配一个向量。也就是说,'m' 是一个标量,但您的 audioread 调用返回一个向量。这就是关于 I 和 B 大小不匹配的错误。您也可以通过将 t 设为二维数组并使用
之类的赋值来解决该问题[t(m,:), fs] =
我认为分割音频最简单的方法是加载它并使用 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 你的向量。没什么大不了的,但是如果您在两者之间来回移动,那么最好有另一个变量来存储信号的原始长度。