# Cross-correlation issue: how can I align two signals?

56 views (last 30 days)
Antonio Morales on 20 Jan 2017
Edited: John Chilleri on 20 Jan 2017
I have two signals A and B (please, see them attached), which have been recorded from different devices with different sampling frequencies, on the same events.
I am trying to align both signals through cross-correlation methods. However, when using the alignsignals function, it does seem to actually delay even more the signals:
% Sampling frequencies
Fs_A = 1000; %
Fs_B = 1926; % 1925.93 Hz is the one provided by the device
% Resample signal A
A = resample(A, Fs_B, Fs_A);
% Plot of the two signals "delayed"
figure(1)
plot(A)
hold
plot(B*1000)
% Aling both signals
X = A;
Y = B;
[Xa,Ya,D] = alignsignals(X,Y,[],'truncate');
figure(2) % plot both signals "aligned"
plot(Xa)
hold
plot(Ya*1000)
I have also tried using xcorr function, with similar result:
% Sampling frequencies
Fs_A = 1000; %
Fs_B = 1926; % 1925.93 Hz is the one provided by the device
% Resample signal A
A = resample(A, Fs_B, Fs_A);
% Plot to visualize that one signal is delayed
figure(1)
plot(A)
hold
plot(B*1000)
% Aling both signals using xcorr
[C,lag] = xcorr(A,B);
figure(2)
plot(lag,C);
[M,I] = max(C);
D = lag(I);
figure(3),plot(1:length(A), A, 'b',1+abs(D):length(B)+abs(D), B*1000, 'r'), title(' "Synchronised" signals ');
Am I making any mistake? Maybe resampling?

John Chilleri on 20 Jan 2017
Edited: John Chilleri on 20 Jan 2017
Hello,
The reason (I suspect) the alignsignals is being weird is because of the huge negative spikes in your B signal. If we set these huge negative spikes to 0 for the align signals calculation, it will properly align the signals (what seems to be properly aligned):
% Sampling frequencies
Fs_A = 1000; %
Fs_B = 1926; % 1925.93 Hz is the one provided by the device
% Resample signal A
A = resample(A, Fs_B, Fs_A);
% Plot of the two signals "delayed"
figure(1)
plot(A)
hold
plot(B*1000)
% Aling both signals
X = A;
Y = B;
Y2 = Y;
Y2(Y2<0) = 0;
[Xa,Ya,D] = alignsignals(X,Y2,[],'truncate');
figure(2) % plot both signals "aligned"
plot(Xa)
hold
plot(Ya*1000)
See for yourself. If you want to still use the original B signal without the zeroing that I implemented, then utilize the delay D output from the alignsignals function, as the D output would remain correct. Simply apply the delay D to the unchanged Y and you'll have your desired result.
Hope this helps!