Matlab错误:索引超出矩阵尺寸 [英] Matlab error: Index exceeds matrix dimensions
问题描述
我正在尝试对音频文件执行STFT.我需要获取每个窗口的fft.
I'm trying to perform STFT on an audio file. I need to get the fft of each window.
我使用了以下代码.
[wave,fs] = wavread('40.wav');
w_length = 1024;
for v = 1:w_length:length(wave)
data_sub = wave(v:v+w_length);
subsection_fft = fft(data_sub);
figure(1)
plot(subsection_fft)
end
但是我收到以下错误消息.
But i get the following error.
??? Index exceeds matrix dimensions.
Error in ==> chk at 7
data_sub = wave(v:v+w_length);
你能告诉我我该怎么做才能纠正这个问题.
Can you tell me what I can do to rectify this.
推荐答案
如错误消息所述,您正在尝试访问wave
中的位置,而该位置不存在.
As the error message says, you are trying to access a position in wave
tat does not exist.
请参见以下示例:
a = rand(7,1);
step = 4;
1:step:7
ans =
1 5
当v = 5
时,您将尝试访问位置v:v+step
,即5到9,但a
最多只能定义7个元素.
when v = 5
, you will try to access position v:v+step
, i.e. 5 to 9, but a
is only defined up to 7 elements.
在您的情况下,wave
的定义最高为length(wave)
,但是在上一次迭代中,您将越界.
In your case, wave
is defined up to length(wave)
, but on the last iteration you will go out of bounds.
为避免这种情况,方法是对末端序列进行采样并减去序列的长度:
To avoid it, on approach would be to sample the end sequences and subtract the length of the sequence:
pos = (1+w_length:w_length:length(wave))-w_length
for v = pos
% do stuff
end
但是,您将剩下一些未处理的部分,您将在最后一次迭代时在循环之外进行处理.
However, you will be left with some unprocessed part which you will have to do outside of the loop as last iteration.
这篇关于Matlab错误:索引超出矩阵尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!