%pith frequency estimation using homomorphic filtering
%clear all
%close all
%The sampling frequency is FS
%The typical male fundamental frequency range=85 to 180 Hz
%The typical female fundamental frequency range=165 to 255 Hz
clc;
clear all;
close all;
i=6;
[A,FS]=audioread('D:\GOPI\DIGITAL_SPEECH_PROCESSING_USING_MATLAB\data\train\s4.wav');
S=A';
%function [PF]=pitchfreqest(S,FS);
 [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));
S1=S1.*hamming(length(S1))';
% subplot(2,3,1)
% plot(S1)
% subplot(2,3,2)
% plot(abs(fft(S1)))
% S2=20*log(abs(fft(S1)));
% %FS/500 is the resolution in S1
% subplot(2,3,3)
% plot(S2)
% S3=abs(ifft(S2));
% subplot(2,3,4)
% plot(S3)
% subplot(2,3,5)
% S4=smooth(S3,5);
% plot(S4)
figure(10)
subplot(2,5,i)
plot(S1)
subplot(2,5,i+1)
S2=abs(fft(S1));
plot(S2)
S3=20*log(S2);
subplot(2,5,i+2)
plot(S3)
S4=abs(ifft(S3));
subplot(2,5,i+3)
plot(S4)
subplot(2,5,i+4)
S5=smooth(S4,5);
plot(S5)

%subplot(2,3,6)
%S5=abs(ifft(exp(abs(fft(S4))/20)));
%plot(S5/max(S5))
S5=S5(1:1:188);
[p,q]=max(S5(9:1:length(S5)));
PF=FS/(q+9);
% hold on
% plot(abs(H)/max(abs(H)),'r')
