Dawn@4: function [] = SVM_Singing( inputFileName ) Dawn@4: Dawn@4: cd 'C:\Users\dawn\Dropbox\TestResults' Dawn@4: Dawn@4: DEBUG = 1; Dawn@4: % output results file name Dawn@4: % masterFileOutputID = fopen( 'SVM_Singing_HNR.txt', 'a' ); Dawn@4: outputFileName = ['SVM_' inputFileName]; Dawn@4: masterFileOutputID = fopen( outputFileName, 'a' ); Dawn@4: Dawn@4: % input results file name Dawn@4: % inputFileName = 'singingBasicHNRStats.txt'; Dawn@4: Dawn@4: fprintf( masterFileOutputID, '\n RESULTS FILE NAME: %s\n', inputFileName); Dawn@4: inputFileID = fopen( inputFileName ); Dawn@4: Dawn@4: % noOfArguments = length(varargin); Dawn@4: Dawn@4: % outputFileName = 'individualResults/SVM_Results_'; Dawn@4: % resultsFileName = 'SVM_Results_'; Dawn@4: titleName = ''; Dawn@4: % for i=1 : noOfArguments Dawn@4: % titleName = [ titleName varargin{i} '_']; Dawn@4: % fprintf( masterFileOutputID, '%s_', varargin{i} ); Dawn@4: % end Dawn@4: Dawn@4: % outputFileName = [ outputFileName titleName ]; Dawn@4: % resultsFileName = [ resultsFileName titleName ]; Dawn@4: Dawn@4: fprintf( masterFileOutputID, '\t' ); Dawn@4: Dawn@4: % outputFileName = [ outputFileName '.txt']; Dawn@4: % resultsFileName = [ resultsFileName '.txt']; Dawn@4: % Dawn@4: % fileOutputID = fopen( outputFileName, 'w' ); Dawn@4: % fileSVMOutputID = fopen( resultsFileName, 'w' ); Dawn@4: Dawn@4: % -------------------- get the data from the results file --------------- Dawn@4: lineCount = 0; Dawn@4: fileCount = 0; Dawn@4: data = []; Dawn@4: groups = []; Dawn@4: Dawn@4: while( ~(feof(inputFileID)) ) Dawn@4: Dawn@4: outputValues = []; Dawn@4: thestr = fgetl(inputFileID); Dawn@4: fileCount = fileCount + 1; Dawn@4: Dawn@4: % determine whether we have a positive or negative sample Dawn@4: sampleEmotion( fileCount ) = 'U'; Dawn@4: if( ~(isempty(strfind(thestr,'pos')))) Dawn@4: % sample is positive Dawn@4: sampleEmotion( fileCount ) = 'P'; Dawn@4: groups( fileCount ) = 1; Dawn@4: elseif( ~(isempty(strfind(thestr,'neg')))) Dawn@4: % sample is negative Dawn@4: sampleEmotion( fileCount ) = 'N'; Dawn@4: groups( fileCount ) = 0; Dawn@4: else Dawn@4: disp('EEEK!'); Dawn@4: fileCount = fileCount - 1; Dawn@4: % pause; Dawn@4: end Dawn@4: Dawn@4: % determine whether we have a male, female or trans sample Dawn@4: % gender( fileCount ) = '?'; Dawn@4: % if( ~(isempty(strfind(thestr,'fem')))) Dawn@4: % % gender is female Dawn@4: % gender( fileCount ) = 'F'; Dawn@4: % elseif( ~(isempty(strfind(thestr,'male')))) Dawn@4: % % gender is male Dawn@4: % gender( fileCount ) = 'M'; Dawn@4: % elseif( ~(isempty(strfind(thestr,'trans')))) Dawn@4: % % gender is trans Dawn@4: % gender( fileCount ) = 'T'; Dawn@4: % else Dawn@4: % disp('EEEK!'); Dawn@4: % pause; Dawn@4: % end Dawn@4: Dawn@4: if(( ~(isempty(strfind(thestr,'pos')))) || ( ~(isempty(strfind(thestr,'neg')))) ) Dawn@4: %how many values are in the string? Dawn@4: % spaces = strfind( thestr, ' ' ); Dawn@4: spaces = [ strfind( thestr, sprintf('\t')) strfind( thestr, ' ' )]; Dawn@4: numberstr = thestr( spaces(1) : end ); % chop off the file name Dawn@4: vars = sscanf( numberstr, '%f', inf ); Dawn@4: data( fileCount, : ) = vars; Dawn@4: end Dawn@4: Dawn@4: lineCount = lineCount + 1; Dawn@4: Dawn@4: end Dawn@4: fclose(inputFileID); Dawn@4: Dawn@4: % ------------ apply the SVM classifier ------------------------ Dawn@4: Dawn@4: resultMatrix = []; Dawn@4: Dawn@4: noOfIterations = 100; Dawn@4: Dawn@4: for n = 1:noOfIterations Dawn@4: % Randomly select training and test sets, perhaps we should try all and Dawn@4: % choose the best? Dawn@4: [train, test] = crossvalind('holdOut',groups); Dawn@4: cp = classperf(groups); Dawn@4: Dawn@4: % Use a linear support vector machine classifier Dawn@4: svmStruct = svmtrain(data(train,:),groups(train)); Dawn@4: classes = svmclassify(svmStruct,data(test,:)); Dawn@4: % See how well the classifier performed Dawn@4: classperf(cp,classes,test); Dawn@4: numbers = cp.CountingMatrix; Dawn@4: Dawn@4: resultMatrix (n,:,:) = cp.DiagnosticTable; Dawn@4: % Dawn@4: end Dawn@4: Dawn@4: Dawn@4: Dawn@4: Dawn@4: % for emotion detection give the confusion matrix as Dawn@4: % ----------------------------------------------------------------- Dawn@4: % positive correctly identified | positive incorrectly identified (1,2) Dawn@4: % negative incorrectly identified (2,1) | negative correctly identified Dawn@4: % ------------------------------------------------------------------ Dawn@4: Dawn@4: % takes the average of 100 iterations - do we want to take the best? Dawn@4: Dawn@4: meanResults(1,1) = mean( resultMatrix(:,1,1) ); Dawn@4: meanResults(1,2) = mean( resultMatrix(:,2,1) ); Dawn@4: meanResults(2,1) = mean( resultMatrix(:,1,2) ); Dawn@4: meanResults(2,2) = mean( resultMatrix(:,2,2) ); Dawn@4: Dawn@4: meanResults(3,:)=0; Dawn@4: meanResults(:,3)=0; Dawn@4: Dawn@4: meanResults(3,3) = (meanResults(1,1) + meanResults(2,2));% / sum(sum(meanResults)); Dawn@4: Dawn@4: % convert to percentages Dawn@4: % how many of each sample do we have? Dawn@4: groupNumbers = unique( groups( test )); Dawn@4: groupNames = unique( sampleEmotion( test )); Dawn@4: sampleEmotionTest = sampleEmotion( test ); Dawn@4: % which group is which emotion? Dawn@4: thisGroupNumber = groupNumbers(1); Dawn@4: thisGroup = find( groups( test ) == thisGroupNumber ); Dawn@4: thisGroupName = unique( sampleEmotionTest( thisGroup )); Dawn@4: Dawn@4: thatGroupNumber = groupNumbers(2); Dawn@4: thatGroup = find( groups( test ) == thatGroupNumber ); Dawn@4: thatGroupName = unique( sampleEmotionTest( thatGroup )); Dawn@4: Dawn@4: if(length( thisGroupName ) ~= 1 ) Dawn@4: disp('ARGH!'); Dawn@4: pause; Dawn@4: end Dawn@4: Dawn@4: thisGroupNumberOfSamples = length( thisGroup ); Dawn@4: thatGroupNumberOfSamples = length( thatGroup ); Dawn@4: Dawn@4: if( thisGroupName == 'P' ) Dawn@4: %swap all the variables ready for checking Dawn@4: temp = thisGroupNumberOfSamples; Dawn@4: thisGroupNumberOfSamples = thatGroupNumberOfSamples; Dawn@4: thatGroupNumberOfSamples = temp; Dawn@4: Dawn@4: temp = thisGroupName; Dawn@4: thisGroupName = thatGroupName Dawn@4: thatGroupName = temp; Dawn@4: disp('CHECK ME!'); Dawn@4: end Dawn@4: Dawn@4: if( thisGroupName == 'N' ) Dawn@4: % group 0 is negative Dawn@4: if( sum( meanResults(1,:) ) == thisGroupNumberOfSamples ) Dawn@4: %if the elements in the first row add up to the number of negative Dawn@4: %samples, then swap the rows because we want the top row to be the Dawn@4: %results for the positive samples Dawn@4: temp(:,1) = meanResults(1:2,2); Dawn@4: temp(:,2) = meanResults(1:2,1); Dawn@4: temp2(1,:) = temp(2,:); Dawn@4: temp2(2,:) = temp(1,:); Dawn@4: Dawn@4: meanResults(1:2,1) = temp2(:,1); Dawn@4: meanResults(1:2,2) = temp2(:,2); Dawn@4: Dawn@4: % check the number of positive samples Dawn@4: if(( sum( meanResults(1,:) ) == thatGroupNumberOfSamples ) ... Dawn@4: && ( thatGroupName == 'P' ) ) Dawn@4: % row 1 is positive Dawn@4: disp('matrix correct'); Dawn@4: else Dawn@4: disp('ARGH!'); Dawn@4: pause; Dawn@4: end Dawn@4: Dawn@4: elseif( sum( meanResults(2,:) ) == thisGroupNumberOfSamples ) Dawn@4: Dawn@4: % the elements in the second row add up to the number of negative Dawn@4: % samples, so the matrix is the correct way around Dawn@4: Dawn@4: % check the number of positive samples Dawn@4: if(( sum( meanResults(1,:) ) == thatGroupNumberOfSamples ) ... Dawn@4: && ( thatGroupName == 'P' ) ) Dawn@4: % row 0 is positive Dawn@4: disp('matrix correct'); Dawn@4: else Dawn@4: disp('ARGH!'); Dawn@4: pause; Dawn@4: end Dawn@4: end Dawn@4: end Dawn@4: Dawn@4: % calculate the percentages Dawn@4: numberOfSamples = sum(sum( meanResults(1:2,1:2))); Dawn@4: percentageResults = meanResults; Dawn@4: percentageResults(1,1) = meanResults(1,1) / numberOfSamples; Dawn@4: percentageResults(1,2) = meanResults(1,2) / numberOfSamples; Dawn@4: percentageResults(2,1) = meanResults(2,1) / numberOfSamples; Dawn@4: percentageResults(2,2) = meanResults(2,2) / numberOfSamples; Dawn@4: percentageResults(3,3) = meanResults(3,3) / numberOfSamples; Dawn@4: Dawn@4: percentageResults = percentageResults * 100 Dawn@4: Dawn@4: confusionMatrix = percentageResults; Dawn@4: fprintf( masterFileOutputID, '\n %f \t %f \n %f \t %f \n %f \t %f \t %f \n', confusionMatrix(1,1), confusionMatrix(1,2), confusionMatrix(2,1), confusionMatrix(2,2), 0, 0, confusionMatrix(3,3)); Dawn@4: Dawn@4: % print latex results to the screen Dawn@4: str1 = sprintf(' & %2.2f & %2.2f & \\\\', percentageResults(1,1), percentageResults(1,2) ); Dawn@4: disp(str1); Dawn@4: str1 = sprintf(' & %2.2f & %2.2f & \\\\', percentageResults(2,1), percentageResults(2,2) ); Dawn@4: disp(str1); Dawn@4: str1 = sprintf(' & & & %2.2f \\\\',percentageResults(3,3) ); Dawn@4: disp(str1); Dawn@4: Dawn@4: fprintf( masterFileOutputID, '\n' ); Dawn@4: fclose( masterFileOutputID ); Dawn@4: Dawn@4: end Dawn@4: Dawn@4: