Using spectral coherence for computing similarity between time series data
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 &lt;- rnorm(1000)

  h &lt;- c(1,3,2,1)/7
  x2 &lt;- 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 &lt;- spectrum(cbind(x1,x2), plot=FALSE,spans=c(5,7))

  plot(Ixy$coh)h)