0% found this document useful (0 votes)
118 views6 pages

Coding

The MATLAB code is for a speech recognition system that allows users to add sounds to a database from files or a microphone, recognize speech by comparing an input to sounds in the database, and view or delete the database. It uses dynamic time warping to compare spectrograms of the input and database sounds and returns the sound with the minimum distance.

Uploaded by

GOURAV506151
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views6 pages

Coding

The MATLAB code is for a speech recognition system that allows users to add sounds to a database from files or a microphone, recognize speech by comparing an input to sounds in the database, and view or delete the database. It uses dynamic time warping to compare spectrograms of the input and database sounds and returns the sound with the minimum distance.

Uploaded by

GOURAV506151
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ggits.

m
clc;clear;close all;
chos=0;
possibility=6;
while chos~=possibility,

chos=menu('GYAN GANGA SRS','-- Add sound from file --','-- Add sound
form microphone --','-- Recognise Speech --','-- Database Information --','--
Delete database --','-- Exit --');
%----------------------------------------------------------------------
% Add a new sound from files
if chos==1,
clc;
[namefile,pathname]=uigetfile('*.wav;*.au','Select a new sound');
if namefile~=0
pos = strfind(namefile,'.');
ext = namefile(pos+1:end);
if strcmp(ext,'au')
[y,Fs,bits] = auread(strcat(pathname,namefile));
end
if strcmp(ext,'wav')
[y,Fs,bits] = wavread(strcat(pathname,namefile));
end
% if the input sound is not mono
if size(y,2)==2
y=y(:,1);
end
classe = input('Insert a class number (sound ID) that will be used
for recognition:');
if (exist('sound_database.dat')==2)
load('sound_database.dat','-mat');
if (Fs ~= samplingfrequency) || (bits ~= samplingbits)
warndlg('Sampling parameters do not match with parameters
already present in database',' Warning ')
else
sound_number = sound_number+1;
data{sound_number,1} = y;
data{sound_number,2} = classe;
data{sound_number,3} = pathname;
data{sound_number,4} = namefile;
save('sound_database.dat','data','sound_number','-
append');
msgbox('Sound added to database','Database
result','help');
disp('Sound added to database');
end
else
samplingfrequency = Fs;
samplingbits = bits;
sound_number = 1;
data{sound_number,1} = y;
data{sound_number,2} = classe;
data{sound_number,3} = pathname;
data{sound_number,4} = namefile;
save('sound_database.dat','data','sound_number','samplingfrequency','samplingb
its');
msgbox('Sound added to database','Database result','help');
disp('Sound added to database');
end
else
warndlg('Input sound must be selected.',' Warning ')
end
end
%----------------------------------------------------------------------
% Add a new sound from microphone
if chos==2
if (exist('sound_database.dat')==2)
load('sound_database.dat','-mat');
message=('The following parameters will be used during
recording:');
disp(message);
message=strcat('Sampling frequency',num2str(samplingfrequency));
disp(message);
message=strcat('Bits per sample',num2str(samplingbits));
disp(message);
durata=input('Insert the duration of the recording (in
seconds):');
micrecorder = audiorecorder(samplingfrequency,samplingbits,1);
disp('Now, speak into microphone...');
record(micrecorder,durata);
while (isrecording(micrecorder)==1)
disp('Recording...');
pause(0.5);
end
disp('Recording stopped.');
dtime=datestr(now);
message=strcat('Your Attendence has been registered at:',dtime);
disp(message);
y = getaudiodata(micrecorder, 'uint8');
plot(y);
classe = num2str(input('Insert your roll number:'));
sound_number = sound_number+1;
data{sound_number,1} = y;
data{sound_number,2} = classe;
data{sound_number,3} = 'Microphone';
data{sound_number,4} = dtime;
save('sound_database.dat','data','sound_number','-append');
msgbox('Sound added to database','Database result','help');
disp('Sound added to database');
else
durata = input('Insert the duration of the recording
(in seconds):');
samplingfrequency = input('Insert the sampling frequency (22050
recommended):');
samplingbits = input('Insert the number of bits per sample (8
recommended):');
micrecorder = audiorecorder(samplingfrequency,samplingbits,1);
disp('Now, speak into microphone...');
record(micrecorder,durata);
while (isrecording(micrecorder)==1)
disp('Recording...');
pause(0.5);
end
disp('Recording stopped.');
dtime=datestr(now);
message=strcat('Your Attendence has been registered at:',dtime);
disp(message);
y = getaudiodata(micrecorder, 'uint8');plot(y);
classe = input('Insert your roll number:');
sound_number = 1;
data{sound_number,1} = y;
data{sound_number,2} = classe;
data{sound_number,3} = 'Microphone';
data{sound_number,4} = dtime;

save('sound_database.dat','data','sound_number','samplingfrequency','samplingb
its');
msgbox('Sound added to database','Database result','help');
disp('Sound added to database');
end
end
%----------------------------------------------------------------------
% % Speech Recognition from microphone
if chos==3
if (exist('sound_database.dat')==2)
load('sound_database.dat','-mat');
Fs = samplingfrequency;
durata = input('Insert the duration of the recording
(in seconds):');
micrecorder = audiorecorder(samplingfrequency,samplingbits,1);
disp('Now, speak into microphone...');
record(micrecorder,durata);
while (isrecording(micrecorder)==1)
disp('Recording...');
pause(0.5);
end
disp('Recording stopped.');
y = getaudiodata(micrecorder, 'uint8');
%----- code for speech recognition -------
vettore_pesi = zeros(sound_number,1);
D1 = specgram(y,512,Fs,512,384);
disp('Database scanning...');
for ii=1:sound_number
D2 = specgram(data{ii,1},512,Fs,512,384);
SM = simmx(abs(D1),abs(D2));
[p,q,C] = dp(1-SM);
peso = C(size(C,1),size(C,2));
vettore_pesi(ii) = peso;
message=strcat('Sound #',num2str(ii));
disp(message);
end
[min_value,min_index] = min(vettore_pesi);
speech_id = data{min_index,2};
%-----------------------------------------
disp('Matching sound:');
message=strcat('File:',data{min_index,4});
disp(message);
message=strcat('Location:',data{min_index,3});
disp(message);
message = strcat('Recognized speech ID: ',num2str(speech_id));
disp(message);
msgbox(message,'Matching result','help');
else
warndlg('Database is empty. No matching is possible.',' Warning ')
end
end
%----------------------------------------------------------------------
% Database Info
if chos==4
clc;
if (exist('sound_database.dat')==2)
load('sound_database.dat','-mat');
message=strcat('Database has total- ',num2str(sound_number),'
entries.');
disp(message);
disp(' ');
for ii=1:sound_number
message=strcat('Roll Number:',num2str(data{ii,2}));
disp(message);
message=strcat('Attendence Recorded at:',data{ii,4});
disp(message);
disp('-');
end
else
warndlg('Database is empty.',' Warning ')
end
end
%----------------------------------------------------------------------
% Delete database
if chos==5
clc;
close all;
if (exist('sound_database.dat')==2)
button = questdlg('Do you really want to remove the Database?');
if strcmp(button,'Yes')
delete('sound_database.dat');
msgbox('Database was succesfully removed from the current
directory.','Database removed','help');
end
else
warndlg('Database is empty.',' Warning ')
end
end
%----------------------------------------------------------------------
end
simmx.m
function M = simmx(A,B)
EA = sqrt(sum(A.^2));
EB = sqrt(sum(B.^2));
M = (A'*B)./(EA'*EB);

dp.m
function [p,q,D] = dp(M)

[r,c] = size(M);

D = zeros(r+1, c+1);
D(1,:) = NaN;
D(:,1) = NaN;
D(1,1) = 0;
D(2:(r+1), 2:(c+1)) = M;

% traceback
phi = zeros(r,c);

for i = 1:r;
for j = 1:c;
[dmax, tb] = min([D(i, j), D(i, j+1), D(i+1, j)]);
D(i+1,j+1) = D(i+1,j+1)+dmax;
phi(i,j) = tb;
end
end

% Traceback from top left


i = r;
j = c;
p = i;
q = j;
while i > 1 & j > 1
tb = phi(i,j);
if (tb == 1)
i = i-1;
j = j-1;
elseif (tb == 2)
i = i-1;
elseif (tb == 3)
j = j-1;
else
error;
end
p = [i,p];
q = [j,q];
end

% Strip off the edges of the D matrix before returning


D = D(2:(r+1),2:(c+1));

You might also like