# How can one do “two series” Fourier analyses in R?

In Statistica one can do a "two series"/bivariate/cross spectrum Fourier analysis to examine the coherency, gain, and phase spectrum across a pair of signals. It is probably a failing on my part, but my attempts to use those search terms to come up with similar values from R have come up blank. What functions/packages should I review in more detail? Can you provide some simple examples?

-
I've used various EEG analysis packages to accomplish the same thing, but never in R -- see if the "bivariate" snippet here is what you are looking for stat.ethz.ch/R-manual/R-patched/library/stats/html/… –  jonsca Apr 3 '12 at 5:36
Close, but it doesn't seem like it calculates gain. –  rpierce Apr 3 '12 at 22:11
I'm not sure how to do that using R. –  jonsca Apr 3 '12 at 22:20

The same familiar spectrum instruction yields cross-spectra. coherency and phase when used on a bivariate time series. Look at this sample code. dIPC and Crudo are respectively (differenced) consumer price index and oil price. We first build a bivariate time series (out of ordinary ts time series) with ts.union, then invoke spectrum.

datos <- ts.union(dIPC,
Crudo)
datos <- window(datos,
start=c(1979,1),
end=c(2002,1))
sp <- spectrum(datos,
main="Petróleo e IPC",
spans=rep(3,5))


We only need now to plot coherency and phase. I find it useful to align them one below the other with

par(mfrow=c(2,1))


and then do the plots:

plot(sp,plot.type="coh")
plot(sp,plot.type="phase")

-
What does (differenced) mean in this context? Is it just a characteristic of your data, or does it represent something I must do to my data before analysis? –  rpierce Apr 3 '12 at 14:49
You difference series x(t) by replacing it with dx(t) = x(t) - x(t-1). Since spectral analysis is intended for stationary random processes, this is a way of bringing non stationary data to near stationarity. Only needed if strong trends are apparent. –  F. Tusell Apr 3 '12 at 16:16
How is gain calculated from this output? –  rpierce Apr 3 '12 at 17:30
Per Statistica's website: "The gain value is computed by dividing the cross-amplitude value by the spectrum density estimates for one of the two series in the analysis. Consequently, two gain values are computed, which can be interpreted as the standard least squares regression coefficients for the respective frequencies." But I'm unclear on how to find the cross-amplitude value. –  rpierce Apr 3 '12 at 21:36
You can dig the source of spectrum, and you will find that cross-spectra are computed in an object of name pgram. Or, since the coherency is defined as the cross-spectrum divided by the square root of the product of univariate spectra (i.e. f12 / sqrt(f11*f22) ), you can find what you want the two gains as tou define them by multiplying the squared coherency by either univariate spectra. –  F. Tusell Apr 4 '12 at 5:28