我有一个巨大的波形矩阵:
[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);
我是关闭还是离开?
答案 0 :(得分:1)
另一种方法是使用重新整形来将矢量排列到具有行n
和列等于ceil(length(w) / n)
的2D矩阵中,即向上舍入,以便它可以被整除,因为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)