Is it possible to align two data sets with irregular sampling while preserving the respective indices?

9 views (last 30 days)
I have 2 data sets. These measurements were taken on a test from different instruments at different sampling rates. There is a similarity in a way that when Data A becomes constant, Data B reaches the peak. There are various answers on the community but they have same sampling frequency. In this case the sampling is different and the sampling is not constant either. I want to align them without manually assigning anything. I also want to preserve the indexing of the original data points after alignment.
Currently, my code uses manual picking of points.
clc
clear all
close all
%% Load normalized data
ta = load('ta.mat'); ta = ta.ta;
tu = load('tu.mat'); tu = tu.tu;
xa = load('xa.mat'); xa = xa.xa;
xu = load('xu.mat'); xu = xu.xu;
%% Plotting the data
figure
tiledlayout(1,2)
nexttile
plot (ta,xa)
hold on
plot (tu,xu)
legend('Data A','Data UT', Location='southeast')
tanew = ta + 360.655; % I have identified the difference manually
nexttile
plot (tanew,xa)
hold on
plot (tu,xu)
legend('Data Anew','Data UT', Location='southeast')
I would appreciate your help in this regard.
Good day

Accepted Answer

Mathieu NOE
Mathieu NOE on 2 Nov 2022
hello
I gave it a try and this is my best result so far
first I simply used the time delat between the red dots (easy solution but not the best result) , then I refined the code and used the green dots
result
code
clc
clear all
close all
%% Load normalized data
load('ta.mat');
load('tu.mat');
load('xa.mat');
load('xu.mat');
% ta / xa : get start and finish points of first flat sections (for ta < 1000)
ta_diff = [0 ; diff(ta(ta<1000))];
[val,idx] = max(ta_diff);
ta_flat1_start = ta(idx-1);
xa_flat1_start = xa(idx-1);
ta_flat1_end = ta(idx);
xa_flat1_end = xa(idx);
% tu / xu : get first peak above y threshold = xa_flat1_end
minpeakdist = 200; % minpeakdist is the minimum desired distance between peaks (optional, defaults to 1)
[LOCS1, PKS1]=peakseek(xu,minpeakdist,xa_flat1_end); % you can probably get similar results with findpeaks for those who prefer
% find valleys (end of exp decay) by using the negated (and smoothed) xu data
xus = smoothdata(xu,'sgolay',30,'degree',1);
[LOCS2, PKS2]=peakseek(-xus,minpeakdist);
LOCS2 = LOCS2(LOCS2>LOCS1(1)); % get LOCS2 indices for LOCS2 above LOCS1(1)
tu2 = tu(LOCS2(1)); % get tu values accordingly
xu2 = xu(tu>tu2); % get xu values accordingly
idx_tu2_refined = find(xu2>xu(LOCS2(1))+1e-3,1,'first'); % we are looking for a few samples latter than the knee itself
% time delta = td
td = tu(LOCS2(1)+idx_tu2_refined) - ta_flat1_end;
%% Plotting the data
figure
tiledlayout(1,2)
nexttile
plot (ta,xa,'+-',ta_flat1_start,xa_flat1_start,'dr',ta_flat1_end,xa_flat1_end,'dg',...
tu,xu,'+-',tu,xus,'-',tu(LOCS1(1)),xu(LOCS1(1)),'*r',tu(LOCS2(1)+idx_tu2_refined),xu(LOCS2(1)+idx_tu2_refined),'*g');
% legend('Data A','Data UT', 'Location','southeast')
% tanew = ta + 360.655; % I have identified the difference manually
tanew = ta + td; % I have identified the difference automatically
nexttile
plot (tanew,xa,tu,xu)
% legend('Data Anew','Data UT', 'Location','southeast')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [locs, pks]=peakseek(x,minpeakdist,minpeakh)
% x is a vector input (generally a timecourse)
% minpeakdist is the minimum desired distance between peaks (optional, defaults to 1)
% minpeakh is the minimum height of a peak (optional)
%
% (c) 2010
% Peter O'Connor
% peter<dot>ed<dot>oconnor .AT. gmail<dot>com
if size(x,2)==1, x=x'; end
% Find all maxima and ties
locs=find(x(2:end-1)>=x(1:end-2) & x(2:end-1)>=x(3:end))+1;
if nargin<2, minpeakdist=1; end % If no minpeakdist specified, default to 1.
if nargin>2 % If there's a minpeakheight
locs(x(locs)<=minpeakh)=[];
end
if minpeakdist>1
while 1
del=diff(locs)<minpeakdist;
if ~any(del), break; end
pks=x(locs);
[garb, mins]=min([pks(del) ; pks([false del])]); %#ok<ASGLU>
deln=find(del);
deln=[deln(mins==1) deln(mins==2)+1];
locs(deln)=[];
end
end
if nargout>1
pks=x(locs);
end
end
  2 Comments
Mathieu NOE
Mathieu NOE on 3 Nov 2022
hello again
we can use interp1 to have a common x axis , but I am not sure what your really mean by "keeping" (or not) the indexing ? what index ?
is the result obtained so far matching your expectations or did I miss something ?

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 3 Nov 2022

Community Treasure Hunt

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

Start Hunting!