# Using spectral coherence for computing similarity between time series data

I don't really know anything about signal processing. I need to find similairty between two 1-dimensional time series. I read somewhere that spectral coherence could be used to find similarity between two time series. But i am not able to get around how to use it. Can some one tell how i can use it to compute similarity between time series.

• did you ever get this issue resolved because I have the same problem and I would like to source C code for this analysis?
– user18552
Dec 1 '15 at 21:54
– Peter K.
Dec 1 '15 at 22:09
– Peter K.
Dec 1 '15 at 22:48

One way to check for similarity between time series is to use the cross-correlation: $$\rho_{xy}(\tau) = \frac{1}{\sigma_x \sigma_y} E[ (x[n] - \mu_x)(y[n] - \mu_y) ]$$ where $\mu_{x,y}$ is the mean of $x$ or $y$ and $\sigma_{x,y}$ is the standard deviation of $x$ or $y$.

This gives us a value between -1 (completely anti-correlated) and +1 (completely correlated).

Sometimes, this doesn't work as well as we'd like because there is some filtering operation relating the two time series, so while there is a strong relationship between the two time series, the cross-correlation is not very close to 1.

As a result, the spectral coherence is sometimes used: $$C_{xy}(f) = \frac{|G_{xy}(f)|^2}{G_{xx}(f)G_{yy}(f)}$$ where $G_{km}$ is the cross-spectral density between $k$ and $m$.

This is now a function of frequency, and the closer it is to 1 at a given frequency, the closer the two signals are related.

I've put together a simple example in R which illustrates the difference between cross-correlation and (spectral) coherence.

The top graph shows the two time series. The middle graph shows the cross-correlation is less than 1. The bottom graph shows the spectral coherence as a function of frequency. You can see that the time series are not as similar in the higher frequencies as they are at low frequency.

Sometimes, the integral of these values is used over a limited bandwidth so that a single figure of similarity is arrived at.

R Code Below

  #15466

x1 <- rnorm(1000)

h <- c(1,3,2,1)/7
x2 <- filter(x1,h, circular=TRUE) + rnorm(1000)/10

par(mfrow=c(3,1))

plot(x1,type="l",col="blue")
lines(x2, col="green",lwd=5)
title("Original and filtered noise signals")

ccf(x1,x2)

Ixy <- spectrum(cbind(x1,x2), plot=FALSE,spans=c(5,7))

plot(Ixy\$coh)h)