# how can we align wto signals with a time lag in between?

I have two signals from two different sensors. I cropped the area i am interested in from both sensor signals. The problem is that the signals start at different time instants. I am searching for a way to align the signals just like below. It is technically just moving the blue signal to align with orange one. I tried using cross correlation but it gave a weird result. Any help please. first image: this is the result of cross correlation of two signals Second image :after applying code on cross correlation to align   • What exactly was weird about your cross correlation results ? – Hilmar Dec 3 '19 at 14:22
• Try removing the mean from both samples prior to doing the cross correlation – Dan Boschen Dec 3 '19 at 14:25
• I use 'lag=np.argmax(signal.correlate(x,y))' where x and y are the signals. This function typically calculates the index at which maximum cross correlation occurs. The index from what I understood is considered to be the time lag. So if I shift the signal Y by 'lag' it should be aligned with x. However the value 'lag' I get from the function is more than the length of the signal. x length 10880 y length 11035 lag i get is 11770. – Naima Abiad Dec 3 '19 at 14:41
• I've deleted my answer as I misunderstood the plots. I thought the blue and orange signals were the same but delayed, but they are actually different signals altogether! – Engineer Dec 4 '19 at 19:53

## 2 Answers

I thought you might have to smooth, sort or create an envelope of your signal, which might help. But then i found this. This should help you.

Or if you wish to use MATLAB, you can use this command.

If the signals themselves are entirely different and you just want to align based on the envelope, then in that case, take the envelope by low pass filtering the absolute value, scale each by its rms value, and adjust the delay while subtracting the two scaled envelopes and evaluate the least squared error (sum the square of the errors between the waveforms sample by sample), when this sum is at the minimum you have optimally aligned the two.