Why do I recieve an error "Index in position 1 exceeds array bounds" ?
1 view (last 30 days)
Show older comments
Joenam Coutinho
on 12 Mar 2022
Commented: Star Strider
on 13 Mar 2022
I have a dataset with 5132 audio samples. The samples have different time duration. Samples ranging from 1 to 10 seconds.
For Eg: Audiofile1 has duration 1 secs, Audiofile 2 has duration of 6 seconds, Audiofile 3 has duration of 10 seconds..................etc.
I had to remove the silent parts from each sample. Then I have to trim this modified sample to just 1 second (First 1 Second). Once the program is run. A few samples work through but some gave me an error ( I had to manually change 'y' with various 'i' values inorder to check which sample gave me the error)
clear;
close all;
NewTrimmed = 'C:\Users\joena\Desktop\backup_cough_from_main_folder\TrimmedSamples\'; %Creation of a new folder for Trimmed samples
s = 'C:\Users\joena\Desktop\backup_cough_from_main_folder\coughswav\';
Coughdirectory = dir(['C:\Users\joena\Desktop\backup_cough_from_main_folder\coughswav','/*.wav']);
Totalfiles =size(Coughdirectory,1); %Counting the number of samples
Name_of_file = cell(1,Totalfiles); % Pre-Allocation using Cell array
Name_of_file{1,Totalfiles} =[];
%k = 'C:\Users\joena\Desktop\backup_cough_from_main_folder\coughswav\006f340f-7067-4a95-ae52-97bf9ba31558.wav';
%Ignore the above commented line
for i = 1:Totalfiles %Loop to go through all the dataset, Remove silence parts and then crop it to 1 sec clips
Name_of_file = strcat(s,Coughdirectory(i).name);
[y,Fs] = audioread(Name_of_file);
envelope = imdilate(abs(y), true(1501, 1)); % To remove the quiet parts in the audio
quietParts = envelope < 0.04; % Or whatever value you want
% Cut out quiet parts and plot.
yEdited = y; % Initialize
yEdited(quietParts) = [];
desiredlength = 1; % Cut the yEdited audio file to 1 second (First One Second)
L = Fs * desiredlength; % ################ IS THERE AN ALTERNATIVE CODE FOR CROPPING AUDIO TO 1 SECOND
trimmedAudioMatrix =yEdited(1:L,:);
plot(trimmedAudioMatrix);
Newfilesaved = strcat(NewTrimmed,Coughdirectory(i).name); % saving the modified audio with the name of the original
audiowrite(Newfilesaved,trimmedAudioMatrix,Fs); % Saved to a new folder 'Newfilesaved'
end
The error ( This error occurs with only some files)
Index in position 1 C
Error in march_11 (line 37)
trimmedAudioMatrix =yEdited(1:L,:);
0 Comments
Accepted Answer
Star Strider
on 12 Mar 2022
Try this —
desiredlength = 1; % Cut the yEdited audio file to 1 second (First One Second)
L = Fs * desiredlength; % ################ IS THERE AN ALTERNATIVE CODE FOR CROPPING AUDIO TO 1 SECOND
trimmedAudioMatrix = yEdited;
if size(yEdited,1) > L
trimmedAudioMatrix = yEdited(1:L,:);
end
The problem is apparently that not all the ‘y’ arrays are longer than 1 second.
.
2 Comments
More Answers (0)
See Also
Categories
Find more on Multichannel Audio Input and Output in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!