function final = magmaster(sig1,sig2)
% final = magmaster(sig1,sig2)
%
% magmaster - takes two signals and returns the dot product of the
% magnitudes of the two signals after the signals are cut and time-warped.
%
% 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
%global variables
samprate = 10000;
N = 100;
step = 100;
%find out where to cut the two signals.
[mag1,pts1] = locatespeech(sig1,N,step,samprate);
sig1cut = (sig1(pts1(1):pts1(2)));
[mag2,pts2] = locatespeech(sig2,N,step,samprate);
sig2cut = (sig2(pts2(1):pts2(2)));
%The average magnitude function on the cut signals gets a cleaner
%average magnitude for the two signals as opposed to cutting the magnitude
%of the whole signal at the determined endpoints.
mag1 = avgmag(sig1cut,N,step);
mag2 = avgmag(sig2cut,N,step);
%Discrete time warping one signal to the other in order to let the peaks
%match up. The time warp is performed both ways (matching sig1 to sig2 and
%vice versa. The output will be the best match of the two signals.
%Let sig1 be the key and sig2 will be time-warped to sig1.
path1 = dtw4(mag1,mag2);
num = dot(mag1,mag2(path1));
den = (norm(mag1))*(norm(mag2(path1)));
final1 = num/den;
%Let sig2 be the key and sig1 will be time-warped to sig2.
path2 = dtw4(mag2,mag1);
num = dot(mag1(path2),mag2);
den = (norm(mag1(path2)))*(norm(mag2));
final2 = num/den;
if final1 > final2
final = final1;
else
final = final2;
end