How to measure the time dependent correlation of two signals

I have two signals (time series) shown in the plot below. Just by looking at the figure, we can see that the two main peaks of both signals are very closely aligned (correlated), however the red signal has additional features elsewhere which don't match the blue curve.

I am looking for some function which can tell me how correlated these two signals are at a given time. IE: at t = -18 and t = +10 where the peaks are located, the correlation should be very close to -1, while at t = -40, the correlation is probably close to 0.

Specifically, I want a robust way to tell that if I see a peak in the blue curve, then there is a corresponding (negative) peak in the red curve.

I've looked into Cross correlation, however this seems suitable for time series which simply have a constant time lag with respect to each other. In my problem the correlation between my two signals is changing with time. I've thought about dividing up the signal into small bins and computing the correlation of each bin, but I was hoping there is a better way.

• You could try looking at how the cross-correlation evolves over time. This would be analogous to a spectrogram, only instead of your axes being time and frequency they would be time and correlation lag. Commented Dec 8, 2015 at 18:52

Your signals look like they have about the same ordinal scale, and little lag. Hence, you can perform local correlations: take a segment of both signals centered at a time location, you can normalize them in amplitude and dispersion (divise by mean and standard deviation, or median and median absolute deviation). Apply a smoothing window: if you do not do that, remember you are already applying a rectangular window. Then do a cross-correlation, and repeat over time.

Such a process creates a 2D image from two 1D signals. It could be called windowed cross-correlation (see also Windowed cross correlation in Matlab), or local cross-correlation. From the latter:

We address the problem of capturing and tracking local correlations among time evolving time series. Our approach is based on comparing the local auto-covariance matrices (via their spectral decompositions) of each series and generalizes the notion of linear cross-correlation

If you are unsure about scales, or want of more generic setting, some perform cross-correlation in a multiscale wavelet domain. You can check the concept of wavelet coherence (Matlab), illustrated by Torrence et al. Interdecadal Changes in the ENSO–Monsoon System , with Matlab software

edit sorry, troughs in red, peaks in blue. I'm on my phone and couldn't see the graphs as I wrote.

It sounds like you want to correlate features of the time series, not the time series themselves. In particular, it looks like you want to detect peaks in the red curve and troughs in the blue graph. Then you want to correlate the presence and position of those features.

Are you guaranteed that peaks are always the interesting things in the red curve? Are troughs always what you want to find in the blue curve? If you can characterize what you're looking for, then do feature detection on each time series. What you want as output is another pair of time series which indicate the times at which your features of interest appear. Once you have these feature time series, correlate them.

Good luck!