Tuesday, June 12, 2012

recognition real code


recognition real code

Code for speaker recognition system:
clear all;
close all;
%The First screen when program runs
FlagModi = 0; % test flag
% Gets the screen size in a matrix that has
% 1. X axis pixel value of the lower left corner
% 2. Y axis pixel value of the lower left corner
% 3. The pixel width of the screens X axis
% 4. The pixel height of the screens Y axis
% In mat lab the size of GUI objects is determined by the four factors
% describes above
pos = get(0,'ScreenSize');
% colors
% The background color variable (contains values in RGB)
Bg_Color = [0.7 0.7 0.8];
% The frame color variable (Contains values in RGB)
Frame_Color2 = [0.9 0.8 0.8];
Frame_Color = [0.8 0.8 0.9];
% The button colot variable (contains values in RGB)
Button_Color1 = [0.7 0.7 0.7];
% main buttons size
% The pixel width of the buttons
button_width = 160;
% The pixel height of the buttons
button_height = 40;
% main window size
% The pixel width of the main window
main_width = 1004;
% The pixel height of the main window
main_height = 688;
% The pixel position of the lower left corner
button_leftX = ((main_width-10)-button_width)/2;
% title position
% The Width of the main title in pixels
title_width = 560;
% The pixel position of the lower left corner
title_leftX = (main_width-title_width)/2;
% The Y pixel of the left bottom corner
title_leftY = 200;
title_height = 100;
% main window
% The main figure window comes using the figure command, the different
% values of the different properties can be adjusted by writing the
% property name in the figure and next the value. If nothing is mentioned
% the default values are taken. See help figure for further assistance
% The hfig is the handle to the figure, it can be said as the reference
% number that points to the figure
% handle = figure (the function name that creates figure) ('Property', 'its value' )
hfig = figure('Name','Speaker Recognition System',...% The name of the figurefigure title
'NumberTitle','on',...% The title appearing on the upper left corner of a window
'Resize','on',...% The resizing option
'Visible','on',...% The visibility of the figure
'Position',[pos(3)/2-main_width/2 pos(4)/2-main_height/2-5 main_width main_height+20],...% The position of the figure
'MenuBar','none', ...% Menu bar
'Color', Bg_Color);% The back ground color
% The text displaying the main title,
% uicontrol is the GUI making function of the matlab, depending on the
% style of the uicontrol it can be a edit box, a text box, a button, a drop
% down etc etc. The Title is the reference number to the figure. The
% properties of the uicontrol can be adjusted or changed just as that of
% the figure.
Title = uicontrol(hfig,...
'style','text',...
'position',[title_leftX title_leftY+350 title_width title_height],... % [from_left from_bottom width height]
'BackgroundColor',Bg_Color,...
'Fontweight','bold',...
'FontSize',20,...
'string',['Speaker Recognition System' ...
, '']);
% The control option is given using the radio buttons
% The option is between the MFCC based code and Modified MFCC based
% approach
ValueButton = 1;% The value of the selection, 1 means MFCC, 0 means modified MFCC
HRadio1 = uicontrol(hfig,...
'style','radiobutton',...
'position',[title_leftX title_leftY+250 title_width title_height],... % [from_left from_bottom width height]
'BackgroundColor',Bg_Color,...
'Fontweight','bold',...
'FontSize',12,...
'string', 'MFCC system', ...
'selected', 'off', ...
'callback', 'ToggleRadioButton1', ...
'value', 1);
HRadio2 = uicontrol(hfig,...
'style','radiobutton',...
'position',[title_leftX title_leftY+170 title_width title_height],... % [from_left from_bottom width height]
'BackgroundColor',Bg_Color,...
'Fontweight','bold',...
'FontSize',12,...
'string', 'Modified MFCC system', ...
'selected', 'off', ...
'callback', 'ToggleRadioButton2');
% The Push button for continue
HButton1=uicontrol(hfig, ...
'style','push', ...
'position',[button_leftX-100 title_leftY-150 button_width button_height], ...
'string', 'CONTINUE',...
'callback','main');% We mention the name of the m file in the callback as a string so that the file starts as the button is pressed
% The Push button for exit or closing the window
Hbutton2=uicontrol(hfig, ...
'style','push', ...
'position',[button_leftX+100 title_leftY-150 button_width button_height], ...
'string', 'Exit',...
'callback','close');% close means close the current figure.
% The result file
% The variable MFCC stores the result for MFCC algo the current session, it is
% initialized by an empty array at the start
MFCC_Result = [];
% The variable modified MFCC stores the result for modified MFCC algo the current session, it is
% initialized by an empty array at the start
modiMFCC_Result = [];
% The save command saves the variables to the test.mat file
save result MFCC_Result modiMFCC_Result;
*************************** TOGGLE RADIOBUTTON1**************************
if(get(HRadio2, 'value') == 1 & get(HRadio1, 'value') == 1)
set(HRadio2, 'value', 0);
end
% Get the value of the radio button for the selection of the corresponding
% code, MFCC or modified MFCC
ValueButton = get(HRadio1, 'value');
***************************TOGGLE RADIOBUTTON2**************************
if(get(HRadio2, 'value') == 1 & get(HRadio1, 'value') == 1)
set(HRadio2, 'value', 0);
end
% Get the value of the radio button for the selection of the corresponding
% code, MFCC or modified MFCC
ValueButton = get(HRadio1, 'value');
********************************THE MAIN FUNCTION*************************
% Close the prev figure
close(hfig);
% The dinmensions of the main window
main_width = 804;
% The pixel height of the main window
main_height = 688;
% The Width and height of the main title in pixels
title_width = 560;
title_height = 100;
% title position
title_width1 = 200;
% The X pixel position of the lower left corner
title_left = (main_width-title_width)/2;
% The Y pixel of the left bottom corner
title_bottom = 150;
% The height of the title
title_height1 = 40;
% create the directories in case they donot exist, if they already exists
% then nothing will happen
WarningFlag = mkdir('test');
WarningFlag = mkdir('moditest');
% The Title string based in the selected radio button
if(ValueButton == 1)
TitleString = 'MFCC Based System';
DirName = 'test';
else
TitleString = 'Modified MFCC Based System';
DirName = 'modiTest';
End
% Gets the screen size in a matrix that has
% 1. X axis pixel value of the lower left corner
% 2. Y axis pixel value of the lower left corner
% 3. The pixel width of the screens X axis
% 4. The pixel height of the screens Y axis
% In matlab the size of GUI objects is determined by the four factors
% describes above
pos = get(0,'ScreenSize');
% colors
Bg_Color = [0.7 0.7 0.8];
Bg_Color1 = [0.7 0.7 0.8];
Frame_Color2 = [0.9 0.8 0.8];
Frame_Color = [0.8 0.8 0.9];
Button_Color = [0.5 0.8 0.9];
Bg_Color5 = [0.5 0.8 0.8];
Bg_Color4 = [1 0.40 0.5];
% main buttons size
button_width = 160;
button_height = 40;
buttonWidth = 120;
buttonHeight = 20;
button_left = 100;
% main window
% The main figure window comes using the figure command, the different
% values of the different properties can be adjusted by writing the
% property name in the figure and next the value. If nothing is mentioned
% the default values are taken. See help figure for further assistance
% The hfig is the handle to the figure, it can be said as the reference
% number that points to the figure
% handle = figure (the function name that creates figure) ('Property', 'its value' )
hfig2 = figure('Name','Speaker Recognition System',...% The name of the figurefigure title
'NumberTitle','off',...% The title appearing on the upper left corner of a window
'Resize','on',...% The resizing option
'Visible','on',...% The visibility of the figure
'Position',[pos(3)/2-main_width/2 pos(4)/2-main_height/2-5 main_width main_height+20],...% The position of the figure
'MenuBar','none', ...% Menu bar
'Color', Bg_Color);% The back ground color
% The text displaying the main title,
% uicontrol is the GUI making function of the matlab, depending on the
% style of the uicontrol it can be a edit box, a text box, a button, a drop
% down etc etc. The Title is the reference number to the figure. The
% properties of the uicontrol can be adjusted or changed just as that of
% the figure.
Title = uicontrol(hfig2,...
'style','text',...
'position',[title_left-15 title_bottom+250 title_width title_height],... % [from_left from_bottom width height]
'BackgroundColor',Bg_Color,...
'Fontweight','bold',...
'FontSize',20,...
'string', TitleString);
% Retreiving the file names of the current database files from the system
% The dir command is the DOS based command that reads files from the
% current directory
DirectoryFileList = dir(DirName);
FileNames = char(DirectoryFileList.name);
[rowFN, colFN] = size(FileNames);
if(rowFN <= 2)
FileNamesListBox = [];
else
FileNamesListBox = [FileNames(3:end, :)];
end
TextList = uicontrol(hfig2,...
'style','text',...
'position',[title_left+100 title_bottom+290 buttonWidth buttonHeight],... % [from_left from_bottom width height]
'BackgroundColor',Bg_Color,...
'FontSize',12,...
'string', 'The DataBase Files');
% The listbox for the names of the file in the current database
HList = uicontrol(hfig2, ...
'style', 'listbox', ...
'position', [title_left+100 title_bottom+200 title_width1+100 title_height1+50], ...
'string', FileNamesListBox);
% The button to add to the database
HButtonAdd = uicontrol(hfig2, ...
'style', 'push', ...
'position', [title_left+100 title_bottom+150 buttonWidth buttonHeight], ...
'BackgroundColor', Button_Color, ...
'string', 'Add ...', ...
'callback', 'Add2System' ...
);
% The button to remove from the database
HButtonRem = uicontrol(hfig2, ...
'style', 'push', ...
'position', [title_left+100+buttonWidth title_bottom+150 buttonWidth buttonHeight], ...
'BackgroundColor', Button_Color, ...
'string', 'Remove ...', ...
'callback', 'RemoveFromSystem' ...
);
% The button to initiate the recognition system
HButtonRecog = uicontrol(hfig2, ...
'style', 'push', ...
'position', [title_left+100+2*buttonWidth title_bottom+150 buttonWidth buttonHeight], ...
'BackgroundColor', Button_Color, ...
'string', 'Recognize', ...
'callback', 'RecognizeSys1' ...
);
% The Summary button
HButtonSum=uicontrol(hfig2, ...
'style','push', ...
'position',[title_left+100 title_bottom+130 buttonWidth*3 buttonHeight], ...
'BackgroundColor',Button_Color ,...
'string', 'Summary of this session',...
'callback','SummaryGen');
% The EXIT button
HButtonExit=uicontrol(hfig2, ...
'style','push', ...
'position',[title_left+100 title_bottom+110 buttonWidth*3 buttonHeight], ...
'BackgroundColor',Button_Color ,...
'string', 'Exit',...
'callback','close');
% The Title for HELP
HELPTitle = uicontrol(hfig2,...
'style','text',...
'position',[title_left+100 title_bottom-10 title_width1+150 title_height1+80],... % [from_left from_bottom width height]
'BackgroundColor',Bg_Color5,...
'Fontweight','bold',...
'FontSize',10,...
'string',['Press "Add.." to add a speaker. Press "Delete" to remove a speaker from the database. '...
'Press "recognize" to test a speaker, In that case speech would be recorded as soon as the button '...
'is pressed']);
*********************************ADD2 SYSTEM:*****************************
% title position
% The width and height of the title with the (x, y) of the lower left
% corner of the title name are initialized
title_width1 = 200;
title_left = (main_width-title_width)/2 - 100;
title_bottom = 200;
title_height1 = 40;
% Gets the screen size in a matrix that has
% 1. X axis pixel value of the lower left corner
% 2. Y axis pixel value of the lower left corner
% 3. The pixel width of the screens X axis
% 4. The pixel height of the screens Y axis
% In matlab the size of GUI objects is determined by the four factors
% describes above
pos = get(0,'ScreenSize');
% colors
Bg_Color = [0.7 0.7 0.8];
Frame_Color2 = [0.9 0.8 0.8];
Frame_Color = [0.8 0.8 0.9];
Button_Color = [0.5 0.8 0.9];
Bg_Color5 = [0.5 0.8 0.8];
% main buttons size
button_width = 120;
button_height = 20;
main_width = 804;
main_height = 488;
% The X value of the lower left corner of uicontrols
button_left = (((main_width-10)-button_width)/2)-100;
% The figure for the Addtion to database menu
hfig3 = figure('Name','Speaker Recognition System ',...
'NumberTitle','off',...
'Resize','on',...
'Visible','on',...
'Position',[pos(3)/2-main_width/2 pos(4)/2-main_height/2-5 main_width main_height+20],...
'MenuBar','none');
% The title of the current figure
Title = uicontrol(hfig3,...
'style','text',...
'position',[title_left+140 title_bottom+200 title_width1+300 title_height1],... % [from_left from_bottom width height]
'BackgroundColor',Bg_Color,...
'Fontweight','bold',...
'FontSize',20,...
'string',['Add to database',' ']);
% The Text holding the file name
HTextFile=uicontrol(hfig3, ...
'style','text', ...
'position',[button_left-50 title_bottom+90 button_width button_height], ...
'string', 'File Name',...
'callback','');
% The edit box in which file name is to be entered
HEditFile=uicontrol(hfig3, ...
'style','edit', ...
'position',[button_left-50 title_bottom+130 button_width button_height], ...
'string', 'Default',...
'callback','');
% The Button to add to the database file
Hbutton1=uicontrol(hfig3, ...
'style','push', ...
'position',[button_left-50 title_bottom+60 button_width button_height], ...
'BackgroundColor',Button_Color ,...
'string', 'Record File...',...
'callback','add2database');
% The button to exit
Hbutton2=uicontrol(hfig3, ...
'style','push', ...
'position',[button_left-50 title_bottom+30 button_width button_height], ...
'BackgroundColor',Button_Color ,...
'string', 'Exit',...
'callback','close');
% The Text box explaining the prodecure
HelpText = uicontrol(hfig3,...
'style','text',...
'position',[title_left+340 title_bottom title_width1+100 title_height1+120],... % [from_left from_bottom width height]
'BackgroundColor',Bg_Color5,...
'Fontweight','bold',...
'FontSize',10,...
'string',['Modify the Edit box value if required and ' ...
'Press "Record File..." to record a speaker"s voice. Voice'...
'will be recorded just after pressing '...
'and vectors would automatically saved in the database file. ']);
****************************ADD2 DATABASE*********************************
% The script saves the Vectors of the Speech from the algorithm to a file
m=1;%0;
n=5; %0;
No_pts=12948; % No of points to be recorded
Fs=22050; %sampling rate
% get the filename from the edit box
FileName = get(HEditFile, 'string');
% Searches for the . from the filename string and takes only the part
% before it and saves it again to the variable FileName
IndexDot = (FileName == '.')*(1:length(FileName))';
if(IndexDot == 0)
FileName = FileName;
else
FileName = FileName(1:IndexDot-1);
end
% Checks if the No name is given or not
if(isempty(FileName))
FileName = 'Default';
set(HEditFile, 'string', FileName)
end
% Checks that the file starts with a character value
if(double(FileName(1)) >= 65 & double(FileName(1)) <= 122 )
if ispc
% The wavrecord is a windows based function for saving the data from
% the mic to a variable in MATLAB
RawVoice = wavrecord(No_pts*2, Fs);
%RawVoice=wavread('lighton8.wav'); % Converting the speech data to corresponding vector quantized form
codebook = MFCC_BasedCodebook(RawVoice, Fs);
eval(['save ' 'test\' FileName ' codebook;']);
DirName = 'test';
% code for modified MFCC
if(FlagModi == 1)
modicodebook = calcFilterCoeffs(RawVoice);
else
modicodebook = modiMFCC_BasedCodebook(RawVoice, Fs);
end
eval(['save ' 'moditest\' FileName ' modicodebook;']);
else
% no routine found for recording sound file in linux
% Linux routine for wavrecord
% Converting the speech data to corresponding vector quantized form
if(ValueButton == 1)
% Code for MFCC
codebook = MFCC_BasedCodebook(RawVoice, Fs);
eval([FileName ' = codebook']);
eval(['save ' 'test\' FileName ' ' FileName]);
% Code for Modified MFCC
codebookModi = calcFilterCoeffs(RawVoice);
eval([FileName ' = codebookModi']);
eval(['save ' 'moditest\' FileName ' ' FileName]);
end
end
% Retreiving the file names of the current database files from the system
% The dir command is the DOS based command that reads files from the
% current directory, The return type of the dir function is a structure
% containing the file name, last modified date, no of bytes in file and
% flag indicating that the name is a directory or a file
DirectoryFileList = dir(DirName);
% type casting the file names retrevied in a structure to simple array
% of strings
FileNames = char(DirectoryFileList.name);
% The size function returns the row and columns of the input matrix,
% array or vector
[rowFN, colFN] = size(FileNames);
% checks of the size of row is less than or equal to 2 that indicates
% that the folder for which dir is used is empty thus nothing should be
% displayed, Valid file names start from 3rd index
if(rowFN <= 2)
FileNames = [];
else
FileNames = [FileNames(3:end, :)];
end
set(HList, 'string', FileNames);
else
% Display error if the 1st value of the file name is not a valid
% character
msgbox('Please Enter a Valid File Name');
end
*******************************MFCC BASED CODEBOOK**********************
function codebook = MFCC_BasedCodebook(Speech, Fs);
% The function takes the Speech sample and the sampling frequency as input
% and generated the codebook after vector quantization of the mfcc output
% train_dir : string name of directory contains all train sound files
% n : number of train files in traindir
% codebook : trained VQ codebooks, code{i} for i-th speaker
% The number of centeriods
CentN = 16;
% The mfcc algorithm
MFCC_Signal = mfcc(Speech, Fs);
% vector quatization for database
codebook = vector_quant(MFCC_Signal, CentN);
**********************MODIFIED MFCC BASED CODEBOOK********************
function codebook = MFCC_BasedCodebook(Speech, Fs);
% The function takes the Speech sample and the sampling frequency as input
% and generated the codebook after vector quantization of the modified mfcc output
% train_dir : string name of directory contains all train sound files
% n : number of train files in traindir
% codebook : trained VQ codebooks, code{i} for i-th speaker
% The number of centeriods
CentN = 16;
% The MODIFIED mfcc algorithm
MFCC_Signal = modimfcc(Speech, Fs);
% vector quatization for database
codebook = vector_quant(MFCC_Signal, CentN);
***********************************MFCC**********************************
function v= mfcc(s,fs)
% The function computed the mfcc output of the input signal s sampled at fs
% s: No of points
% fs: Sampling rate
N=256; % size of each frame
M=156; % overlap size
nof=40; % number of filters
len=80; % The number of times for loop is to be run
a(1:N,1:len)=0; % framing the signal with overlap
% initialization of the first chunk
a(:,1)=s(1:N);
for j=2:len
% extracts frames from the input speech vector
a(:,j)=s((N-M)*j+1:(N-M)*j+N);
end;
% change 1. kaiser window is used in stead of hamming window
% computes the kaiser window coefficients
h= kaiser(N, 5); % windowing
% applies the hamming window to each frame
for j=1:len;
b(:,j)= a(:,j).* h;
end
% computes the mel filter bank coeffs
m=melfilterbank(nof,N,fs); % normailising to mel freq
% The computation of the cepstrum coefficients
for j=1:len
y(:,j)=fft(b(:,j)); % calculating fft
n2 = 1 + floor(N/2); % adjust the dimensions of the vector y for mel filter banks
% The absolute of the fft is considered instead of computing the square
% of the fft
ms = m * abs(y(1:n2,j)); % applies the mel filter bank
v(:,j)=dct(log(ms)); % converting back to time domain
end
v(1,:)=[];
********************************MELFILTERBANK*****************************
function m = melfilterbank(p, n, fs)
% The function computed the mel filter banks for robust speaker recognition
% The filter spectrum is such that the passband area remains the same yet
% the pasband frequencies decrease and the power increases, to emphasis the
% higher frequency components
% p number of filters in filterbank
% n length of fft
% fs sample rate in Hz
f0 = 700/fs;
fn2 = floor(n/2);
lr = log(1 + 0.5/f0) / (p+1);
% convert to fft bin numbers with 0 for DC term
bl = n * (f0 * (exp([0 1 p p+1] * lr) - 1));
b1 = floor(bl(1)) + 1;
b2 = ceil(bl(2));
b3 = floor(bl(3));
b4 = min(fn2, ceil(bl(4))) - 1;
pf = log(1 + (b1:b4)/n/f0) / lr;
fp = floor(pf);
pm = pf - fp;
r = [fp(b2:b4) 1+fp(1:b3)];
c = [b2:b4 1:b3] + 1;
v = 2 * [1-pm(b2:b4) pm(1:b3)];
m = sparse(r, c, v, p, 1+fn2);
*********************************VECTOR QUANTISATION*********************
function b = vector_quant(v,no_centroids)
% v: speech vectors ,mel scaled.
% no_codebook: no.of centroids reqd. Here k=16.
% b: Codebook generated
%warning off MATLAB:divideByZero
no_update=5 ; %no of updates
c=mean(v,2) ; % finding initial codebook
e = 0.01 ; % splitting parameter
c(:,1)=c(:,1) + c(:,1)*e ; % splitting the codebook into 2.
c(:,2)=c(:,1) - c(:,1)*e;
for up1=1:no_update;
d=euclid_dist(v,c); % calculating the euclidean distance.
[m,id]=min(d,[],2); % finding the minimum distance.
[rows,cols]=size(c);
for j=1:cols;
c(:,j)=mean(v(:,find(id==j)),2); % finding the centroid of the new cluster.
end;
end %update end
n=1;n=n*2;
while cols < style="mso-spacerun:yes"> ; % updating the code book to get the reqd. no.
for i = 1:cols ; % of code vectors.
c(:,i)=c(:,i) + c(:,i)*e;
c(:,i+n)=c(:,i) - c(:,i)*e;
end;
for up2=no_update %update2
d=euclid_dist(v,c); % calculating the euclidean distance
[m,i]=min(d,[],2); % finding the minimum distance
[rows,cols] = size(c);
for j=1:cols;
c(:,j)=mean(v(:,find(i==j)),2); % finding the centroid of the new cluster
end;
end %end update2
n=n*2;
end
b=c;
****************************** EUCLID DISTANCE*****************************
function d = euclid_dist(x,y)
% x, y: Two matrices whose each column is an a vector data.
% d: Element d(i,j) will be the Euclidean distance between two
% column vectors X(:,i) and Y(:,j)
%
% The Euclidean distance D between two vectors X and Y is:
% D = sum((x-y).^2).^0.5
% The dimentions of the matrix x is returned in M,N
[M, N] = size(x);
% The dimensions of the matrix y is returned in M2 and P
[M2, P] = size(y);
d = zeros(N, P);
% Checks if the siz miss matched
if (N <>
copies = zeros(1,P);
% Implements the formula for computing euclidean distanc
for n = 1:N
d(n,:) = sum((x(:, n+copies) - y) .^2, 1);
end
else
copies = zeros(1,N);
% Implements the formula for computing euclidean distanc
for p = 1:P
d(:,p) = sum((x - y(:, p+copies)) .^2, 1)';
end
end
% the final step of computing the eulid distance Z = sqrt(X^2 + Y^2)
d = d.^0.5;
*********************CALCULATING FILTER COEFFICIENTS*********************
function [meanCoeffs, cepLP] = calcFilterCoeffs(speech)
% The function calculates the Linear Predictive filter coefficients of the
% speech input and then computes cepstral derivatives from the computed
% coefficients.
% The size of the frame, the speech would be broken into small frames and
% then LP filter coeffs would be computed for each of the frame, the
% cepltral derivative of the mean of the computed LP coeffs would be
% calculated to save computational cost
chunkSize = 256;
% The total number of chunks to be processed
totalChunks = 35;
% The filter coeffient array initialization
filtCoeffs = [];
for index = 1:totalChunks
% The chunkData is the nth frame depending on the for loop index
chunkData = speech((index-1)*chunkSize + 1: index*chunkSize );
% computed the correlation of the frame and then flips it in order to
% get the r(0) at the beging
corrChunkData = flipud(filter(chunkData, 1, flipud(chunkData)));
% computed the Autocorrelation matrix
corrMatrixChunk = toeplitz(corrChunkData(1:end-1));
% comuted the LP filter coefficents
filtCoeffs(:, index) = inv(corrMatrixChunk)*corrChunkData(2:end);
end
% computed the mean of the coefficients
meanCoeffs = mean(filtCoeffs, 2);
% The initialization of the variable to keep the cepstral coefficients
cepLP = zeros(chunkSize-1, 1);
for index = 1:chunkSize-1
% The algorithm to compute the cepstral coefficient
cepLP(index) = meanCoeffs(index) + (((1:index-1)/index).*(cepLP(1:index-1))')*meanCoeffs(index-1:-1:1);
end
****************************REMOVE FROM SYSTEM**************************
% The current script removes the file from the database
% Retreive the database file names
% The get command is used to get the current property values from an
% object, the values then can be changed using the set command
FileName = get(HList, 'string');
% Get the current selected file index
DeleteIndex = get(HList, 'value');
% Get the current size of the Database, The filename is a string array that
% has the file names of the files is the current database, The size command
% returns the Columns and the Rows of the array of strings returned by get
% command
if(~isempty(FileName))
[indexR, indexC] = size(FileName);
% Remove the index to be deleted using the find command, the find command
% returns the indexes of the values that satisify the logic in the argument
Index = find(1:indexR ~= DeleteIndex);
% delete the files from the database and update the database view in
% the GUI as well
eval(['delete test\' FileName(DeleteIndex, :)]);
eval(['delete moditest\' FileName(DeleteIndex, :)]);
FileName = [FileName(Index, :)];
set(HList, 'string', FileName);
else
msgbox('Error! Cannot remove from an empty database');
end
***************************RECOGNIZE SYSTEM1******************************
% The script converts the current speech file to Vector and compares it
% with the files in the database based on the system choosen.
No_pts=12948; % No of points to be recorded
Fs=22050; %sampling rate
hfig4= figure('Name','Speaker Recognition System ',...
'NumberTitle','off',...
'Resize','on',...
'Visible','on',...
'Position',[pos(3)/2-main_width/2 pos(4)/2-main_height/2-5 main_width main_height+20],...
'MenuBar','none');
Hbutton1=uicontrol(hfig4, ...
'style','push', ...
'position',[button_left-50 title_bottom+60 button_width button_height], ...
'BackgroundColor',Button_Color ,...
'string', 'Record File...');%ows function to record a speech from mic
Speech = wavrecord(No_pts, Fs);
Hbutton2=uicontrol(hfig4, ...
'style','push', ...
'position',[button_left-50 title_bottom+30 button_width button_height], ...
'BackgroundColor',Button_Color ,...
'string', 'Exit',...
'callback','recognise2');
********************************RECOGNIZE2*********************************
% Retrieve the filenames from the database
close(hfig4);
FileNames = dir('test');
FileNames = char(FileNames.name);
[rowFN, colFN] = size(FileNames);
% checks if the database is empty then error message is displayed
if(rowFN <= 2)
FileNames = [];
msgbox('No file in database to compare with');
else
FileNames = [FileNames(3:end, :)];
% calclulation of the time taken for the algorithm
MFCC_Time = cputime;
v = mfcc(Speech, Fs); % Compute MFCC's
% initialization of the minimum distance
distmin = 100;%inf;
% initialization of the index variable
k1 = 0;
for index = 1:rowFN-2
% load the files from the database
eval(['load test\' FileNames(index, :) ';']);
% compute the distance of the currently recorded speech with all
% the files in the database
d = euclid_dist(v, codebook);
% finds the minimum of the distances
dist =sum( min(d,[],2)) / size(d,1);
% checks the minimum distance and marks the index
if dist <>
distmin = dist;
k1 = index;
ClosestMatch = codebook;
end
end
% calclulation of the time taken for the algorithm
MFCC_Time = cputime - MFCC_Time;
end
figure;
plot(v(:, 1), v(:, 2), 'b.'); hold on;
plot(ClosestMatch(:, 1), ClosestMatch(:, 2), 'r+'); grid on;
axis([-16 16 -16 16]);
legend('Caculated Codebook', 'Stored Codebook');
title('The match of the Calculated and the best match stored codebook (MFCC)')
% Retrieve the filenames from the database
FileNames = dir('modiTest');
FileNames = char(FileNames.name);
[rowFN, colFN] = size(FileNames);
if(rowFN <= 2)
FileNames = [];
else
FileNames = [FileNames(3:end, :)];
% calclulation of the time taken for the algorithm
modiMFCC_Time = cputime;
if(FlagModi == 1)
% calculation of the ceptral filter coeffs
testcode = calcFilterCoeffs(Speech);
% the error variance threshold
minErrorVar = 0.01;
minVal = minErrorVar;
else
v = modimfcc(Speech, Fs);
minErrorVar = 100;%inf
minVal = 2.0;%30,%12;
end
indicator = 0;
% Modified MFCC computation
for index = 1:rowFN-2
% load the files from the database
eval(['load moditest\' FileNames(index, :) ';']);
if(FlagModi == 1)
% calculation of teh variace
ErrorVar = var(modicodebook - testcode);
else
% The euclid distance of the current speech with the data in
% the database
d = euclid_dist(v, modicodebook);
ErrorVar =sum( min(d,[],2)) / size(d,1);
end
% marks the index of the recognized file
if(ErrorVar <>
minErrorVar = ErrorVar;
indicator = index;
ClosestMatch = codebook;
end
end
% The computation of the algorithm cpu time
modiMFCC_Time = cputime - modiMFCC_Time;
displayResult; % the result display script
end
figure;
plot(v(:, 1), v(:, 2), 'b.'); hold on;
plot(ClosestMatch(:, 1), ClosestMatch(:, 2), 'r+'); grid on;
axis([-16 16 -16 16]);
legend('Caculated Codebook', 'Stored Codebook');
title('The match of the Calculated and the best match stored codebook (Modified MFCC)')
**************************SUMMARY OF GENERATION*************************
% The script to generate the summary of the current session
% loads the result
load result;
% checks if the result is empty that means no experiment has been done yet
if(~isempty(MFCC_Result))
% plots the CPU time
% the subplot command splits the figure into smaller plots so that more
% than one plot can be created in a figure
figure, subplot(211);
% The bar displayes the bar graph
bar([MFCC_Time modiMFCC_Time]);
title('CPU time taken by the two processes');
ylabel('CPU Time');
xlabel('MFCC based algo modified MFCC based algo');
% plots the %age accuracy of both the systems
subplot(212);
bar(100*[sum(MFCC_Result) sum(modiMFCC_Result)]/length(modiMFCC_Result));
title('Accuracy %age of the two systems');
ylabel('% age accuracy');
xlabel('MFCC based algo modified MFCC based algo');
else
msgbox('No experiment done for this session');
end
*****************************DISPLAYS RESULT*******************************
% The script displays the result of the current session with the check
% boxes to mark whether the results are correct or not for analysis
% purposes
% retreives the current file names in directory 'test'
dirInfo = dir('test');
% converts the directory names to string array
dirName = char(dirInfo.name);
% Checks if the euclidian distance is less than the threhsold only then the
% result would be displayed
if distmin <>
msg=sprintf(['Speaker best matches with file ' dirName(k1+2, :)]);
else
msg=sprintf('Match Not name found');
end
% Checks if the value is less than the threshold only then the result
% would be displayed
if minErrorVar <>
msg2=sprintf(['Speaker best matches with file ' dirName(indicator+2, :)]);
else
msg2=sprintf('Match Not name found');
end
% GUI Info
% get screen size
pos = get(0,'ScreenSize');
% colors
Bg_Color = [0.7 0.7 0.8];
Bg_Color1 = [0.8 0.8 0.8];
Frame_Color2 = [0.9 0.8 0.8];
Frame_Color = [0.8 0.8 0.9];
Button_Color1 = [0.7 0.7 0.7];
Bg_Color4 = [1 0.40 0.5];
% main buttons size
button_width = 150;
button_height = 40;
main_width = 1004;
main_height = 688;
main_width = 804;
main_height = 488;
button_left = ((main_width-10)-button_width)/2;
% title position
title_width = 660;
title_left = (main_width-title_width)/2;
%title_bottom = y_top_frame1+2*border;
title_bottom = 200;
title_height = 50;
% main window
hfigD = figure('Name','Speaker Recognition System ',...
'NumberTitle','off',...
'Resize','on',...
'Visible','on',...
'Position',[pos(3)/2-main_width/2 pos(4)/2-main_height/2-5 main_width main_height+20],...
'MenuBar','none');
% The exit button
button2=uicontrol(hfigD, ...
'style','push', ...
'position',[button_left title_bottom-100 button_width button_height], ...
'string', 'EXIT',...
'callback','closeDisp');
% Displays the message
Title1 = uicontrol(hfigD,...
'style','text',...
'position',[title_left+50 title_bottom+150 title_width-200 title_height],... % [from_left from_bottom width height]
'BackgroundColor',Bg_Color4,...
'Fontweight','bold',...
'FontSize',12,...
'string',[msg,': using the MFCC algorithm']);
% The check box
text1 = uicontrol(hfigD,...
'style','checkbox',...
'position',[title_left+50 title_bottom+100 title_width-200 title_height],... % [from_left from_bottom width height]
'Fontweight','bold',...
'FontSize',12,...
'string',['Check the check box if the result is not correct']);
% The message for 2nd algo
Title2 = uicontrol(hfigD,...
'style','text',...
'position',[title_left+50 title_bottom title_width-200 title_height],... % [from_left from_bottom width height]
'BackgroundColor',Bg_Color4,...
'Fontweight','bold',...
'FontSize',12,...
'string',[msg2,': using the modified MFCC algorithm']);
% the check box for 2nd algo
text2 = uicontrol(hfigD,...
'style','checkbox',...
'position',[title_left+50 title_bottom-50 title_width-200 title_height],... % [from_left from_bottom width height]
'Fontweight','bold',...
'FontSize',12,...
'string',['Check the check box if the result is not correct']);
*********************************CLOSE DISPLAY****************************
% The script file to close the result display function
% load the previous result of the current session
load result;
% get the value of the check box 1 and invert it and store in variable
MFCC_Result = [MFCC_Result ~get(text1, 'value')];
% get the value of the check box 2 and invert it and store in variable
modiMFCC_Result = [modiMFCC_Result ~get(text2, 'value')];
% save the result back with modified variables
save result MFCC_Result modiMFCC_Result;
% close the figure that displays the result
close(hfigD);

1 comment:

  1. excuse me kind sir but i notice there are some incomplete lines like this condition if dist <>
    and if(ErrorVar <> could you please tell what the conditions are for this? thanks JJ

    ReplyDelete