I performed transfer learning on yamnet, then, when I try to classify a sound I recorded using y = net(x), it says "Array indices must be positive integers or logical values."

5 views (last 30 days)
The purpose of this program is to detect the gender and age of the speaker. It gives you the option to record your voice or use pre-recorded audio files to evaluate.
I then used a small dataset to perform transfer learning on yamnet, following the procedure on openExample('deeplearning_shared/TransferLearningUsingYAMNetExample').
Now, after training the model, I try to use it using the last command in the code, calling the function, but it reports the error "Array indices must be positive integers or logical values." back
disp('Welcome to the voice classifier!')
query = "Do you want to use a pre-recorded voice line? Type yes or no. \n";
mode = string(input(query, 's'));
noStr = 'no';
yesStr = 'yes';
if mode == yesStr;
prompt = "Type the filename of the audio file you wish to analyze. \n";
fileTest = input(prompt, "s"); % Navigates the desired audio file
% to be processed and inputs it to be read
fileInfotest = dir(fullfile('**', fileTest)); % Traces directory of the audio file
fileInfoscrp = dir(fullfile('*', 'Finalproj1.m')); % Traces directory of the .m file
fileFolderpath1 = string(getfield(fileInfotest, 'folder')); % Takes the path of the audio file from the directory
fileFolderpatharr1 = regexp(fileFolderpath1, '/| \', 'split'); % Isolates the folder name from the path into an array
fileFoldersrc = fileFolderpatharr1(end); % Plucks the source folder name from the array
fileFolderpath2 = string(getfield(fileInfoscrp, 'folder')); % Takes the path of the .m file from the directory
fileSrc = 'Desktop'; % If the folder of the audio file matches this, it will be moved to the main project folder
fileDest = 'Finals'; % The file will not be relocated if it is in this folder
if fileFoldersrc == fileSrc;
movefile(fullfile(fileFolderpath1, fileTest), fullfile(fileFolderpath2)); % Moves file to project folder
display('The file has been transferred to the operating folder and is being processed.')
clear fileFoldersrc % Clears the comparison string for next session
return
elseif fileFoldersrc == fileDest;
disp('The file is being processed.')
clear fileFoldersrc % Clears the comparison string for next session
return
else
return
end
else mode == noStr;
disp('You chose to record your voice.')
fs = 44100; % Sampling rate
nbits = 16;
nChannels = 1;
duration = 3; % Recording duration
fileRec = audiorecorder(fs, nbits, nChannels);
fprintf('Press any key to start %g seconds of recording…', duration); pause
fprintf('Recording…');
recordblocking(fileRec, duration); % Records for a set amount of seconds
fprintf('Finished recording.\n');
fprintf('Press any key to play the recording…'); pause; fprintf('\n');
play(fileRec);
fprintf('Processing the waveform…\n');
fileTest = getaudiodata(fileRec); % Get audio sample data
clear mode
end
gen_and_age = gen_and_ageNet(fileTest);

Accepted Answer

Ruchika Parag
Ruchika Parag on 28 Nov 2024
Hi @Eiram Voriz, the error message "Array indices must be positive integers or logical values" typically occurs in MATLAB when you try to use a non-integer or non-logical value as an index for an array. Let's go through your code to identify and resolve the issue.Potential Issues and Solutions:
  1. String Comparison: MATLAB string comparisons should use 'strcmp' or 'strcmpi' (for case-insensitive comparison) instead of '=='. This might not be directly causing the error, but it's a good practice to avoid logical errors.
  2. File Handling: Ensure that the file paths and names are correct. If 'fileTest' is supposed to be an audio file name, make sure it's a valid string and not an array or other data type.
  3. Function Call: The function 'gen_and_ageNet'(fileTest) should be called with the correct input type. If 'fileTest' is expected to be an audio file path, ensure it's a string. If it's expected to be audio data, ensure it's a numeric array.
Here's a revised version of your code with corrections and added debugging statements:
disp('Welcome to the voice classifier!');
query = "Do you want to use a pre-recorded voice line? Type yes or no. \n";
mode = string(input(query, 's'));
noStr = 'no';
yesStr = 'yes';
if strcmp(mode, yesStr)
prompt = "Type the filename of the audio file you wish to analyze. \n";
fileTest = input(prompt, "s"); % Input the audio file name
fileInfotest = dir(fullfile('**', fileTest)); % Find the file
fileInfoscrp = dir(fullfile('*', 'Finalproj1.m')); % Find the script file
if isempty(fileInfotest)
error('File not found. Please check the filename and try again.');
end
fileFolderpath1 = string(fileInfotest.folder); % Get the folder path
fileFolderpatharr1 = regexp(fileFolderpath1, '/|\\', 'split'); % Split path components
fileFoldersrc = fileFolderpatharr1(end); % Get the last folder name
fileFolderpath2 = string(fileInfoscrp.folder); % Get the script folder path
fileSrc = 'Desktop'; % Source folder
fileDest = 'Finals'; % Destination folder
if strcmp(fileFoldersrc, fileSrc)
movefile(fullfile(fileFolderpath1, fileTest), fullfile(fileFolderpath2)); % Move the file
disp('The file has been transferred to the operating folder and is being processed.');
elseif strcmp(fileFoldersrc, fileDest)
disp('The file is being processed.');
else
error('Unexpected folder location. The file cannot be processed.');
end
elseif strcmp(mode, noStr)
disp('You chose to record your voice.');
fs = 44100; % Sampling rate
nbits = 16;
nChannels = 1;
duration = 3; % Recording duration
fileRec = audiorecorder(fs, nbits, nChannels);
fprintf('Press any key to start %g seconds of recording…', duration); pause;
fprintf('Recording…');
recordblocking(fileRec, duration); % Record audio
fprintf('Finished recording.\n');
fprintf('Press any key to play the recording…'); pause; fprintf('\n');
play(fileRec);
fprintf('Processing the waveform…\n');
fileTest = getaudiodata(fileRec); % Get audio sample data
else
error('Invalid input. Please enter "yes" or "no".');
end
% Call the function with the correct input type
try
gen_and_age = gen_and_ageNet(fileTest);
catch ME
disp('Error occurred while calling gen_and_ageNet:');
disp(ME.message);
end
Make sure the function 'gen_and_ageNet' is expecting the type of input you are providing (fileTest). If it expects a file path, ensure 'fileTest' is a string with the correct path. If it expects audio data, ensure 'fileTest' is a numeric array. Hope this helps!
  1 Comment
Eiram Voriz
Eiram Voriz on 30 Nov 2024
Thank you for the corrections, sir! Unfortunately, I couldn’t apply your suggestions as the deadline has passed for this project, but I wholeheartedly appreciate your help! I will take into account your tips for the future, if I ever encounter a similar problem.
Thank you once again and godspeed!

Sign in to comment.

More Answers (0)

Categories

Find more on Sequence and Numeric Feature Data Workflows 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!