extract audio features from music
Show older comments
My code below is failing to extract the audio features specified on the code and I can't solve the problem. Anyone can help please? Thanks very much
Matlab presents the following errors:
'Unrecognized function or variable 'spectralCentroid'.
Error in AudioFeaturesExtractAudioToolbox_01>extractAudioFeatures (line 49)
spectralCentroid = spectralCentroid(audio,sampleRate);
Error in AudioFeaturesExtractAudioToolbox_01 (line 20)
features = extractAudioFeatures(audio,sampleRate,chosenFeatures);'
%This code extracts audio features from all audio files within a folder and saves the extracted features in separate '.mat' files
% select the folder path where audio files are located
folderPath = 'C:\Users\xxxxx\Documents\MATLAB\Experiment3';
%get a list of all audio files in the folder
fileList = dir(fullfile(folderPath,'*.wav'));
%Specify the chosen audio features
chosenFeatures = {'mfcc','spectralCentroid','spectralRolloff'};
% Loop through each audio file
for i = 1:length(fileList)
% Read the audio file
filePath = fullfile(folderPath,fileList(i).name);
[audio,sampleRate] = audioread(filePath);
% Extract chosen audio features
features = extractAudioFeatures(audio,sampleRate,chosenFeatures);
% Do something with extracted features (e.g. save to a file)
saveFilePath = fullfile(folderPath,[fileList(i).name '_features.mat']);
save(saveFilePath,'features');
end
%%
% Function to extract the chosen audio features
function features = extractAudioFeatures(audio,sampleRate,chosenFeatures)
% Convert audio to mono if it is stereo
if size(audio,2) > 1
audio = mean(audio,2);
end
% Normalize the audio signal
audio = audio / max(abs(audio));
% Extract the chosen audio features
features = struct();
if ismember('mfcc', chosenFeatures)
mfccCoeffs = mfcc(audio,sampleRate);
features.mfcc = mean(mfccCoeffs,2); % Take the mean across time
end
if ismember('spectralCentroid',chosenFeatures)
spectralCentroid = spectralCentroid(audio,sampleRate);
features.spectralCentroid = mean(spectralCentroid); %single value
end
if ismember('spectralRolloff = mean(spectralRolloff', chosenFeatures)
spectralRolloff = spectralRolloffPoint(audio, sampleRate);
features.spectralRolloff = mean(spectralRolloff); %single value
end
% Add more feature extractions as needed later
end
Answers (1)
Star Strider
on 24 Sep 2023
0 votes
The spectralCentroid function was introduced in R2019a. You need to have that or a later release to be able to use it.
6 Comments
Rita Campos
on 1 Oct 2023
Edited: Rita Campos
on 1 Oct 2023
Star Strider
on 1 Oct 2023
I assume that you have the Audio Toolbox licensed and installed.
If you do, and you are still having the same problem, run these from a script or your Command Window:
restoredefaultpath
rehash toolboxcache
.
Rita Campos
on 1 Oct 2023
Star Strider
on 1 Oct 2023
My pleasure!
Rita Campos
on 1 Oct 2023
Star Strider
on 1 Oct 2023
I do not have any experience with the Audio Toolbox, or its functions.
Categories
Find more on Subspace Methods 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!