Engee documentation
Notebook

Alignment of time-shifted signals

Often during measurements, data comes asynchronously from different sensors. The data must be synchronised in order to create a comprehensive picture when using multiple sensors.

For this application we need a DSP library for digital signal processing and a FileIO library for loading JLD2 files.

In [ ]:
Pkg.add( ["DSP", "JLD2"] )

Let's read the input data and plot the graphs:

In [ ]:
using JLD2
@load "relategsignals.jld2"

plot(
    plot( s1, label=L"s_1" ),
    plot( s2, label=L"s_2" ),
    plot( s3, label=L"s_3" ),
    layout=(3,1), link = :x
)
Out[0]:

As we can see, the signal s2 comes before s1, and s1, in turn, is ahead of s3. The exact delay between signals of the same length can be found using the function DSP.Util.filddelay, and to apply it to signals of different lengths, we reduce the length of all signals to the length of the smallest of them.

As a result, we will notice that s2 is 350 samples ahead of the signal s1, that s3 is late relative to s1 by 150 samples, and that the signal s2 is 500 samples ahead of s3.

In [ ]:
using DSP

min_size = min( size(s1,1), size(s2,1), size(s3,1) )
t12 = DSP.Util.finddelay(s1[1:min_size], s2[1:min_size])
t31 = DSP.Util.finddelay(s3[1:min_size], s1[1:min_size])
t32 = DSP.Util.finddelay(s3[1:min_size], s2[1:min_size])

t12, t31, t32
Out[0]:
(350, 150, 500)

Let's align the signals manually with respect to s2 (which comes the earliest):

In [ ]:
plot( s1[t12+1:end], label=L"s_1", size=(600,300) )
plot!( s2, label=L"s_2" )
plot!( s3[t32+1:end], label=L"s_3" )
Out[0]:

Units are added to the offset, because at offset 0 we need to output the signal starting at index 1. It would be easier to use the function DSP.Util.shiftsignal.

To avoid intermediate operations, we can use the function alignsignals. It delays earlier signals, so that all signals will be shifted to the moment of signal onset s3.

In [ ]:
x1 = s1[1:min_size]
x2,t21 = DSP.Util.alignsignals( s2[1:min_size], s1[1:min_size] )
x3,t31 = DSP.Util.alignsignals( s3[1:min_size], s1[1:min_size] );

plot(
    plot( x1 ),
    plot( x2 ),
    plot( x3 ),
    layout=(3,1), leg=false
)
Out[0]:

Conclusion

Now the studied signals are synchronised in terms of onset time and we can proceed to further processing.