Phase Alignment Algorithm
In order for our music to have good sound quality, there is one last step to
our algorithm. As it is now, if our signals were added together,
their beats would not necessarily occur at the same time even though
their tempos are the same. To fix this problem, we need
to align the songs beats such that they occurr simultaneously. This phase
alignment process is simply an extension of the beat matching algorithm.
We begin by running one of our signals through all the steps of
the beatmatching algorithm prior to the comb filtering. This gives
a signal with strong peaks where the beats occur. Since we know
the frequency at which these beats occur, we know which comb filter
corresponds to this tempo. This comb filter is then convolved with
the signal for one period. This is implemented by multiplication
of the FFT's of the comb filter and the modified signal. We take
the energy of this output and find the location in time of the maximum
energy value.
This process is repeated for the other signal, resulting in two
locations in time  where the first beat occurs in each signal. The
signal whose beat occurs first is simply zeropadded until the two
beats occur simultaneously. Now the two songs are phasealigned.
Matlab Code
 phasealign.m  takes a time domain signal and returns the location of
% the sample where its first beat occurs.
Matlab Plots
Before Phase Alignment:
After Phase Alignment:
