user2482542
user2482542

Reputation: 361

Matlab error: Index exceeds matrix dimensions

I'm trying to perform STFT on an audio file. I need to get the fft of each window.

I used the follwing code.

[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.

Upvotes: 1

Views: 12140

Answers (1)

Oleg
Oleg

Reputation: 10686

As the error message says, you are trying to access a position in wave tat does not exist.

See this example:

a    = rand(7,1);
step = 4;

1:step:7
ans =
     1     5

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.

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.

Upvotes: 1

Related Questions