- using max and find, find the highest value (peak) and it's position (index)
- using simple logic, find which maximum is "further" (which index is higher). then, find the difference between the distances of peak (difference in index numbers)
- using NaN, create vector full of NaNs, the length will be length of the original data + difference of the maximum indexes
- using indexing and knowledge about position of the peaks, insert your data into your NaN vector.
How to shift data sets automatically
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Damaris Litton
el 23 de Mzo. de 2023
Respondida: Mathieu NOE
el 27 de Mzo. de 2023
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 comentarios
Vilém Frynta
el 23 de Mzo. de 2023
hello,
yes.
% load data
load U.mat
load D.mat
% make vectors from your data (previously columns)
U = U'
D = D'
% 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
% 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.
Respuesta aceptada
Mathieu NOE
el 27 de Mzo. de 2023
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 comentarios
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!