We have seen that correlation can be used as a tool to detect periodicity within a signal. Now we are going to use the correlation to align signals. We start with two segments of ECG of the polygraphic recording. We have extracted two segments containing one ECG complex and want to align them
-->stacksize(20000000) -->load signals -->ecg = signals(12); -->ndx1 = 50; -->ndx2 = 130; -->wind = 70; -->beat1 = ecg(ndx1:ndx1+wind); -->beat2 = ecg(ndx2:ndx2+wind); -->corrbeat = corr(beat1,beat2,70); -->xsetech([0,0,1,1/3]) -->plot(beat1) -->xsetech([0,1/3,1,1/3]) -->plot(beat2) -->xsetech([0,2/3,1,1/3]) -->plot(corrbeat)
And the result is
![]() |
As we can see, both signals are similar but are not aligned. We are going to use correlation to align them. We can see that correlation reaches a maximum value at the point where both signals are maximally correlated. So, we can use this value to align them.
-->xbasc() -->xsetech([0,0,1,1]) -->[maxim,delay]=max(corrbeat); -->plot(ecg(ndx1:ndx1+wind)); -->plot(ecg(ndx2+delay-1:ndx2+delay-1+wind));
Notice that we calculate the delay with 'max'. This command returns the maximum value of the correlation as well as its position. Then we use the value of the position to advance the second signal. (In Scilab the first index of arrays takes the value of `1' instead of `0'. We correct this fact by subtracting `1').
One important fact is that aligning signals in this way the whole trace is considered (not only the point at which a level is reached). If our signal contains a huge artifact it can alter the process of alignment. If we want align some component we can prefilter the signal (high pass or low pass) to line the components to which we are interested and, once aligned, we can consider the whole signal.