# 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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.9331647,"math_prob":0.8905608,"size":2144,"snap":"2021-04-2021-17","text_gpt3_token_len":494,"char_repetition_ratio":0.13644859,"word_repetition_ratio":0.0,"special_character_ratio":0.23927239,"punctuation_ratio":0.09219858,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9857317,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,5,null,5,null,5,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-15T02:01:49Z\",\"WARC-Record-ID\":\"<urn:uuid:ee82f78c-49ba-438b-ae26-470af6a7e465>\",\"Content-Length\":\"185049\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:51ac81cb-4815-4a59-b2a5-8010e9284a69>\",\"WARC-Concurrent-To\":\"<urn:uuid:8aee24db-0cb5-49b6-9349-297338dcbc18>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://dsp.stackexchange.com/questions/62346/how-can-we-align-wto-signals-with-a-time-lag-in-between/62377\",\"WARC-Payload-Digest\":\"sha1:ZCG4ONAH7USIJ5WD7LOAX5CE5FISPQRW\",\"WARC-Block-Digest\":\"sha1:YIL6V6HHMNPVKQRB64CBCGFYP3CKMP6J\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038082988.39_warc_CC-MAIN-20210415005811-20210415035811-00276.warc.gz\"}"}