Transforming graphs horizontally and vertically to properly compare

조회 수: 4 (최근 30일)
Luke
Luke 2025년 7월 9일
댓글: Mathieu NOE 2025년 7월 10일
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!
  댓글 수: 2
Mathieu NOE
Mathieu NOE 2025년 7월 9일
hello
idea could be to isolate the first peak to do the x / y repositionning
could you share some data
I believe this is not too much complicated

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

채택된 답변

Mathieu NOE
Mathieu NOE 2025년 7월 9일
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
William Rose
William Rose 2025년 7월 10일
@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.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 ECG / EKG에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by