필터 지우기
필터 지우기

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

조회 수: 5 (최근 30일)
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?
Thank you for your answers!
load('signal_a.mat')
load('signal_b.mat')
load('time_a.mat')
load('time_b.mat')
%--------------------------------------------------------------------------
% 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
Mathieu NOE 2021년 4월 23일
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
load('signal_a.mat')
load('signal_b.mat')
load('time_a.mat')
load('time_b.mat')
%--------------------------------------------------------------------------
% 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')
  댓글 수: 4
Simon Beck
Simon Beck 2021년 4월 26일
Hi Mathieu NOE,
thank your very much. This is what I was looking for.
I have to dive more into programming with MATLAB I guess to achieve what I actually want.
Greetings,
Simon
Mathieu NOE
Mathieu NOE 2021년 4월 26일
Glad I could be of some help
Helping the others is also a way for me to improve my skills ...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by