How to shift data sets automatically

조회 수: 15 (최근 30일)
Damaris Litton
Damaris Litton 2023년 3월 23일
답변: Mathieu NOE 2023년 3월 27일
I have two sets of data that produce curves (see attached), and I want to analsye them both so need the data points to line up. Is there a code I can use to get Matlab to line up the data points for me? i.e the peak point for U should be the same data point as the one for D. The matrices are the same size but can be shortened if necessary to do this.
  댓글 수: 3
Damaris Litton
Damaris Litton 2023년 3월 23일
Thank you @Vilém Frynta! I have attached my data, would it be possible for you to explain using the data sets? Thank you.
Vilém Frynta
Vilém Frynta 2023년 3월 23일
hello,
yes.
% load data
load U.mat
load D.mat
% make vectors from your data (previously columns)
U = U'
U = 1×2328
0.0043 0.0043 0.0045 0.0044 0.0047 0.0046 0.0047 0.0046 0.0051 0.0049 0.0050 0.0047 0.0048 0.0049 0.0048 0.0047 0.0046 0.0044 0.0043 0.0044 0.0049 0.0048 0.0046 0.0045 0.0047 0.0049 0.0046 0.0047 0.0046 0.0046
D = D'
D = 1×2328
0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0139 0.0140
% find maxs (peaks)
idx.U = find(U == max(U));
idx.D = find(D == max(D));
% difference between the peaks
idx.diff = idx.U - idx.D
idx = struct with fields:
U: 656 D: 398 diff: 258
% let's assume U is further
is_U_further = 1;
% find if D is further (if idx.diff is negative)
if idx.diff < 0
is_U_further = 0;
idx.diff = idx.D - idx.U % switch the difference in that case
end
% create NaN vector and new variables
L = length(U);
new.U = NaN([1 L+idx.diff]);
new.D = NaN([1 L+idx.diff]);
% fit the values so the peaks are at the same index
if is_U_further == 1; % if U is further... do this:
new.U(1:L) = U;
new.D(idx.diff:idx.diff+L-1) = D; % move D further, so it matches
end
if is_U_further == 0;% if D is further...
new.D(1:L) = D;
new.U(idx.diff:idx.diff+L-1) = U;
end
plot(new.U)
hold on
plot(new.D)
thought i did it wrong but it looks like "D" is just too small (no pun intended). you can try to strech that or make new, relative Y axis for it.

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

채택된 답변

Mathieu NOE
Mathieu NOE 2023년 3월 27일
hello
myabe this ?
I took the options to have both peaks overlaid and also the two curves have same initial y value
This is the result
of course you can change the code if your prefer to have same y final value but I doubt you can have start, end and peaks points all 3 overlaid
% load data
load U.mat
load D.mat
% find maxs (peaks)
[umax,iumax] = max(U);
[umin,iumin] = min(U);
% dU = umax- umin;
dU = umax- U(1);
[dmax,idmax] = max(D);
[dmin,idmin] = min(D);
% dD = dmax- dmin;
dD = dmax- D(1);
% D y dir stretch factor
D_ySt = dU/dD;
D = D*D_ySt;
% apply y shift to have both curves start at same value U(1)
yshift = D(1) - U(1);
D = D - yshift;
% x axis shift to align both peaks (delay D vs U)
xshift = iumax - idmax;
% define x axis vectors
xu = 1:numel(U)
xd = xu + xshift;
plot(xu,U,xd,D)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by