Recently I wanted to align some time-series data recently for plotting. I couldn’t find some library function in R to do it for me (actually I did, but they involved stuffing around with special time series objects instead of vectors), and the problem was interesting anyway so I wrote something myself. The idea is to find the offset using cross-correlation and then do a phase shift to align. This can all be done in the frequency domain, where the cross-correlation is just the Hadamard product between the Fourier components of the signal to align with the conjugate of the reference.
there’s two align function, align2() aligns a pair of signals with x as the reference and align() aligns the columns of a matrix. There’s a bunch of padding added for the Fourier transform so the final vectors will contain leading and trailing zeros. The plotting function removes these. Here’s an example, unaligned:
 Semi-cute but well known, reversing the Fourier coefficients of a real signal reduces to calculating the complex conjugate. As an ordinary convolution in the frequency domain is x ⊗ y, the cross-correlation is thus x ⊗ y̅.