Transforming graphs horizontally and vertically to properly compare

5 views (last 30 days)
I have several files of data which, when plotted, look like the image presented. My problem is that i need them all to be laid on top of each other, with the beginning of the rise of the first sinusoidal wave matched up in the x and y. This is so that I can create an average waveform to reduce noise in the readings. Currently, my code impoorts all my text files, 30 of them, into an array and plots them using the following loop
for K = 1 : 30 %open and read the text files
S{K} = readtable(files(K));
Sr{K} = rmmissing(S{K}); %remove NaN values from the text files
end
for K = 1 : 30 %plot each indivdual graph for comparisons
plot(Sr{K}.(1), Sr{K}.(2), 'displayname', files(K));
hold on
end
I know how to transform them all individually, but cannot figure out how to make it all happen at once. I attempted to use a matchFeatures command, but could not figure out precisely how to do what I wished.
The goal is for the first graph to look similar to the second, where the initial rise is matched, allowing the peaks to be easily compared.
Any help would be appreciated, thank you!

Accepted Answer

Mathieu NOE
Mathieu NOE on 9 Jul 2025
hello
so I decided to do some x shift on your data based on the 3 major peaks x locs
you can use the regular findpeaks but I like the simplicity and speed of peakseek (fex : PeakSeek - File Exchange - MATLAB Central)
attached for your convenience
also I like this , because it makes the filenames and directoris names sorting correctly , what the regular dir is not capable of
and this is the result so far :
code :
fileDir = pwd; % current directory (or specify which one is the working directory)
S = dir(fullfile(fileDir,'Free*.txt')); % get list of data files in directory
S = natsortfiles(S); % sort file names into natural order , see :
%(https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
% init
ysum = 0;
% plot
figure
hold on
for k = 1:numel(S)
filename = S(k).name; % filenames are sorted
out = readmatrix( fullfile(fileDir, filename));
x = out(:,1);
dx = mean(diff(x),'omitnan');
y = out(:,2);
% take the first n major peaks
n = 3;
[locs, ~]=peakseek(abs(y),5,3);
locs = locs(1:n)';
alllocs(:,k) = locs;
if k>1
xshift = mean(alllocs(:,k) - alllocs(:,1));
else
xshift = 0 ;
xref = x; % keep this as the "good" x array for the fina plot
end
legstr{k} = filename(1:length(filename)-4);
plot(x-xshift*dx,y);
% sum y data for the mean curve)
ysum = ysum +y;
end
ymean = ysum/k; % mean curve
legstr{k+1} = 'mean';
plot(xref,ymean,'k','linewidth',2.5);
xlim([min(xref) max(xref)]);
legend(legstr);
  7 Comments
William Rose
William Rose on 10 Jul 2025
@Luke, you're welcome.
@Mathieu NOE, very nice. Good idea about using detrend too - with that plus your alignment code, the traces really line up well.

Sign in to comment.

More Answers (0)

Categories

Find more on ECG / EKG in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!