clear all
[s, fs] = wavread('tada.wav');

s1 = wavread('taaa.wav');
% convert to mono, if need be: s1 = s1(:,1);
% test with different phase: s1 = s1(42:end);

s2 = wavread('taahaa.wav');
len1 = length(s1);
len2 = length(s2);
startFade = 20000;
endFade = 20500;

interval = 1000;
s1interval = s1(endFade-interval:endFade-1);
energy1 = sum(s1interval .^ 2);
s2interval = s2(startFade:startFade+interval-1);
energy2 = sum(s2interval .^ 2);

s2 = s2 * sqrt(energy1 / energy2);

co = normxcorr2(s2(startFade:startFade+500-1), s1interval);
[m, n] = max(co);
shift = n;

fade = (1:endFade-startFade+1)'/(endFade-startFade+1);
r = [s1(1:startFade-1); (1-fade).*s1(startFade:endFade)+fade.*s2(startFade-shift:endFade-shift); s2(endFade+1-shift:end)];
soundsc(r, fs)
% plot((1:len1),s1,(1:len2),s2)