# How can I avoid offset at start and end of signal when using xcorr-function?

2 views (last 30 days)
Simon Beck on 23 Apr 2021
Commented: Mathieu NOE on 26 Apr 2021
I have two signals:
signal_a, 200 Hz with time time_a
signal_b 40 Hz with time_b.
First I downsampled signal a to the same sampling rate of signal_b.
Then I precut the signals to fit them better, used detrend to make them better compareable.
Finally I used the Xcorr-function to fit the two signals on the x-axis.
I have this lag at the beginning and at the end of the signals . Is there a way to avoid this? (I know this is how xcorr-function works.)
Or do I have to cut the signal in several pieces, then use xcorr and put them back together?
%--------------------------------------------------------------------------
% calculating sampling rate of signal_a and signal_b
%--------------------------------------------------------------------------
Ts_a = mean(diff(time_a));
Fs_a = 1/Ts_a;
Ts_b = mean(diff(time_b));
Fs_b = 1/Ts_b;
%--------------------------------------------------------------------------
% downsample signal a and detrend both signals
%--------------------------------------------------------------------------
signal_a_downsample = resample(signal_a,time_a,Fs_b);
signal_a_detrend = detrend(signal_a_downsample);
signal_b_detrend = detrend(signal_b);
%--------------------------------------------------------------------------
% cutting the signals to same begin- and end timestamps
%--------------------------------------------------------------------------
sig_a = signal_a_detrend(40:9095)
sig_b = signal_b_detrend(104:9270)
%--------------------------------------------------------------------------
% using xcorrr-function to fit signals on x-axis
%--------------------------------------------------------------------------
[c_a, lag_a] = xcorr(sig_b,sig_a);
c_a = c_a/max(c_a);
[m_a, i_a] = max(c_a);
t_a = lag_a(i_a);
sig_b_offset = sig_b(t_a:end);
%--------------------------------------------------------------------------
% plotting the results
%--------------------------------------------------------------------------
subplot(3,1,1)
plot(time_b, signal_a_detrend)
hold on
plot(time_b, signal_b_detrend)
sgtitle('signal_a and signal_b fitting with xcorr-function')
title('raw data signal_a and signal_b')
legend('signal_a', 'signal_b')
subplot(3,1,2)
plot(time_b(1:length(sig_a)), sig_a)
hold on
plot(time_b(1:length(sig_b)), sig_b)
title('precut, downsample, detrend')
legend('signal_a', 'signal_b')
subplot(3,1,3)
plot(time_b(1:length(sig_a)),sig_a,'b')
hold on
plot(time_b(1:length(sig_b_offset)), sig_b_offset, 'r')
title('precut, downsample, detrend - xcorr-fit -')
legend('signal_a', 'signal_b')

Mathieu NOE on 23 Apr 2021
hello Simon
I tweaked a bit your code ; see my comments and mods
no more manual cuts needed it's all automatic; also there is one section now that can be removed as it's no more needed
--------------------------------------------------------------------------
% cutting the signals to same begin- and end timestamps
%--------------------------------------------------------------------------
% sig_a = signal_a_detrend(40:9095)
% sig_b = signal_b_detrend(104:9270)
% basically we can skip this section now as the manual cut is no more
% needed
sig_a = signal_a_detrend;
sig_b = signal_b_detrend;
so you can now simplify your code
hope it helps
full code below :
clearvars
%--------------------------------------------------------------------------
% calculating sampling rate of signal_a and signal_b
%--------------------------------------------------------------------------
Ts_a = mean(diff(time_a));
Fs_a = 1/Ts_a;
Ts_b = mean(diff(time_b));
Fs_b = 1/Ts_b;
%--------------------------------------------------------------------------
% downsample signal a and detrend both signals
%--------------------------------------------------------------------------
% signal_a_downsample = resample(signal_a,time_a,Fs_b);
signal_a_downsample = interp1(time_a,signal_a,time_b); % I prefer this method as it will ensure that both data share the same time axis
signal_a_detrend = detrend(signal_a_downsample);
signal_b_detrend = detrend(signal_b);
figure(1)
plot(time_b, signal_a_detrend)
hold on
plot(time_b, signal_b_detrend)
%--------------------------------------------------------------------------
% cutting the signals to same begin- and end timestamps
%--------------------------------------------------------------------------
% sig_a = signal_a_detrend(40:9095)
% sig_b = signal_b_detrend(104:9270)
% basically we can skip this section now as the manual cut is no more
% needed
sig_a = signal_a_detrend;
sig_b = signal_b_detrend;
%--------------------------------------------------------------------------
% using xcorrr-function to fit signals on x-axis
%--------------------------------------------------------------------------
[c_a, lag_a] = xcorr(sig_b,sig_a);
% c_a = c_a/max(c_a); % not needed
[m_a, i_a] = max(c_a);
t_a = lag_a(i_a);
sig_b_offset = sig_b(t_a:end);
time_b_offset = time_b(1:length(sig_b_offset));
sig_a_trunc = sig_a(1:length(sig_b_offset));
%--------------------------------------------------------------------------
% plotting the results
%--------------------------------------------------------------------------
subplot(3,1,1)
plot(time_b, signal_a_detrend)
hold on
plot(time_b, signal_b_detrend)
sgtitle('signal_a and signal_b fitting with xcorr-function')
title('raw data signal_a and signal_b')
legend('signal_a', 'signal_b')
subplot(3,1,2)
% plot(time_b(1:length(sig_a)), sig_a)
plot(time_b, sig_a)
hold on
% plot(time_b(1:length(sig_b)), sig_b)
plot(time_b, sig_b)
title('precut, downsample, detrend')
legend('signal_a', 'signal_b')
% subplot(3,1,3)
% plot(time_b(1:length(sig_a)),sig_a,'b')
% hold on
% plot(time_b(1:length(sig_b_offset)), sig_b_offset, 'r')
% title('precut, downsample, detrend - xcorr-fit -')
% legend('signal_a', 'signal_b')
subplot(3,1,3)
plot(time_b_offset,sig_a_trunc,'b')
hold on
plot(time_b_offset, sig_b_offset, 'r')
title('precut, downsample, detrend - xcorr-fit -')
legend('signal_a', 'signal_b')
Mathieu NOE on 26 Apr 2021
Glad I could be of some help
Helping the others is also a way for me to improve my skills ...

R2021a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!