function corr = pitchmaster(sig1,sig2)
% corr = pitchmaster(sig1,sig2)
%
% pitchmaster - Takes in two signals, computes their pitch tracks, dynamic
% time warps them, and determines their correlation.  Both sig1 onto sig2
% and sig2 onto sig1 are performed, with the larger of the two correlations
% being returned.
%
% ELEC 301 Project - "Mars Lander the Theologian", aka, "Speaker Verification"
% Sara MacAlpine, Aamir Virani, Nipul Bharani, JP Slavinsky
% WRC (OCEE) Class of 2001 - Fall '99

samprate = 10000;
windowsize = 512;

%******************************************************
%Produce Pitch Tracks
%******************************************************
b1=specgram(sig1,windowsize,samprate);
track1=pitch(b1,samprate);

b2=specgram(sig2,windowsize,samprate);
track2=pitch(b2,samprate);

%******************************************************
%Prepare for Dynamic Time Warp
%******************************************************
%cut the trailing and leading zeros of track1
i=1;
while (track1(i)==0)
  i=i+1;
end
j=length(track1);
while (track1(j)==0)
  j=j-1;
end
track1cut=track1(i:j);

%cut the trailing and leading zeros of track2
i=1;
while (track2(i)==0)
  i=i+1;
end
j=length(track2);
while (track2(j)==0)
  j=j-1;
end
track2cut=track2(i:j);

%******************************************************
%Perform Dynamic Time Warping
%******************************************************
%map track2 onto track1 and calculate dot product
path2on1  = dtw4(track1cut,track2cut);
track2dtw = track2cut(path2on1);
dotWDTW1  = dot(track1cut,track2dtw)/(norm(track1cut)*norm(track2dtw));

%map track1 onto track2 and calculate dot product
path1on2  = dtw4(track2cut,track1cut);
track1dtw = track1cut(path1on2);
dotWDTW2  = dot(track2cut,track1dtw)/(norm(track2cut)*norm(track1dtw));

%******************************************************
%Take Larger Dot Product as Result
%******************************************************
if (dotWDTW2>dotWDTW1),
  corr=dotWDTW2;
else
  corr=dotWDTW1;
end
