Perform cross co-relation to find best fit

Hello Community,
I have two signals s_acti.mat and phs1.mat.
how do i find the time interval of s_acti that best fits with phs1.
i intend to perform cross corelation and find the suitable time frame in s_acti that best fits with phs1.

Antworten (1)

Mathieu NOE
Mathieu NOE am 13 Dez. 2021

0 Stimmen

hello
the two signals have a slightly (but constant) slopes , so the best estimation of time delta is measured at half the amplitude
>> dt = - t0_neg1 + t0_neg2
dt = 1.1076e-08
code :
clc
clearvars
load('s_acti.mat');
time1 = s_acti(:,1);
data1 = s_acti(:,2);
load('phs1.mat');
time2 = phs1(:,1);
data2 = phs1(:,2);
%% my code below
threshold = min(data1)/2; % your value here
[t0_pos1,s0_pos1,t0_neg1,s0_neg1]= crossing_V7(data1,time1,threshold,'linear'); % positive (pos) and negative (neg) slope crossing points
% ind => time index (samples)
% t0 => corresponding time (x) values
% s0 => corresponding function (y) values , obviously they must be equal to "threshold"
[t0_pos2,s0_pos2,t0_neg2,s0_neg2]= crossing_V7(data2,time2,threshold,'linear'); % positive (pos) and negative (neg) slope crossing points
figure(4)
plot(time1,data1,time2,data2,time1,threshold*ones(size(time1)),'k--',t0_neg1,s0_neg1,'dr',t0_neg2,s0_neg2,'dg','linewidth',2,'markersize',12);grid on
legend('signal 1','signal 2','threshold','negative slope crossing points signal 1','negative slope crossing points signal 2');
dt = - t0_neg1 + t0_neg2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [t0_pos,s0_pos,t0_neg,s0_neg] = crossing_V7(S,t,level,imeth)
% [ind,t0,s0,t0close,s0close] = crossing_V6(S,t,level,imeth,slope_sign) % older format
% CROSSING find the crossings of a given level of a signal
% ind = CROSSING(S) returns an index vector ind, the signal
% S crosses zero at ind or at between ind and ind+1
% [ind,t0] = CROSSING(S,t) additionally returns a time
% vector t0 of the zero crossings of the signal S. The crossing
% times are linearly interpolated between the given times t
% [ind,t0] = CROSSING(S,t,level) returns the crossings of the
% given level instead of the zero crossings
% ind = CROSSING(S,[],level) as above but without time interpolation
% [ind,t0] = CROSSING(S,t,level,par) allows additional parameters
% par = {'none'|'linear'}.
% With interpolation turned off (par = 'none') this function always
% returns the value left of the zero (the data point thats nearest
% to the zero AND smaller than the zero crossing).
%
% check the number of input arguments
error(nargchk(1,4,nargin));
% check the time vector input for consistency
if nargin < 2 | isempty(t)
% if no time vector is given, use the index vector as time
t = 1:length(S);
elseif length(t) ~= length(S)
% if S and t are not of the same length, throw an error
error('t and S must be of identical length!');
end
% check the level input
if nargin < 3
% set standard value 0, if level is not given
level = 0;
end
% check interpolation method input
if nargin < 4
imeth = 'linear';
end
% make row vectors
t = t(:)';
S = S(:)';
% always search for zeros. So if we want the crossing of
% any other threshold value "level", we subtract it from
% the values and search for zeros.
S = S - level;
% first look for exact zeros
ind0 = find( S == 0 );
% then look for zero crossings between data points
S1 = S(1:end-1) .* S(2:end);
ind1 = find( S1 < 0 );
% bring exact zeros and "in-between" zeros together
ind = sort([ind0 ind1]);
% and pick the associated time values
t0 = t(ind);
s0 = S(ind);
if ~isempty(ind)
if strcmp(imeth,'linear')
% linear interpolation of crossing
for ii=1:length(t0)
%if abs(S(ind(ii))) >= eps(S(ind(ii))) % MATLAB V7 et +
if abs(S(ind(ii))) >= eps*abs(S(ind(ii))) % MATLAB V6 et - EPS * ABS(X)
% interpolate only when data point is not already zero
NUM = (t(ind(ii)+1) - t(ind(ii)));
DEN = (S(ind(ii)+1) - S(ind(ii)));
slope = NUM / DEN;
slope_sign(ii) = sign(slope);
t0(ii) = t0(ii) - S(ind(ii)) * slope;
s0(ii) = level;
end
end
end
% extract the positive slope crossing points
ind_pos = find(sign(slope_sign)>0);
t0_pos = t0(ind_pos);
s0_pos = s0(ind_pos);
% extract the negative slope crossing points
ind_neg = find(sign(slope_sign)<0);
t0_neg = t0(ind_neg);
s0_neg = s0(ind_neg);
else
% empty output
ind_pos = [];
t0_pos = [];
s0_pos = [];
% extract the negative slope crossing points
ind_neg = [];
t0_neg = [];
s0_neg = [];
end
end

6 Kommentare

Mathieu NOE
Mathieu NOE am 17 Dez. 2021
hello
problem solved?
Sajid Afaque
Sajid Afaque am 7 Feb. 2022
Hello , not excatly.
I want to find the instant of time at which the two signals start diverting from each other.
like in above figure till 21e-9 they appera to be similar (though exactly not same). it is for the signal sam_act and sam_p0.
so i need to find this instant 21e-9 second
This would be the first part,
later i would left shift the signal sam_act by 21e-9 seconds and again repeat the above process (this time for signals shifted_sam_act and sam_p1) and find the instance of time till when they appear to be nearly similar. i.e. 217e-9 seconds
so keeping any 1 scenario of above mentioned, i want to know how can i find the instant of time till the two signals appear to be similar
Sorry for the delayed response and thanks for the help
Mathieu NOE
Mathieu NOE am 2 Mär. 2022
hello Sajid
sorry , Iwas quite busy
did you finally come up with a solution ?
Sajid Afaque
Sajid Afaque am 14 Mär. 2022
Bearbeitet: Sajid Afaque am 14 Mär. 2022
First of all thank you very much for your time.
Yes eventually i came up with a solution.
i wanted to find the time intervals at which subsignals sam_p0 and sam_p1 start in the main signal sam_act.
slightly twisted the idea i initially wanted to implement and used the below steps
  1. I used a technique more or less similar to binary search,where i divided the entire time range of main signals into n intervals.
  2. placed my desired subsignal at these intervals calculated the error(area between two signals) , searching for minimum error
  3. Considered this interval which resulted into minimum error along with its neighbouring interval and again divided this whole interval range into n intervals
  4. Continued the above iteration process until my error was very minimal.
Mathieu NOE
Mathieu NOE am 14 Mär. 2022
hello Sajid
good news and happy that you could find a solution to your problem
all the best
M
Sajid Afaque
Sajid Afaque am 14 Mär. 2022
Thanks !!

Melden Sie sich an, um zu kommentieren.

Produkte

Version

R2020b

Gefragt:

am 10 Dez. 2021

Kommentiert:

am 14 Mär. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by