Follow posts tagged #fourier in seconds.

Sign up

Aligning time-series

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[1].

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:

unaligned

and aligned:

aligned

[1] 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̅.

“Happiness,” wrote Fourier, “consists in having many passions and many means to satisfy them.” Fourier called for the complete gratification of every man’s desires. But he assured his readers that the quest for pleasure, no matter how selfish it might seem, was identical with the practice of social virtue: in Harmony ‘the man who devotes himself most ardently to pleasure becomes eminently useful; for the happiness of all!”
Loading more posts...