extract audio features from music
5 views (last 30 days)
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
0 Comments
Answers (1)
Star Strider
on 24 Sep 2023
The spectralCentroid function was introduced in R2019a. You need to have that or a later release to be able to use it.
6 Comments
See Also
Categories
Find more on Audio and Video Data 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!