How can I read .ogg datasets and apply LSTM classification (my code included)?

2 views (last 30 days)
I need to read .ogg audio files (datasets) in Matlab, get its size for crreating array and and apply LSTM classification. I have 2 classes normal and anomalies. Could you give me a code to be able to read audio files and do classification? You can separate our data into three parts. For example, 80% of all normal and anomaly signals for training (2 classes), 10% for validation, and 10% for testing.
You can create sample sounds .ogg files for your code. And the code shall be able to read different audio datasets.
I have used the following code but need advise for me proper modification:
normal = zeros(100,1000); %return matrix
anomaly = zeros(100,1000);
%assuming you have 100 audio files of 1000 samples each, for both normal and anomaly classes,
%you can use a loop to read the files and split the data for training / Validation / Testing.
for i = 1:100
normal_name = strcat('normal_',num2str(i),'.ogg'); %preserves them in cell arrays
anomoly_name = strcat('anomaly_',num2str(i),'.ogg');
%files named for anomaly_0 to anomaly_100
normal(i) = audioread(normal_name);
anomaly(i) = audioread(anomaly_name);
end
%The above arrays can be split for training/Validation/Testing data set
%Normal=NNoice
%Anomaly=ANoice
%NNoise = 2*rand([100,1000]) - 1; %changed N
NLabels = repelem(categorical("normal"),100,1);
ANoise = filter(1,[1,-0.999],NNoise);
ANoise = ANoise./max(abs(ANoise),[],'all');
ALabels = repelem(categorical("anomaly"),100,1);
%pNoise = pinknoise([N,1000]);n%removed pink noices
%Labels = repelem(categorical("pink"),1000,1)
sound(NNoise(:,1),fs)
melSpectrogram(NNoise(:,1),fs)
title('Normal')
sound(ANoise(:,1),fs)
melSpectrogram(ANoise(:,1),fs)
title('Anomaly')
%sound(pNoise(:,1),fs)
%melSpectrogram(pNoise(:,1),fs)
%title('Pink Noise')
featuresTrain = extract(aFE,audioTrain);
[numHopsPerSequence,numFeatures,numSignals] = size(featuresTrain)
audioTrain = [NNoise(:,1:80),ANoise(:,1:80)];%change
labelsTrain = [ALabels(1:80);NLabels(1:80)];
audioValidation = [NNoise(:,81:end),ANoise(:,81:end)]; %changed according 100 samples
labelsValidation = [NLabels(81:end);ALabels(81:end)];
aFE = audioFeatureExtractor("SampleRate",fs, ...
"SpectralDescriptorInput","melSpectrum", ...
"spectralCentroid",true, ...
"spectralSlope",true);
featuresTrain = permute(featuresTrain,[2,1,3]);
featuresTrain = squeeze(num2cell(featuresTrain,[1,2]));
numSignals = numel(featuresTrain)
[numFeatures,numHopsPerSequence] = size(featuresTrain{1})
featuresValidation = extract(aFE,audioValidation);
featuresValidation = permute(featuresValidation,[2,1,3]);
featuresValidation = squeeze(num2cell(featuresValidation,[1,2]));
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(50,"OutputMode","last")
fullyConnectedLayer(numel(unique(labelsTrain)))
softmaxLayer
classificationLayer];
options = trainingOptions("adam", ...
"Shuffle","every-epoch", ...
"ValidationData",{featuresValidation,labelsValidation}, ...
"Plots","training-progress", ...
"Verbose",false);
net = trainNetwork(featuresTrain,labelsTrain,layers,options);
NNoiseTest = 2*rand([100,1]) - 1;
classify(net,extract(aFE,NNoiseTest)')
ANoiseTest = filter(1,[1,-0.999],ANoiseTest);
ANoiseTest= ANoiseTest./max(abs(ANoiseTest),[],'all');
classify(net,extract(aFE,ANoiseTest)')
%pNoiseTest = pinknoise(N);
%classify(net,extract(aFE,pNoiseTest)')

Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!