Audio sample path no valid
1 次查看(过去 30 天)
显示 更早的评论
function kNN_model_add_class(modelName, className, classPath, ...
listOfStatistics, stWin, stStep, mtWin, mtStep)
%
% function kNN_model_add_class(modelName, className, classPath, ...
% listOfStatistics, stWin, stStep, mtWin, mtStep)
%
% This function adds an audio class to the kNN classification model
%
% ARGUMENTS;
% - modelName: the filename of the model (mat file)
% - className: the name of the audio class to be added to the model
% - classPath: the path of the directory where the audio segments of the
% new class are stored
% - listOfStatistics: list of mid-term statistics (cell array)
% - stWin, stStep: short-term window size and step
% - mtWin, mtStep: mid-term window size and step
%
% Example:
% kNN_model_add_class('modelSpeech.mat', 'speech', './Music/', ...
% {'mean','std',}, 0.050, 0.025, 2.0, 1.0);
%
if ~exist(classPath,'dir')
error('Audio sample path is not valid!');
else
classPath = [classPath filesep];
end
% check if the model elaready exists:
fp = fopen(modelName, 'r');
if fp>0 % check if file already exists
load(modelName);
end
% Feature extraction:
D = dir([classPath '*.wav']);
F = [];
for (i=1:length(D)) % for each wav file in the given path:
curFileName = [classPath D(i).name];
FileNamesTemp{i} = curFileName;
% mid-term feature extraction for each wav file:
midFeatures = featureExtractionFile(curFileName, ...
stWin, stStep, mtWin, mtStep, listOfStatistics);
% long-term averaging:
longFeatures = mean(midFeatures,2);
F = [F longFeatures];
end
% save the model:
Statistics = listOfStatistics;
fp = fopen(modelName, 'r');
if fp<0 % model does not exist --> generate
ClassNames{1} = className;
Features{1} = F;
FileNames{1} = FileNamesTemp;
save(modelName, 'ClassNames', 'Features', ...
'Statistics', 'stWin', 'stStep', 'mtWin', 'mtStep', 'FileNames');
else
load(modelName);
ClassNames{end+1} = className;
Features{end+1} = F;
FileNames{end+1} = FileNamesTemp;
save(modelName, 'ClassNames', 'Features', ...
'Statistics', 'stWin', 'stStep', 'mtWin', 'mtStep', 'FileNames');
end
I have this function and when i try to call it with command
strDir = 'C:\Users\User\Desktop\3rd year\bachelor thesis\cats_dogs\train';
Statistics = {'mean', 'median', 'std', 'stdbymean', 'max', 'min'};
stWin = 0.040;
stStep = 0.040;
mtWin = 2;
mtStep = 1;
kNN_model_add_class('model8.mat', 'dog', [strDir '.dog/'], ...
Statistics, stWin, stStep, mtWin, mtStep);
It gives me an error... probably a path of srtDir is incorrectly written. Can you please help me.
Example of a code call would be great
1 个评论
Walter Roberson
2020-11-3
does
C:\Users\User\Desktop\3rd year\bachelor thesis\cats_dogs\train.dog
exist as a directory?
采纳的回答
Kamil Kacer
2020-11-4
10 个评论
Walter Roberson
2020-11-5
function kNN_model_add_class(modelName, className, classPath, ...
listOfStatistics, stWin, stStep, mtWin, mtStep)
%
% function kNN_model_add_class(modelName, className, classPath, ...
% listOfStatistics, stWin, stStep, mtWin, mtStep)
%
% This function adds an audio class to the kNN classification model
%
% ARGUMENTS;
% - modelName: the filename of the model (mat file)
% - className: the name of the audio class to be added to the model
% - classPath: the path of the directory where the audio segments of the
% new class are stored
% - listOfStatistics: list of mid-term statistics (cell array)
% - stWin, stStep: short-term window size and step
% - mtWin, mtStep: mid-term window size and step
%
% Example:
% kNN_model_add_class('modelSpeech.mat', 'speech', './Music/', ...
% {'mean','std',}, 0.050, 0.025, 2.0, 1.0);
%
if ~exist(classPath,'dir')
error('Audio sample path is not valid!');
else
classPath = [classPath filesep];
end
% check if the model elaready exists:
fp = fopen(modelName, 'r');
if fp>0 % check if file already exists
load(modelName);
end
% Feature extraction:
D = dir([classPath '*.wav']);
F = [];
for (i=1:length(D)) % for each wav file in the given path:
curFileName = [classPath D(i).name];
FileNamesTemp{i} = curFileName;
[data, fs] = audioread(curFileName); %NEW
signal = struct('Filt_data', data, 'SampleRate', fs); %NEW
% mid-term feature extraction for each wav file:
midFeatures = featureExtractionFile(signal, ... %CHANGED
stWin, stStep, mtWin, mtStep, listOfStatistics);
% long-term averaging:
longFeatures = mean(midFeatures,2);
F = [F longFeatures];
end
% save the model:
Statistics = listOfStatistics;
fp = fopen(modelName, 'r');
if fp<0 % model does not exist --> generate
ClassNames{1} = className;
Features{1} = F;
FileNames{1} = FileNamesTemp;
save(modelName, 'ClassNames', 'Features', ...
'Statistics', 'stWin', 'stStep', 'mtWin', 'mtStep', 'FileNames');
else
load(modelName);
ClassNames{end+1} = className;
Features{end+1} = F;
FileNames{end+1} = FileNamesTemp;
save(modelName, 'ClassNames', 'Features', ...
'Statistics', 'stWin', 'stStep', 'mtWin', 'mtStep', 'FileNames');
end
更多回答(3 个)
Walter Roberson
2020-11-3
replace
[strDir '.dog/']
with
fullfile(strDir, 'dog')
1 个评论
Walter Roberson
2020-11-4
Your original code had
strDir = 'C:\Users\User\Desktop\3rd year\bachelor thesis\cats_dogs\train';
and I suggested you convert
kNN_model_add_class('model8.mat', 'dog', [strDir '.dog/'], ...
Statistics, stWin, stStep, mtWin, mtStep);
to
kNN_model_add_class('model8.mat', 'dog', fullfile(strDir, 'dog'), ...
Statistics, stWin, stStep, mtWin, mtStep);
You did convert the call, but you also changed to
strDir = 'C:\Users\User\Desktop\3rd year\bachelor thesis\cats_dogs\train\dogs';
You need to decide whether your strDir is the complete path to your training class (like the way you just changed it to) or if it is the path to the directory that contains your classes (like you had originally.)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Audio I/O and Waveform Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!