clear;clc;
[y,fs] = wavread('PhoneNumberA.wav');
n = length(y);
% remove low frequency noise
Wn=650/4000;
[b,a]=butter(3,Wn,'high');
y=filter(b,a,y);
%threshold equals half of the average energy
threshold=0.5*sum(y.^2)./n;
N=512;
framenum=floor(n/N);
fq=[697,770,852,943,1209,1336,1477,1663];
fqnum=length(fq);
%find useful segments
r=[];num=blanks(7);
e=zeros(1,framenum);
startframe=zeros(1,7);
count= ones(1,7);
flag=0;n=1;
for i=1:1:framenum
frame=y(N*(i-1)+1:N*i);
e(i)=sum(frame.^2)/N;
%count the number of useful continuous frames
switch flag
case 0
if(e(i)>=threshold)
startframe(n)=i;
flag=1;
else flag=0;
end
case 1
if(e(i)>=threshold)
count(n)=count(n)+1;
flag=1;
else flag=0;
n=n+1;
end
end
end
figure
plot(e);
line(1:1:framenum,threshold,'Color','r','LineStyle','-' );
%discriminate the digits
figure
for i=2
r=y(N*(startframe(i)-1)+1:N*(count(i)+startframe(i)-1));
R=fft(r);
subplot(7,1,i);
plot(abs(R));
axis tight
fq1=1100;%divide the segment into fq_low & fq_high part
n1=round(fq1/fs*count(i)*N);
m1=[];
m1=R(1:n1);
m2=[];
m2=R(n1+1:count(i)*N/2);
%find peak in each part and the corrisponding fq
[max_value1,index1] = max(m1);
[max_value2,index2] = max(m2);
index2=index2+n1;
fqlow=index1/(count(i)*N)*fs;
fqhigh=index2/(count(i)*N)*fs;
%match the calculated fqs to the specific 8 basic fqs
for j1=1:1:fqnum;
if abs(fqlow-fq(j1))<20
fqlow=fq(j1);
end
end
for j2=1:1:fqnum;
if abs(fqhigh-fq(j2))<20
fqhigh=fq(j2);
end
end
fq_mx=zeros(1,2);
fq_mx=[fqlow,fqhigh];
if fq_mx==[fq(1),fq(5)]
num(i)='1';
elseif fq_mx==[fq(1),fq(6)]
num(i)='2';
elseif fq_mx==[fq(1),fq(7)]
num(i)='3';
elseif fq_mx==[fq(2),fq(5)]
num(i)='4';
elseif fq_mx==[fq(2),fq(6)]
num(i)='5';
elseif fq_mx==[fq(2),fq(7)]
num(i)='6';
elseif fq_mx==[fq(3),fq(5)]
num(i)='7';
elseif fq_mx==[fq(3),fq(6)]
num(i)='8';
elseif fq_mx==[fq(3),fq(7)]
num(i)='9';
elseif fq_mx==[fq(4),fq(6)]
num(i)='0';
end
end
disp(['The phone number is:',num]);