You are on page 1of 3

% Assignment # 1

% Submitted by : Bharat Patel & Marvin Odor


clear all
No_of_Gaussians=10;
disp('-------------------------------------------------------------------');
disp(' Speaker recognition System');
disp(' A simple demonstration of speaker recognition using MFCCs and GMMS');
disp('Marvin Odor & Bharat Patel - MITS students at UOIT, Oshawa, Canada');
disp('-------------------------------------------------------------------');
disp(' ');
reply= ' ';
while (~(reply == '4'))
No_of_Gaussians=10;
reply = input('1.Enrollment \n2.Identification \n3.Verification \n4.Exit \nW
hat do you want? \n','s');
if isempty(reply)
reply = '4';
end
if length(reply)>1
disp('Invalid input try again!!!');
reply=' ';
end
%%%%%%%%%%%%%%%%%%%%%%% ENROLLMENT %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if reply=='1'
user=input('Please enter username to be enrolled: \n','s');
% --- check whether username already exist in our database----
fid=fopen('\data\user.dat','rt');
while(~feof(fid))
ln=fgetl(fid);
[d1 d2]=strtok(ln);
if (strcmp(d1,user))
disp('User already enrolled in the system, please try diffrent usern
ame...');
fclose(fid);
return;
end
end
fclose(fid);
% --- record voice data, extract features & save both in our database
disp('Recording started, please speak for 1 min.');
Fs=8000;
y = wavrecord(60*Fs,Fs,'int16');
disp('Recording completed!!!');
wavwrite(y,[ '\data\' user '.wav']);
fid=fopen('\data\user.dat','a+t');
fprintf(fid,'%s %s\n',user,[ '\data\' user '.wav']);
fclose(fid);
disp('Enrollment completed, Congratulations!!!');
enroll_fea=melcepst(y,Fs);
save (['\data\' user '.fea'],'-ascii', 'enroll_fea');
clear all;
reply=' ';
end
%%%%%%%%%%%%%%%%%%%%%%% IDENTIFICATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if reply=='2'
cnt=0;

% ----read features for all users in the database ----
fid=fopen('\data\user.dat','rt');
while(~feof(fid))
cnt=cnt+1;
ln=fgetl(fid);
[d1 d2]=strtok(ln);
s=load(['\data\' d1 '.fea'],'-ascii');
train_fea(:,:,cnt)=s;
end
nuser=cnt;
fclose(fid);
% ---- Record test user's data & extract features
disp('Test recording started, please speak for 30 sec.');
Fs=8000;
test_data = wavrecord(30*Fs,Fs,'int16');
disp('Recording completed!!!');
test_fea=melcepst(test_data,Fs);

% -----Create models from the for users in our database
%train_fea=zeros(nuser,1);
% mu_train=zeros(nuser,1);
%sigma_train=zeros(nuser,1);
% c_train=zeros(nuser,1);
for cnt=1:nuser
s=train_fea(:,:,cnt);
[mu_train(:,:,cnt), sigma_train(:,:,cnt), c_train(:,:,cnt)]=gmm_estimate
(s',No_of_Gaussians);

end
% -----Compare test user's features against models of known users
result=zeros(nuser,1);
for cnt=1:nuser
[lYM,lY]=lmultigauss(test_fea', mu_train(:,:,cnt),sigma_train(:,:,cnt),c
_train(:,:,cnt));
result(cnt)=mean(lY);
end
disp('--------------Identification results------------------');
disp(result);
disp('--------------------------------------------');

%---- find user name for the close match from database
[mval,mind]=max(result);
fid=fopen('\data\user.dat','rt');
cnt=0;
while(~feof(fid))
cnt=cnt+1;
ln=fgetl(fid);
if (cnt==mind)
[d1 d2]=strtok(ln);
user=d1;
break;
end
end
fclose(fid);
disp('--------------The closest match is------------------');
disp(user);
disp('--------------------------------------------');
clear all;
reply=' ';
end
%%%%%%%%%%%%%%%%%%%%%%% VERIFICATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if reply=='3'
cnt=0; found=0;
% ---- check whether username exist in our database
user=input('Please enter username to be verified: \n','s');
fid=fopen('\data\user.dat','rt');
while(~feof(fid))
cnt=cnt+1;
ln=fgetl(fid);
[d1 d2]=strtok(ln);
if (strcmp(d1,user))
found=cnt;
break;
end
end
nuser=cnt;
fclose(fid);
if found==0
disp('The entered user not in our database, please enroll before using t
his option!!!');
return;
end
%----Record voice data to be verified, extract features ---------
%----and compare against model from data in ourdatabase ------
disp('Verification recording started, please speak for 30 sec.');
Fs=8000;
test_data = wavrecord(30*Fs,Fs,'int16');
disp('Recording completed!!!');
train_fea=load([ '\data\' user '.fea'],'-ascii');
[mu_train,sigma_train,c_train]=gmm_estimate(train_fea',No_of_Gaussians);
test_fea=melcepst(test_data,Fs);
[lYM,lY]=lmultigauss(test_fea', mu_train,sigma_train,c_train);
disp('--------------Verification result------------------');
disp(mean(lY));
disp('--------------------------------------------');
clear all;
reply=' ';
end
end
return;
% show as matrix for intuition
%figure; imagesc(A); colorbar;

You might also like