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 Kommentar

does
C:\Users\User\Desktop\3rd year\bachelor thesis\cats_dogs\train.dog
exist as a directory?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Kamil Kacer
Kamil Kacer am 4 Nov. 2020

0 Stimmen

Now I get this error I am assuming i need to pass a struct to the function featureExtractionFile but I dont know how
so it can go throught every audio in directory.
Example of call would be appreciative
function [midFeatures, Centers, stFeaturesPerSegment] = ...
featureExtractionFile(signal, stWin, stStep, mtWin, mtStep, featureStatistics)
% function [midFeatures, Centers, stFeaturesPerSegment] = ...
% featureExtractionFile(fileName, stWin, stStep, mtWin, mtStep, ...
% featureStatistics)
%
% [mtFeatures, centers] = featureExtractionFile(fileName, ...
% 0.040, 0.040, 2.0, 1.0, {'mean','std'});
%
% This function reads a struct element and computes
% audio feature statitstics on a mid-term basis.
%
% ARGUMENTS:
% - signal: audio signal (struct)
% - stWin: short-term window size (in seconds)
% - stStep: short-term window step (in seconds)
% - mtWin: mid-term window size (in seconds)
% - mtStep: mid-term window step (in seconds)
% - featureStatistics: list of statistics to be computed (cell array)
%
% RETURNS
% - midFeatures [numOfFeatures x numOfMidTermWins] matrix
% (each collumn represents a mid-term feature vector)
% - Centers: representive centers for each
% mid-term window (in seconds)
% - stFeaturesPerSegment cell that contains short-term feature sequences
%
% (c) 2014 T. Giannakopoulos, A. Pikrakis
% convert mt win and step to ratio (compared to the short-term):
mtWinRatio = round(mtWin / stStep);
mtStepRatio = round(mtStep / stStep);
readBlockSize = 60; % one minute block size:
% get the length of the audio signal to be analyzed:
% ndret til struct brug!
a = signal.Filt_data;
fs = signal.SampleRate;
numOfSamples = length(a);
BLOCK_SIZE = round(readBlockSize * fs); % Antal samples per minut
curSample = 1;
count = 0;
midFeatures = [];
Centers = [];
stFeaturesPerSegment = {};
while (curSample <= numOfSamples) % while the end of file has not been reahed
% find limits of current block:
N1 = curSample;
N2 = curSample + BLOCK_SIZE - 1;
if (N2>numOfSamples)
N2 = numOfSamples;
end
tempX = signal.Filt_data(N1:N2,:); % ndret til struct brug!
% STEP 1: short-term feature extraction:
Features = stFeatureExtraction(tempX, fs, stWin, stStep);
% STEP 2: mid-term feature extraction:
[mtFeatures, st] = mtFeatureExtraction(...
Features, mtWinRatio, mtStepRatio, featureStatistics);
for (i=1:length(st))
stFeaturesPerSegment{end+1} = st{i};
end
Centers = [Centers readBlockSize * count + (0:mtStep:(N2-N1)/fs)];
midFeatures = [midFeatures mtFeatures];
% update counter:
curSample = curSample + BLOCK_SIZE;
count = count + 1;
end
if (length(Centers)==1)
Centers = (numOfSamples / fs) / 2;
else
C1 = Centers(1:end-1);
C2 = Centers(2:end);
Centers = (C1+C2) / 2;
end
if (size(midFeatures,2)>length(Centers))
midFeatures = midFeatures(:, 1:length(Centers));
end
if (size(midFeatures,2)<length(Centers))
Centers = Centers(:, 1:size(midFeatures,2));
end

10 Kommentare

Kamil Kacer
Kamil Kacer am 4 Nov. 2020
@Walter Roberson can u pleae help me it is really inportant
Walter Roberson
Walter Roberson am 4 Nov. 2020
Go back to the original featureExtractionFile -- the one that you pass a file name in and it reads the data from the file.
Change
midFeatures = featureExtractionFile(curFileName, ...
stWin, stStep, mtWin, mtStep, listOfStatistics);
to
[data, fs] = audioread(curFileName);
signal = struct('Filt_data', data, 'SampleRate', fs);
midFeatures = featureExtractionFile(signal, ...
stWin, stStep, mtWin, mtStep, listOfStatistics);
Kamil Kacer
Kamil Kacer am 5 Nov. 2020
Please what exactly do i put in curFilename a tried the one in the picture i also put the exact wav. name of a file inside of a folder dogs it worked but then the function kNN_model_add_class didnt
I assume it didnt work because it need all file inside of a folder 'dogs'.
Can u please give me an example what exactly should i put instead of curFileName.
Thanks
strDir = 'C:\Users\User\Desktop\3rd year\bachelor thesis\cats_dogs\train';
Kamil Kacer
Kamil Kacer am 5 Nov. 2020
This is the path to the folder
I just dont know how to call [data, fs] = audioread(curFileName); => what do i put instead of curFileName.
And how do I call function kNN_model_add_class
You have
for (i=1:length(D)) % for each wav file in the given path:
curFileName = [classPath D(i).name];
that is the curFileName you should be passing in.
C:\Users\User\Desktop\3rd year\bachelor thesis\cats_dogs\dogs
You cannot keep moving files around and expect me to somehow know! You clearly showed
C:\Users\User\Desktop\3rd year\bachelor thesis\cats_dogs\train\dogs
The directory name you should be putting into strDir is the full path to the folder that your dog_barking_0.wav file is in,
Iam sorry Iam just so confused.. I changed my directory to this 'dogs' that where all the files are.' as you said
strDir = 'C:\Users\User\Desktop\3rd year\bachelor thesis\cats_dogs\dogs';
Then i tried to call this function but obviously it doesnt work
[data, fs] = audioread(curFileName);
Can u please give me an example of calling this function
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
Kamil Kacer
Kamil Kacer am 5 Nov. 2020
I just got it thank you so much

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Kamil Kacer
Kamil Kacer am 3 Nov. 2020

0 Stimmen

Yes this is the directory.
And also please how do i initialize or call featureExtractionFile function where i want to do audiowav by audioway as you see at the picture dog_barking0 dog_barking_1 etc
Walter Roberson
Walter Roberson am 3 Nov. 2020

0 Stimmen

replace
[strDir '.dog/']
with
fullfile(strDir, 'dog')

1 Kommentar

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.)

Melden Sie sich an, um zu kommentieren.

Kamil Kacer
Kamil Kacer am 3 Nov. 2020

0 Stimmen

I replaced it but it still gives me an error

Kategorien

Mehr zu Code Generation and Deployment finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by