1

我有一个巨大的波形矩阵:

[w,fs] = wavread('file.wav');
length(w)
ans =

  258048

我想分段遍历这个矩阵(比如 50 个)并获得这些段的最大值以将其与另一个值进行比较。我试过这个:

thold = max(w) * .04;
nwindows = 50;
left = 1;
right = length(w)/nwindows;
counter = 0;
for i = 1:nwindows
  temp = w(left:right);
  if (max(temp) > thold)
      counter = counter + 1;
  end
  left = right;
  right = right+right;
end

但是 MATLAB 抛出了大量警告并给了我这个错误:

Index exceeds matrix dimensions.

Error in wlengthdur (line 17)
  temp = w(left:right);

我是接近还是偏离路线?

4

1 回答 1

1

另一种方法是使用 reshape 将向量排列到一个二维矩阵中,其行数n和列数等于ceil(length(w) / n)即四舍五入,以便它可以整除,因为 matlab 矩阵必须是矩形的。这样,您可以在一个步骤中找到最大值或您需要的任何内容,而无需循环。

w = randn(47, 1);
%this needs to be a column vector, if yours isn't call w = w(:) to ensure that it is

n = 5;

%Pad w so that it's length is divisible by n
padded = [w; nan(n - mod(length(w), n), 1)];

segmented_w = reshape(padded, n, []);

max(segmented_w)
于 2013-03-19T07:12:48.303 回答