I get the error 'Subscript indices must either be real positive integers or logicals'? Please help!

2 次查看(过去 30 天)
Hi,
I am trying to take short parts of hundreds of mp3 files and mix them automatically. It works until about 43rd song but after that I get the error message at the line of "x = x(end-fs*excerptDur:end);". I can't find the solution. Please help.
---------------------------------------------
filenames = dir('*.mp3');
clc
analysis.dur = zeros(1, length(filenames));
fs = 44100;
totalDur = 100;
y = zeros(1, fs*totalDur);
excerptDur = 2;
hop = excerptDur/4*fs;
startPtr = 1;
endPtr = excerptDur*fs+1;
warning off
for i=1:length(filenames)
[x, fs] = audioread(filenames(i).name);
x = mean(x');
x = x(1:end/10);
x = x(end-fs*excerptDur:end);
analysis.dur(i) = round(length(x)/fs*60);
x1 = hann(length(x)).*x';
y(startPtr:endPtr) = y(startPtr:endPtr) + x1';
startPtr = startPtr + hop;
endPtr = endPtr + hop;
disp(filenames(i).name)
end
y = y(1:endPtr);
warning on
sound(y, fs)
spectrogram(y,2048,2000,'yaxis');

回答(1 个)

Stephen23
Stephen23 2015-11-14
Your index is probably going to zero or negative. Consider if fs*exceptDur is larger than the length of x
x = x(end-fs*excerptDur:end);
then it is equivalent to
x = x(-N:end);
for some whole number N. You could avoid this by using max:
x = x(max(1,end-fs*excerptDur):end);
  4 个评论
JAE KANG JANG
JAE KANG JANG 2015-11-19
Thank you so much for your reply. The problem was one of mp3 files had a different sample rate.
I am trying to extract some portions(about 1 second) from a lot of mp3 files(about 1000) then put them in order to create a mix of an audio file. The code extracts in the middle of audio file. However, it would be great if it extracts the beginning of the chorus part. I think the chorus part has the peak or maximum energy in the song and I've been figuring out but I couldn't and I really need help... If you can help me that would be really great.. Thank you so much for your help. I really appreciate it.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Multirate Signal Processing 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by