[R] estimate phase shift between two signals

Dylan Beaudette dylan.beaudette at gmail.com
Wed Jun 4 19:38:47 CEST 2008


Are there any functions in R that could be used to estimate the phase-shift 
between two semi-sinusoidal vectors? Here is what I have tried so far, using 
the spectrum() function -- possibly incorrectly:

# generate some fake data, normalized to unit circle
x <- jitter(seq(-2*pi, 2*pi, by=0.1), amount=pi/8)

# functions defining two out-of-phase phenomena
f1 <- function(x) jitter(sin(x), amount=0.25)
f2 <- function(x, a) jitter(sin(x + a), amount=0.25)

# compute y-values
# we are setting the phase shift arbitrarily
s <- pi/1.5632198
y1 <- f1(x)
y2 <- f2(x, s)

# plot:
plot(x, y1, type='p', col='red', cex=0.5)
lines(lowess(x, y1, f=0.25), col='red')

points(x, y2, col='blue', cex=0.5)
lines(lowess(x, y2, f=0.25), col='blue')

# generate time series object
comb.ts <- ts(matrix(c(y1, y2), ncol=2))

# multivariate spectral decomposition
spec <- spectrum(comb.ts, detrend=FALSE)

# but how to interpret the phase estimate?

the mean 'phase' as returned from spectrum() does not seem to match the value 
used to generate the data... Am I mis-interpreting the use or output from 
spectrum() here? If so, is there a general procedure for estimating a 
phase-shift between two noisy signals? Would I first have to fit a smooth 
function in order to solve this analytically?

Thanks in advance,

Dylan Beaudette
Soil Resource Laboratory
University of California at Davis

More information about the R-help mailing list