I have 2 datasets which vary with time as shown in the figure - Sa – Red , Sb – BLACK . I am doing the analysis in R. The entire dataset and a segment of data from 30 - 31 s are shown below - enter image description here -

As you can see from the figures, the datasets appear similar but one dataset leads (or lags) the other dataset in time. The downward peaks appear to be slightly shifted in time for one of the signals wrt the other. I would like to determine the following –

  1. The baseline value (reference) for the two signals – Is there a statistical method to find out this? Is there some signal processing techniques that need to applied before determining the baseline? For Sa, the baseline is more noisy and shifts up and down – so it seems difficult to assume a constant reference line

  2. The time lag / lead between these two signals – I tried to make the reference line constant by removing data points above an approximate baseline and replacing it with the approx baseline value. Then I found out values sa1, sa2, sa3, sa4, ..etc. Similarly, I found sb1 ,sb2, sb3, sb4,… etc.

enter image description here

2a. Is there a way I can map sa1 to sb1, sa2 to sb2..and so on? The idea is to calculate (sa1-sb1), (sa2-sb2),… and find out the average time lag/ lead for the two signals over the entire dataset. I understand that this method is very approximate, since the resolution is lost while assuming a baseline based on just eyeballing.

2b. Is there another method for calculating the time lag/ lead? Or the exact times sa1,sa2.. etc at which the signal drops below the reference line and comes back to its reference line? Will a cross correlation work? How do I use it for noisy signals?

3.The frequency of the downward peaks

  • $\begingroup$ there is a curious offset it has for Sa. the baseline for Sb had dropped 0.2 from the running max value, where the Sa baseline didn't have the same drop in value. i just think you need to define mathematically what the baseline is doing. $\endgroup$ Sep 23, 2018 at 1:15

1 Answer 1


You can try the function 'ccf' in R. The way I would setup your data is: df= time | red | black | red diff | black diff |

** make sure your time interval's are constant. i.e every 5 seconds/10 seconds etc

red diff and black diff being the difference between the adjacent values in the red and black data columns, respectively. i.e. diff(df$red) = red diff

Then do a ccf on the 2 difference columns and adjust 'lag.max' as necessary to view the plot that is generated. There should be a peak in the graph which is the lag (that occurs the most) between red and black.

  • $\begingroup$ Hi: This won't answer your question but regardless of whether you work in time domain or frequency domain, the first thing you should do is decide on what granularity you're interested in. in other words, suppose you want to know how much one leads ( or lags ) the other when considering values every x seconds. Then, depending on that answer, you need to smooth the data accordingly. The ccf results will depend critically on the time frame being considered and you'll obviously get totally different results depending on what you use. $\endgroup$
    – mark leeds
    Jul 25, 2018 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.