clc
clear all
close all
%The sampling frequency is FS
%The formant frequency ranges are as follows
%F1 270 to 1030  F2-840 to 3200 F3-2240 to 3730 Hz
%F1 235 to 850 Hz  F2-595 to 2400 Hz 
%F1- 270 to  730 Hz ,F2-840 to 2290 Hz, F3-1690 to 3010 Hz
[A,FS]=audioread('D:\GOPI\DIGITAL_SPEECH_PROCESSING_USING_MATLAB\data\test\s8.wav');
S=A';
 [res1,res2,speechsegment,utforste,ltforste,ltforzcr]=endpointdetection(S,FS)
S=speechsegment;
SEGLEN=FS*30*10^(-3);
%Identify the frame for extracting pitch
S1=S(fix(length(S)/2):SEGLEN-1+fix(length(S)/2));

%function [F]=forexthom(S1,FS)
S1=S1.*hamming(length(S1))';
[H,W]=freqz(1,lpc(S1,11),'whole');
figure
plot(abs(H))
figure
subplot(2,2,1)
plot(S1)
subplot(2,2,2)
plot(abs(fft(S1,512)))
S2=20*log(abs(fft(S1,512)));
subplot(2,2,3)
plot(S2)
S3=smooth(S2,50);
S3=S3+abs(min(S3));
S3=S3/max(S3);
S3=exp(S3)-1;
S3=S3/max(S3);
subplot(2,2,4)
plot(S3)
hold on
%S4=abs(exp(S3)/max(exp(S3)));
%subplot(2,3,4)
%figure
%plot(S4)

% S5=S5(1:1:188);
% [p,q]=max(S5(7:1:length(S4)))
% PF=FS/(q+7);
% hold on
% plot(abs(H)/max(abs(H)),'r')

formant1=0;
formant2=0;
formant3=0;

th1=round((1024*80)/FS);
p=[270/FS 730/FS 840/FS 2290/FS 1690/FS 3010/FS]*512;

cons=th1;
for i=1:1:2
p1=round(p);
while((p1(i)-cons)<=0)
cons=cons-1;
end
[p2,q2]=max(S3(p1(i)-cons:1:p1(i)+cons));
pos1=p1(i)-cons+q2-1;
colpos(i)=pos1;
format(i)=((pos1-1)*FS)/512;
end


[m,n]=max(S3(colpos(1:2)));
n1=n+colpos(1);
if ((n1-th1)<=0)   
bwth=(S3(n1)/S3(n1-1)+S3(n1)/S3(n1+1) )/2;
if(bwth<(1.414/th1))
formant1=((colpos(n)-1)*FS)/512;
plot(colpos(n),S3(colpos(n)),'ro')    
else
disp('there is no formant 1')
end
else
 bwth=(S3(n1)/S3(n1-th1)+S3(n1)/S3(n1+th1) )/2;
 if(bwth<1.4141)
    formant1=((colpos(n)-1)*FS)/512;
plot(colpos(n),S3(colpos(n)),'ro')
else
disp('there is no formant 1')
end
end


[m,n]=max(S3(colpos(3:4)));
n1=n+colpos(3);
if ((n1-th1)<=0)   
bwth=(S3(n1)/S3(n1-1)+S3(n1)/S3(n1+1) )/2;
if(bwth<(1.4141/th1))
    formant2=((colpos(n+2)-1)*FS)/512;
 plot(colpos(n+2),S3(colpos(n+2)),'ro')
else
disp('there is no formant 2')
end
else
 bwth=(S3(n1)/S3(n1-th1)+S3(n1)/S3(n1+th1) )/2;
 if(bwth<1.414)
    formant2=((colpos(n+2)-1)*FS)/512;
plot(colpos(n+2),S3(colpos(n+2)),'ro')
else
disp('there is no formant 2')
end
end
[m,n]=max(S3(colpos(5:6)));
n1=n+colpos(5);
if ((n1-th1)<=0)   
bwth=(S3(n1)/S3(n1-1)+S3(n1)/S3(n1+1) )/2;
if(bwth<(1.4141/th1))
    formant3=((colpos(n+4)-1)*FS)/512;
plot(colpos(n+4),S3(colpos(n+4)),'ro')
else
disp('there is no formant 3')
end
else
 bwth=(S3(n1)/S3(n1-th1)+S3(n1)/S3(n1+th1) )/2;
 if(bwth<1.414)
    formant3=((colpos(n+4)-1)*FS)/512;
plot(colpos(n+4),S3(colpos(n+4)),'ro')
else
disp('there is no formant 3')
end
end

F=[formant1 formant2 formant3];

% %     if((270<=format(i))&(format(i)<=7>030))
% %         colformat1=[colformat1 colpos(i)];
% %     end
% end
% [m,n]=max(S3(colformat1));
% if(isempty(n)==1)
%     
% else
% %formant1=format(length(colformat1)+n-1);
% formant1=((colformat1(n)-1)*FS)/512;
% end

% colformat2=[];
% for i=3:1:4
%     if((840<=format(i))&(format(i)<=2290))
%         colformat2=[colformat2 colpos(i)];
%     end
% end
% [m,n]=max(S3(colformat2));
% if(isempty(n)==1)
%     disp('there is no formant 2')
% else
% %formant2=format(length(colformat2)+n+1);
% formant2=((colformat2(n)-1)*FS)/512;
% end
% 
% colformat3=[];
% for i=5:1:6
%     if((1690<=format(i))&(format(i)<=3010))
%       colformat3=[colformat3 colpos(i)];
%     end
% end
% [m,n]=max(S3(colformat3));
% if(isempty(n)==1)
%     disp('there is no formant 3')
% else
% %formant3=format(length(colformat3)+n+3);
% formant3=((colformat3(n)-1)*FS)/512;
% end
% disp(formant1)
% disp(formant2)
% disp(formant3)
